1、 使用json字符串直接创建 2、 使用Map集合 3、 使用第三方库来序列化 createDocumentBySerialize 4、 使用内置的帮助器XContentFactory.jsonBuilder()
@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 = this.transportClient.prepareIndex("twitter", "tweet") .setSource(json, XContentType.JSON).get(); //获取索引 final String _index = response.getIndex(); //获取类型 final String _type = response.getType(); // 文档ID String _id = response.getId(); // 版本 long _version = response.getVersion(); // 返回的操作状态 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"); //this.transportClient.prepareIndex 可以传入id final IndexResponse response = this.transportClient.prepareIndex("twitter", "tweet") .setSource(json, XContentType.JSON).get(); //获取索引 final String _index = response.getIndex(); //获取类型 final String _type = response.getType(); // 文档ID String _id = response.getId(); // 版本 long _version = response.getVersion(); // 返回的操作状态 RestStatus status = response.status(); System.out.println("索引名称:"+_index+" "+"类型 :" + _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status); }
/** *这种方式是使用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 = response.getIndex(); // 类型 String _type = response.getType(); // 文档ID String _id = response.getId(); // 版本 long _version = response.getVersion(); // 返回的操作状态 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 = response.getIndex(); // 类型 String _type = response.getType(); // 文档ID String _id = response.getId(); // 版本 long _version = response.getVersion(); // 返回的操作状态 RestStatus status = response.status(); System.out.println("索引名称:"+_index+" "+"类型 :" + _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status); } catch (IOException e) { e.printStackTrace(); } }
去elasticsearch的head页面查看: