使用 Java 在 Elasticsearch 中按查询更新
Update By Query in Elasticsearch using Java
我目前使用的是 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”的“用户”,并使用给定值更新“列表”字段。此查询同时更新多个文档。
我在这里 https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.3/java-docs-update.html 阅读了 Java 的更新 API,但找不到我要找的东西。 Update API for Java 只讨论一次更新单个文档。有没有办法更新多个文档?对不起,如果我遗漏了一些明显的东西。感谢您的时间。
更新:
我尝试了下面的 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 开始,查询更新功能可作为 REST 端点 _update_by_query
使用,但对 Java 客户端也不可用。为了从您的 Java 客户端代码调用此端点,您需要在 pom.xml 中包含 reindex
模块,如下所示
<dependency>
<groupId>org.elasticsearch.module</groupId>
<artifactId>reindex</artifactId>
<version>2.3.2</version>
</dependency>
那么您需要在构建客户端时包含此模块:
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();
更新
如果您需要指定更新应关注的类型,您可以这样做:
ubqrb.source("twitter").source().setTypes("type1");
BulkIndexByScrollResponse r = ubqrb.script(script)
.filter(termQuery("user", "kimchy"))
.get();
在 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);
我目前使用的是 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”的“用户”,并使用给定值更新“列表”字段。此查询同时更新多个文档。 我在这里 https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.3/java-docs-update.html 阅读了 Java 的更新 API,但找不到我要找的东西。 Update API for Java 只讨论一次更新单个文档。有没有办法更新多个文档?对不起,如果我遗漏了一些明显的东西。感谢您的时间。
更新:
我尝试了下面的 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 开始,查询更新功能可作为 REST 端点 _update_by_query
使用,但对 Java 客户端也不可用。为了从您的 Java 客户端代码调用此端点,您需要在 pom.xml 中包含 reindex
模块,如下所示
<dependency>
<groupId>org.elasticsearch.module</groupId>
<artifactId>reindex</artifactId>
<version>2.3.2</version>
</dependency>
那么您需要在构建客户端时包含此模块:
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();
更新
如果您需要指定更新应关注的类型,您可以这样做:
ubqrb.source("twitter").source().setTypes("type1");
BulkIndexByScrollResponse r = ubqrb.script(script)
.filter(termQuery("user", "kimchy"))
.get();
在 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);