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*/publicstaticbooleanindexDocByJSON(Clientclient,Stringindex,Stringtype,Stringid,Stringjson){// IndexIndexRequestBuilderindexRequestBuilder=client.prepareIndex();indexRequestBuilder.setIndex(index);indexRequestBuilder.setType(type);indexRequestBuilder.setId(id);indexRequestBuilder.setSource(json);indexRequestBuilder.setTTL(8000);// 执行IndexResponseindexResponse=indexRequestBuilder.get();returnindexResponse.isCreated();}
测试,下面代码存储梅西信息到索引为football-index,类型为football-type,id为1的文档中:
@TestpublicvoidindexDocByJSON()throwsException{Stringindex="football-index";Stringtype="football-type";Stringid="1";Stringjson="{"+""club":"巴萨罗那","+""country":"阿根廷","+""name":"梅西""+"}";booleanresult=IndexDocAPI.indexDocByJSON(client,index,type,id,json);logger.info("--------- indexDocByJSON {}",result);}
2. Map方式
/*** 使用Map 产生JSON 索引文档* @param client* @param index* @param type* @param id*/publicstaticbooleanindexDocByMap(Clientclient,Stringindex,Stringtype,Stringid,Mapmap){// IndexIndexRequestBuilderindexRequestBuilder=client.prepareIndex();indexRequestBuilder.setIndex(index);indexRequestBuilder.setType(type);indexRequestBuilder.setId(id);indexRequestBuilder.setSource(map);indexRequestBuilder.setTTL(8000);// 执行IndexResponseindexResponse=indexRequestBuilder.get();returnindexResponse.isCreated();}
测试,下面代码存储穆勒信息到索引为football-index,类型为football-type,id为2的文档中:
@TestpublicvoidindexDocByMap()throwsException{Stringindex="football-index";Stringtype="football-type";Stringid="2";Mapmap=Maps.newHashMap();map.put("name","穆勒");map.put("club","拜仁慕尼黑俱乐部");map.put("country","德国");booleanresult=IndexDocAPI.indexDocByMap(client,index,type,id,map);logger.info("--------- indexDocByMap {}",result);}
3. 序列化方式
/*** 利用Json序列化 产生JSON 索引文档** @param client* @param index* @param type* @param id*/publicstaticbooleanindexDocByBean(Clientclient,Stringindex,Stringtype,Stringid,Objectbean){// Bean转换为字节ObjectMappermapper=newObjectMapper();byte[]json;try{json=mapper.writeValueAsBytes(bean);}catch(JsonProcessingExceptione){logger.error("---------- json 转换失败 Bean:{}",bean.toString());returnfalse;}// IndexIndexRequestBuilderindexRequestBuilder=client.prepareIndex();indexRequestBuilder.setIndex(index);indexRequestBuilder.setType(type);indexRequestBuilder.setId(id);indexRequestBuilder.setSource(json);indexRequestBuilder.setTTL(8000);// 执行IndexResponseresponse=indexRequestBuilder.get();returnresponse.isCreated();}
测试,下面代码存储卡卡信息到索引为football-index,类型为football-type,id为3的文档中:
@TestpublicvoidindexDocByBean()throwsException{Stringindex="football-index";Stringtype="football-type";Stringid="3";FootballPlayerfootballPlayer=newFootballPlayer();footballPlayer.setName("卡卡");footballPlayer.setClub("奥兰多城俱乐部");footballPlayer.setCountry("巴西");booleanresult=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*/publicstaticbooleanindexDocByXContentBuilder(Clientclient,Stringindex,Stringtype,Stringid,XContentBuilderxContentBuilder){// IndexIndexRequestBuilderindexRequestBuilder=client.prepareIndex();indexRequestBuilder.setIndex(index);indexRequestBuilder.setType(type);indexRequestBuilder.setId(id);indexRequestBuilder.setSource(xContentBuilder);indexRequestBuilder.setTTL(8000);// 执行IndexResponseresponse=indexRequestBuilder.get();returnresponse.isCreated();}
测试,下面代码存储托雷斯信息到索引为football-index,类型为football-type,id为4的文档中:
@TestpublicvoidindexDocByXContentBuilder()throwsException{Stringindex="football-index";Stringtype="football-type";Stringid="4";XContentBuilderxContentBuilder;try{xContentBuilder=XContentFactory.jsonBuilder();xContentBuilder.startObject().field("name","托雷斯").field("club","马德里竞技俱乐部").field("country","西班牙").endObject();}catch(IOExceptione){logger.error("----------indexDocByXContentBuilder create xContentBuilder failed",e);return;}booleanresult=IndexDocAPI.indexDocByXContentBuilder(client,index,type,id,xContentBuilder);logger.info("--------- indexDocByXContentBuilder result {}",result);}
备注:
你还可以通过startArray(string)和endArray()方法添加数组。.field()方法可以接受多种对象类型。你可以给它传递数字、日期、甚至其他XContentBuilder对象。