如何使用 Java 中的 API Update(版本 5)更新 ElasticSearch 中的哈希图数据?
2019-10-31
2195
我正在尝试使用以下代码更新 Elastic Search Server 中的数据。
1) 我已使用以下内容将数据插入弹性搜索中。
HashMap<String, String> hmExtra = new HashMap<String, String>();
hmExtra.put("One", "1");
hmExtra.put("Two", "2");
HashMap<String, String> data = new HashMap<String, String>();
data.put("case_name","Test");
data.put("case_extra",hmExtra);
IndexRequest indexRequest = new IndexRequest("org-8").type("Group").source(data);
IndexResponse response = client.index(indexRequest, header);
当我使用键查询 Elastic 搜索时,返回以下结果:
{
"_index": "org-8",
"_type": "Group",
"_id": "AW4gTZwCAdqCPewR2ukW",
"_version": 2,
"found": true,
"_source": {
"case_name","Test",
"case_extra": {
"One": "1",
"Two": "2"
}
}
}
插入数据(HashMap)后,它成功插入到搜索中。
2) 尝试更新相同内容。
之后,当我尝试使用以下代码更新 case_extra 时。搜索查询显示现有值。
HashMap<String, String> hmExtra = new HashMap<String, String>();
hmExtra.put("One", "9");
HashMap<String, String> data = new HashMap<String, String>();
data.put("case_name","Test2");
data.put("case_extra",hmExtra);
UpdateRequest request = new UpdateRequest("org-8", "Group", "AW4gTZwCAdqCPewR2ukW").doc(data);
UpdateResponse updateResponse = client.update(request);
结果:
{
"_index": "org-8",
"_type": "Group",
"_id": "AW4gTZwCAdqCPewR2ukW",
"_version": 2,
"found": true,
"_source": {
"case_name","Test2",
"case_extra": {
"One": "9",
"Two": "2"
}
}
}
根据以上结果,键“Two”未从 case_extra 中删除。
我需要帮助在 elasticsearch 中使用 Api Update,我尝试了上述代码,问题是从 case_extra 中删除了 HashMap 的第二个值。
提前致谢。
更新
这是我尝试过的代码。
在更新请求之前,文档如下所示:
{
"_index": "org-8",
"_type": "Group",
"_id": "AW4cWb2g90EOmg5E-TFl",
"_version": 22,
"found": true,
"_source": {
"case_name": "Test1",
"case_extra": {
"One": "1",
"Two": "2"
}
}
}
更新请求代码与 Garvit Khamesra 在他的回答中给出的相同:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200,"http")));
HashMap<String, String> hmExtra1 = new HashMap<String, String>();
hmExtra1.put("One", "9");
Map<String, Object> data1 = new HashMap<>();
data1.put("case_extra",hmExtra1);
data1.put("case_name", "Test2");
Map<String, Object> parameters = Collections.singletonMap("val", data1);
UpdateRequest request = new UpdateRequest("org-8", "Group", "AW4cWb2g90EOmg5E-TFl");
Script script = new Script(ScriptType.INLINE, "painless",
"ctx._source = params.val", parameters);
request.script(script);
try {
UpdateResponse updateResponse = client.update(request);
System.out.println(updateResponse.getGetResult().sourceAsString());
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
运行上述代码后,我收到的错误如下:
ElasticsearchStatusException[Elasticsearch exception [type=illegal_argument_exception, reason=[script] unknown field [source], parser not found]]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:618)
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:594)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:501)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:474)
at org.elasticsearch.client.RestHighLevelClient.update(RestHighLevelClient.java:353)
at com.goclockwork.ontracksearchengine.test.UpdateSearchTest.main(UpdateSearchTest.java:54)
Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/org-8/Group/AW4cWb2g90EOmg5E-TFl/_update?timeout=1m], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"[script] unknown field [source], parser not found"}],"type":"illegal_argument_exception","reason":"[script] unknown field [source], parser not found"},"status":400}
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:357)
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:346)
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
at java.lang.Thread.run(Unknown Source)
我正在使用的依赖项如下:
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
</dependencies>
2个回答
你尝试的是弹性搜索的部分更新方法。我们需要使用脚本更新。
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200,"http")));
HashMap<String, String> hmExtra1 = new HashMap<String, String>();
hmExtra1.put("One", "9");
Map<String, HashMap<String, String>> data1 = new HashMap<>();
data1.put("case_extra",hmExtra1);
Map<String, Object> parameters = singletonMap("val", data1);
UpdateRequest request = new UpdateRequest("org-8", "Group", "CBCyJm4BWKXF8n_XGsze");
Script script = new Script(ScriptType.INLINE, "painless",
"ctx._source = params.val", parameters);
request.script(script);
UpdateResponse updateResponse = client.update(request);
现在文档将变为
{"_index":"org-8","_type":"Group","_id":"CBCyJm4BWKXF8n_XGsze","_version":5,"found":true,"_source":{"case_extra":{"One":"9"}}}
Garvit Khamesra
2019-10-31
在 ES 7.9 中,使用 UpdateByQueryRequest 也可以实现此效果
Map<String, Object> map = new HashMap<String, Object>();
UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest("indexName"); updateByQueryRequest.setConflicts("proceed"); updateByQueryRequest.setQuery(new TermQueryBuilder("_id", documentId)); Script script = new Script(ScriptType.INLINE, "painless", "ctx._source = params", map); updateByQueryRequest.setScript(script);
Usman
2020-09-14