我正在尝试使用以下代码更新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>
答案 0 :(得分:0)
您要尝试的是弹性搜索的部分更新方法。我们需要使用脚本更新。
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"}}}
答案 1 :(得分:0)
在ES 7.9中,这也可以使用UpdateByQueryRequest
Map
map = new HashMap (); 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);