[英]Update By Query in Elasticsearch using Java
I'm currently using Elasticsearch V2.3.1.我目前使用的是 Elasticsearch V2.3.1。 I want to use the following Elasticsearch query in Java.我想在 Java 中使用以下 Elasticsearch 查询。
POST /twitter/_update_by_query
{
"script": {
"inline": "ctx._source.List = [‘Item 1’,’Item 2’]”
},
"query": {
"term": {
"user": "kimchy"
}
}
}
The above query searches for “user” named “kimchy” and updates the “List” field with given values.上述查询搜索名为“kimchy”的“user”并使用给定值更新“List”字段。 This query updates multiple documents at the same time.此查询同时更新多个文档。 I read about the Update API for Java here https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.3/java-docs-update.html but couldn't find what I was looking for.我在这里阅读了 Java 的更新 API https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.3/java-docs-update.html但找不到我要找的东西. The Update API for Java only talks about updating single document at a time. Java 的更新 API 只讨论一次更新单个文档。 Is there any way to update multiple documents?有没有办法更新多个文档? Sorry if I'm missing something obvious.对不起,如果我遗漏了一些明显的东西。 Thank you for your time.谢谢你的时间。
Update:更新:
I tried the below Java Code:我尝试了以下 Java 代码:
Client client = TransportClient.builder().addPlugin(ReindexPlugin.class)
.build().addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9300));
UpdateByQueryRequestBuilder ubqrb = UpdateByQueryAction.INSTANCE
.newRequestBuilder(client);
Script script = new Script("ctx._source.List = [\"Item 1\",\"Item 2\"]");
//termQuery is not recognised by the program
BulkIndexByScrollResponse r = ubqrb.source("twitter").script(script)
.filter(termQuery("user", "kimchy")).execute().get();
So I edited the Java Program as above and the termQuery is not identified by Java.所以我编辑了上面的 Java 程序,并且术语查询不是由 Java 识别的。 May I know what I'm doing wrong here?我可以知道我在这里做错了什么吗? Thanks.谢谢。
As of ES 2.3, the update by query feature is available as the REST endpoint _update_by_query
but nor for Java clients.从 ES 2.3 开始,按查询更新功能可用作 REST 端点_update_by_query
但也不适用于 Java 客户端。 In order to call this endpoint from your Java client code, you need to include the reindex
module in your pom.xml, like this为了从您的 Java 客户端代码调用此端点,您需要在 pom.xml 中包含reindex
模块,如下所示
<dependency>
<groupId>org.elasticsearch.module</groupId>
<artifactId>reindex</artifactId>
<version>2.3.2</version>
</dependency>
Then you need to include this module when building your client:然后你需要在构建客户端时包含这个模块:
clientBuilder.addPlugin(ReindexPlugin.class);
Finally you can call it like this:最后你可以这样称呼它:
UpdateByQueryRequestBuilder ubqrb = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
Script script = new Script("ctx._source.List = [\"Item 1\",\"Item 2\"]");
BulkIndexByScrollResponse r = ubqrb.source("twitter")
.script(script)
.filter(termQuery("user", "kimchy"))
.get();
UPDATE更新
If you need to specify the type(s) the update should focus on, you can do so:如果您需要指定更新应关注的类型,您可以这样做:
ubqrb.source("twitter").source().setTypes("type1");
BulkIndexByScrollResponse r = ubqrb.script(script)
.filter(termQuery("user", "kimchy"))
.get();
In ES 7.9 this also works using UpdateByQueryRequest在 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.