我正在尝试使用以下代码更新 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);
当我使用该键查询弹性搜索时,得到以下结果:
{
"_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"
}
}
}
根据上述结果,没有从 case_extra 中删除键“Two”。
我需要帮助才能在 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>