es之java操作插⼊⽂档
4⽅式:
1、使⽤json字符串直接创建
2、使⽤Map集合
3、使⽤第三⽅库来序列化  createDocumentBySerialize
4、使⽤内置的帮助器XContentFactory.jsonBuilder()
1:使⽤JSON字符串创建
@Test
public void createDocumentByManually(){
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
//IndexRequestBuilder prepareIndex(String index, String type)
final IndexResponse response  = ansportClient.prepareIndex("twitter", "tweet")
.setSource(json, XContentType.JSON).get();
文档字符串是什么//获取索引
final String _index = Index();
//获取类型
final String _type = Type();
// ⽂档ID
String _id = Id();
// 版本
long _version = Version();
// 返回的操作状态
RestStatus status = response.status();
System.out.println("索引名称:"+_index+" "+"类型 :" +  _type + " ⽂档ID:"+_id+" 版本:"+_version+" 返回的操作状态:"+status); }
2:使⽤Map集合
@Test
public void createDocumentByMap(){
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
//ansportClient.prepareIndex 可以传⼊id
final IndexResponse response = ansportClient.prepareIndex("twitter", "tweet")
.setSource(json, XContentType.JSON).get();
//获取索引
final String _index = Index();
//获取类型
final String _type = Type();
// ⽂档ID
String _id = Id();
/
/ 版本
long _version = Version();
// 返回的操作状态
RestStatus status = response.status();
System.out.println("索引名称:"+_index+" "+"类型 :" +  _type + " ⽂档ID:"+_id+" 版本:"+_version+" 返回的操作状态:"+status); }
3:使⽤第三⽅库来序列化
/**
*这种⽅式是使⽤jsckson来序列化⼀个bean的⽅式进⾏操作的
* import com.fasterxml.jackson.databind.*;
* */
@Test
public void createDocumentBySerialize(){
try {
// insstance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
//构造⼀个类
Person p = new Person();
p.setUser("kimchy");
p.setPostDate(new Date());
p.setMessage("trying out Elasticsearch");
// generate json
byte[] json = mapper.writeValueAsBytes(p);
IndexResponse response = this.client.prepareIndex("twitter3", "tweet")
.setSource(json, XContentType.JSON)
.get();
// 索引名称
String _index = Index();
// 类型
String _type = Type();
// ⽂档ID
String _id = Id();
// 版本
long _version = Version();
/
/ 返回的操作状态
RestStatus status = response.status();
System.out.println("索引名称:"+_index+" "+"类型 :" +  _type + " ⽂档ID:"+_id+" 版本:"+_version+" 返回的操作状态:"+status);    } catch (JsonProcessingException e) {
e.printStackTrace();
}
}
4:使⽤内置的帮助器jsonBuilder()
@Test
public void createDocumentByJsonBuilder(){
XContentBuilder builder = null;
try {
builder = jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject();
String json = builder.string();
IndexResponse response = this.client.prepareIndex("twitter4", "tweet")
.setSource(json, XContentType.JSON)
.get();
// 索引名称
String _index = Index();
// 类型
String _type = Type();
// ⽂档ID
String _id = Id();
// 版本
long _version = Version();
// 返回的操作状态
RestStatus status = response.status();
System.out.println("索引名称:"+_index+" "+"类型 :" +  _type + " ⽂档ID:"+_id+" 版本:"+_version+" 返回的操作状态:"+status);    } catch (IOException e) {
e.printStackTrace();
}
}
去elasticsearch的head页⾯查看:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。