使用 Java 在 Elasticsearch 中按查询更新 [英] Update By Query in Elasticsearch using Java
问题描述
我目前使用的是 Elasticsearch V2.3.1.我想在 Java 中使用以下 Elasticsearch 查询.
I’m currently using Elasticsearch V2.3.1. I want to use the following Elasticsearch query in Java.
POST /twitter/_update_by_query
{
"script": {
"inline": "ctx._source.List = [‘Item 1’,’Item 2’]"
},
"query": {
"term": {
"user": "kimchy"
}
}
}
上述查询搜索名为kimchy"的user"并使用给定值更新List"字段.此查询同时更新多个文档.我在这里阅读了 Java 的更新 API https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.3/java-docs-update.html 但找不到我要找的东西.Java 的更新 API 只讨论一次更新单个文档.有没有办法更新多个文档?对不起,如果我遗漏了一些明显的东西.感谢您的时间.
The above query searches for "user" named "kimchy" and updates the "List" field with given values. 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. The Update API for Java only talks about updating single document at a time. Is there any way to update multiple documents? Sorry if I’m missing something obvious. Thank you for your time.
更新:
我尝试了以下 Java 代码:
I tried the below Java Code:
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();
因此,我按上述方式编辑了 Java 程序,但 Java 未识别 termQuery.我可以知道我在这里做错了什么吗?谢谢.
So I edited the Java Program as above and the termQuery is not identified by Java. May I know what I'm doing wrong here? Thanks.
推荐答案
从 ES 2.3 开始,按查询更新功能可用作 REST 端点 _update_by_query
,但不适用于 Java 客户端.为了从您的 Java 客户端代码调用此端点,您需要在 pom.xml 中包含 reindex
模块,如下所示
As of ES 2.3, the update by query feature is available as the REST endpoint _update_by_query
but nor for Java clients. In order to call this endpoint from your Java client code, you need to include the reindex
module in your pom.xml, like this
<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();
更新
如果您需要指定更新应关注的类型,您可以这样做:
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();
这篇关于使用 Java 在 Elasticsearch 中按查询更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!