Index API 允许我们存储一个JSON格式的文档,使数据可以被搜索。文档通过index、type、id唯一确定。我们可以自己提供一个id,或者也使用Index API 为我们自动生成一个。
这里有几种不同的方式来产生JSON格式的文档(document):
// Index
IndexRequestBuilder indexRequestBuilder = client.prepareIndex();
indexRequestBuilder.setIndex(index);
indexRequestBuilder.setType(type);
indexRequestBuilder.setId(id);
indexRequestBuilder.setSource(json);
indexRequestBuilder.setTTL(8000);
// 执行
IndexResponse indexResponse = indexRequestBuilder.get();
测试,下面代码存储梅西信息到索引为football-index,类型为football-type,id为1的文档中:
String index = "football-index";
String type = "football-type";
String id = "1";
String json = "{" +
"\"club\":\"巴萨罗那\"," +
"\"country\":\"阿根廷\"," +
"\"name\":\"梅西\"" +
"}";
// Index
IndexRequestBuilder indexRequestBuilder = client.prepareIndex();
indexRequestBuilder.setIndex(index);
indexRequestBuilder.setType(type);
indexRequestBuilder.setId(id);
indexRequestBuilder.setSource(map);
indexRequestBuilder.setTTL(8000);
// 执行
IndexResponse indexResponse = indexRequestBuilder.get();
测试,下面代码存储穆勒信息到索引为football-index,类型为football-type,id为2的文档中:
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", "德国");
// Bean转换为字节
ObjectMapper mapper = new ObjectMapper();
byte[] json;
try {
json = mapper.writeValueAsBytes(bean);
} catch (JsonProcessingException e) {
logger.error("---------- json 转换失败 Bean:{}", bean.toString());
return false;
}
// Index
IndexRequestBuilder indexRequestBuilder = client.prepareIndex();
indexRequestBuilder.setIndex(index);
indexRequestBuilder.setType(type);
indexRequestBuilder.setId(id);
indexRequestBuilder.setSource(json);
indexRequestBuilder.setTTL(8000);
// 执行
IndexResponse response = indexRequestBuilder.get();
测试,下面代码存储卡卡信息到索引为football-index,类型为football-type,id为3的文档中:
String index = "football-index";
String type = "football-type";
String id = "3";
FootballPlayer footballPlayer = new FootballPlayer();
footballPlayer.setName("卡卡");
footballPlayer.setClub("奥兰多城俱乐部");
footballPlayer.setCountry("巴西");
ElasticSearch提供了一个内置的帮助类XContentBuilder来产生JSON文档
// Index
IndexRequestBuilder indexRequestBuilder = client.prepareIndex();
indexRequestBuilder.setIndex(index);
indexRequestBuilder.setType(type);
indexRequestBuilder.setId(id);
indexRequestBuilder.setSource(xContentBuilder);
indexRequestBuilder.setTTL(8000);
// 执行
IndexResponse response = indexRequestBuilder.get();
测试,下面代码存储托雷斯信息到索引为football-index,类型为football-type,id为4的文档中:
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;
}
备注:
你还可以通过startArray(string)和endArray()方法添加数组。.field()方法可以接受多种对象类型。你可以给它传递数字、日期、甚至其他XContentBuilder对象。
ElasticSearch版本:2.x