[ElasticSearch]Java API 之 索引文档 (Index API)
Index API 允许我们存储一个JSON格式的文档,使数据可以被搜索。文档通过index、type、id唯一确定。我们可以自己提供一个id,或者也使用Index API 为我们自动生成一个。
这里有几种不同的方式来产生JSON格式的文档(document):
(1)手动方式,使用原生的byte[]或者String
(2)使用Map方式,会自动转换成与之等价的JSON
(3)使用第三方库来序列化beans,如Jackson
(4)使用内置的帮助类 XContentFactory.jsonBuilder()
1. 手动方式
/*** 手动方式 产生JSON 索引文档* @param client* @param index* @param type* @param id* @param json*/public static boolean indexDocByJSON(Client client, String index, String type, String id, String json) {// IndexIndexRequestBuilder indexRequestBuilder = client.prepareIndex();indexRequestBuilder.setIndex(index);indexRequestBuilder.setType(type);indexRequestBuilder.setId(id);indexRequestBuilder.setSource(json);indexRequestBuilder.setTTL(8000);// 执行IndexResponse indexResponse = indexRequestBuilder.get();return indexResponse.isCreated();}
测试,下面代码存储梅西信息到索引为football-index,类型为football-type,id为1的文档中:
@Testpublic void indexDocByJSON() throws Exception {String index = "football-index";String type = "football-type";String id = "1";String json = "{" +"\"club\":\"巴萨罗那\"," +"\"country\":\"阿根廷\"," +"\"name\":\"梅西\"" +"}";boolean result = IndexDocAPI.indexDocByJSON(client, index, type , id, json);logger.info("--------- indexDocByJSON {}", result);}
2. Map方式
/*** 使用Map 产生JSON 索引文档* @param client* @param index* @param type* @param id*/public static boolean indexDocByMap(Client client, String index, String type, String id, Map<String, Object> map) {// IndexIndexRequestBuilder indexRequestBuilder = client.prepareIndex();indexRequestBuilder.setIndex(index);indexRequestBuilder.setType(type);indexRequestBuilder.setId(id);indexRequestBuilder.setSource(map);indexRequestBuilder.setTTL(8000);// 执行IndexResponse indexResponse = indexRequestBuilder.get();return indexResponse.isCreated();}
测试,下面代码存储穆勒信息到索引为football-index,类型为football-type,id为2的文档中:
@Testpublic void indexDocByMap() throws Exception {String index = "football-index";String type = "football-type";String id = "2";Map<String, Object> map = Maps.newHashMap();map.put("name", "穆勒");map.put("club", "拜仁慕尼黑俱乐部");map.put("country", "德国");boolean result = IndexDocAPI.indexDocByMap(client, index, type , id, map);logger.info("--------- indexDocByMap {}", result);}
3. 序列化方式
/*** 利用Json序列化 产生JSON 索引文档** @param client* @param index* @param type* @param id*/public static boolean indexDocByBean(Client client, String index, String type, String id, Object bean) {// Bean转换为字节ObjectMapper mapper = new ObjectMapper();byte[] json;try {json = mapper.writeValueAsBytes(bean);} catch (JsonProcessingException e) {logger.error("---------- json 转换失败 Bean:{}", bean.toString());return false;}// IndexIndexRequestBuilder indexRequestBuilder = client.prepareIndex();indexRequestBuilder.setIndex(index);indexRequestBuilder.setType(type);indexRequestBuilder.setId(id);indexRequestBuilder.setSource(json);indexRequestBuilder.setTTL(8000);// 执行IndexResponse response = indexRequestBuilder.get();return response.isCreated();}
测试,下面代码存储卡卡信息到索引为football-index,类型为football-type,id为3的文档中:
@Testpublic void indexDocByBean() throws Exception {String index = "football-index";String type = "football-type";String id = "3";FootballPlayer footballPlayer = new FootballPlayer();footballPlayer.setName("卡卡");footballPlayer.setClub("奥兰多城俱乐部");footballPlayer.setCountry("巴西");boolean result = IndexDocAPI.indexDocByBean(client, index, type , id, footballPlayer);logger.info("--------- indexDocByBean {}", result);}
4. XContentBuilder帮助类方式
ElasticSearch提供了一个内置的帮助类XContentBuilder来产生JSON文档
/*** 使用帮助类XContentBuilder 产生JSON 索引文档* @param client* @param index* @param type* @param id* @param xContentBuilder* @return*/public static boolean indexDocByXContentBuilder(Client client, String index, String type, String id, XContentBuilder xContentBuilder) {// IndexIndexRequestBuilder indexRequestBuilder = client.prepareIndex();indexRequestBuilder.setIndex(index);indexRequestBuilder.setType(type);indexRequestBuilder.setId(id);indexRequestBuilder.setSource(xContentBuilder);indexRequestBuilder.setTTL(8000);// 执行IndexResponse response = indexRequestBuilder.get();return response.isCreated();}
测试,下面代码存储托雷斯信息到索引为football-index,类型为football-type,id为4的文档中:
@Testpublic void indexDocByXContentBuilder() throws Exception {String index = "football-index";String type = "football-type";String id = "4";XContentBuilder xContentBuilder;try {xContentBuilder = XContentFactory.jsonBuilder();xContentBuilder.startObject().field("name", "托雷斯").field("club", "马德里竞技俱乐部").field("country", "西班牙").endObject();} catch (IOException e) {logger.error("----------indexDocByXContentBuilder create xContentBuilder failed", e);return;}boolean result = IndexDocAPI.indexDocByXContentBuilder(client, index, type, id, xContentBuilder);logger.info("--------- indexDocByXContentBuilder result {}", result);}
你还可以通过startArray(string)和endArray()方法添加数组。.field()方法可以接受多种对象类型。你可以给它传递数字、日期、甚至其他XContentBuilder对象。