使用Java在Elasticsearch中按查询更新

我目前正在使用Elasticsearch V2.3.1。 我想在Java中使用以下Elasticsearch查询。

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的Update API仅讨论一次更新单个文档。 有没有办法更新多个文件? 对不起,如果我错过了一些明显的东西。 感谢您的时间。

更新:

我尝试了下面的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(); 

所以我编辑了如上所述的Java程序,并且Java不识别termQuery。 我可以知道我在这里做错了吗? 谢谢。

从ES 2.3开始,按查询function更新可用作REST端点_update_by_query但也不用于Java客户端。 要从Java客户端代码调用此端点,您需要在pom.xml中包含reindex模块,如下所示

  org.elasticsearch.module reindex 2.3.2  

然后,您需要在构建客户端时包含此模块:

 clientBuilder.addPlugin(ReindexPlugin.class); 

最后你可以像这样调用它:

 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

如果您需要指定更新应关注的类型,您可以这样做:

 ubqrb.source("twitter").source().setTypes("type1"); BulkIndexByScrollResponse r = ubqrb.script(script) .filter(termQuery("user", "kimchy")) .get();