版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/SunnyYoona/article/details/52806082
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) {
-
// Index
-
IndexRequestBuilder 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的文檔中:
-
@Test
-
public 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) {
-
// Index
-
IndexRequestBuilder 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的文檔中:
-
@Test
-
public 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;
-
}
-
// Index
-
IndexRequestBuilder 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的文檔中:
-
@Test
-
public 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) {
-
// Index
-
IndexRequestBuilder 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的文檔中:
-
@Test
-
public 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對象。
參考:
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html#java-docs-index-generate