Elasticsearch 更新索引文件
Elasticsearch Update indexdocument
我需要为 elasticsearch table 更新索引文档,这是我实现的代码。但是它不起作用,有什么问题,我应该如何实现它?
我的代码。
Map<String, Object> matching_result;
for (SearchHit hit : response_text.getHits()) {
matching_result = hit.getSource();
String flag_value = matching_result.get("flag").toString();
matching_result.put("flag", true);
}
String indexString = JSONConverter.toJsonString(matching_result);
IndexResponse response = client.prepareIndex("index_name", "data").setSource(indexString).execute().actionGet();
boolean created = response.isCreated();
System.out.println("created or updated--------------------->" + created);
System.out.println("flag value==========" + matching_result.get("flag"));
return actual_theme;
(JSONConverter.toJsonString) 是我们的库 class 用于转换为 json 字符串。
这个查询有什么问题?
它不是更新现有文档,而是创建一个新文档。我想改变现有的。
根据您的示例代码,"update" 看起来您的意思是您正在尝试替换整个文档。为此,您必须指定要更新的文档的 id
。
使用 Java API,除了在 IndexRequestBuilder
上调用 setSource
之外,您还需要通过调用 setId
来提供 ID .例如:
IndexResponse response = client.prepareIndex("index_name", "data")
.setSource(indexString)
.setId(123) <----- supply the ID of the document you want to replace
.execute()
.actionGet();
否则,如您所知,在 ES 中您可以选择进行部分更新。即只更新文档中的某些字段。这可以通过脚本或通过提供部分文档来完成。看看 documentation for the Update API.
无论哪种情况,您都需要向 ES 提供您要修改的文档的 ID。
我需要为 elasticsearch table 更新索引文档,这是我实现的代码。但是它不起作用,有什么问题,我应该如何实现它?
我的代码。
Map<String, Object> matching_result;
for (SearchHit hit : response_text.getHits()) {
matching_result = hit.getSource();
String flag_value = matching_result.get("flag").toString();
matching_result.put("flag", true);
}
String indexString = JSONConverter.toJsonString(matching_result);
IndexResponse response = client.prepareIndex("index_name", "data").setSource(indexString).execute().actionGet();
boolean created = response.isCreated();
System.out.println("created or updated--------------------->" + created);
System.out.println("flag value==========" + matching_result.get("flag"));
return actual_theme;
(JSONConverter.toJsonString) 是我们的库 class 用于转换为 json 字符串。 这个查询有什么问题? 它不是更新现有文档,而是创建一个新文档。我想改变现有的。
根据您的示例代码,"update" 看起来您的意思是您正在尝试替换整个文档。为此,您必须指定要更新的文档的 id
。
使用 Java API,除了在 IndexRequestBuilder
上调用 setSource
之外,您还需要通过调用 setId
来提供 ID .例如:
IndexResponse response = client.prepareIndex("index_name", "data")
.setSource(indexString)
.setId(123) <----- supply the ID of the document you want to replace
.execute()
.actionGet();
否则,如您所知,在 ES 中您可以选择进行部分更新。即只更新文档中的某些字段。这可以通过脚本或通过提供部分文档来完成。看看 documentation for the Update API.
无论哪种情况,您都需要向 ES 提供您要修改的文档的 ID。