使用Java更新在Elasticsearch中的查询 [英] Update By Query in Elasticsearch using Java

查看:274
本文介绍了使用Java更新在Elasticsearch中的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  POST / twitter / _update_by_query 
{
script:{
inline新评新新新新新旗新新新新旗新新新新旗新新旗新新旗200新新新新新旗新新旗200新新新新新新新新新新新旗新新旗新新旗新新新新新新新旗新新旗新新旗新新新新新新新旗新新新旗新新新新新新新新新旗新新新旗新新旗新新新新新新新旗新新新旗新新新旗新新新旗新新新新新新新旗新新新新新旗新新新旗新新新新新旗新新新旗新新新新新旗新新新新新旗新新新新新旗新新新旗新新新旗新新新新新旗新新新新新旗新新旗新新新旗新新旗新新新新新新新旗新新旗新新旗新旗新:kimchy
}
}
}

以上查询搜索名为kimchy的user,并使用给定值更新List字段,此查询同时更新多个文档
我在这里阅读了关于Java的Update API, https://www.elastic.co/guide/ en / elasticsearch / client / java-api / 2.3 / java-docs-update.html ,但找不到我正在寻找的内容,Java API更新API只讨论一次更新单个文档。有什么办法更新多个文件?对不起,如果我缺少一些明显的东西,谢谢你的时间。



更新:



我试过了以下Java代码:

 客户端客户端= TransportClient.builder()。addPlugin(ReindexPlugin.class)
.build ().addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName(127.0.0.1),9300));新新新新旗新新新新旗新新旗新新新新旗新新旗新新旗新新旗新新旗新新新新旗新新旗新新新旗新新旗新新旗旗新新旗新新新新旗新新旗200新新新新旗新新旗200新新新新旗新新旗旗哨旗新新新新旗新

脚本脚本=新脚本(ctx._source.List = [\项1 \,\\项2 \]);

// termQuery不被程序识别
BulkIndexByScrollResponse r = ubqrb.source(twitter)。script(script)
.filter(termQuery(user kimchy))执行()得到()。

所以我编辑了如上所述的Java程序,而termQuery没有被Java标识。我可以知道我在这里做错了吗?谢谢。

解决方案

从ES 2.3起,查询功能更新可用作REST端点 _update_by_query 而不是Java客户端。 X-454545454545 CEEC X- 20045 X-45454545 X- 20045 CEEC X- $ b

 <依赖关系> 
< groupId> org.elasticsearch.module< / groupId>
< artifactId> reindex< / artifactId>
< version> 2.3.2< / version>
< / dependency>新新旗新新新旗新新新旗新新旗旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新旗新旗新旗新新旗新新旗新新旗新新旗新旗新旗新旗新旗新新旗新新旗新新旗新旗新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新旗新旗新旗新旗新旗新新旗新新旗新新旗新新旗新新旗新旗新旗新旗新新旗新新旗新旗新旗新新新新旗新新旗新新旗新旗新旗新旗新新旗新新旗新新旗新旗新旗新旗新旗新新旗新新旗新新旗新新旗新旗新旗新旗新旗新新旗新新旗新新旗新旗新b $ b

  clientBuilder.addPlugin(ReindexPlugin.class);新评新200新200新新新新旗新新新旗新新旗新新旗新新旗200新新新新旗新新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新新旗新新旗新新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新新旗新新旗新旗新旗新旗新旗新旗新新旗新旗新旗新旗新旗新旗新旗新新旗新新旗新新旗新旗新旗旗旗新旗新旗新旗新旗新旗新旗新旗新新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新新旗新新旗新新旗新旗新旗新旗新旗新旗新旗pre>  UpdateByQueryRequestBuilder ubqrb = UpdateByQueryAction.INSTANCE.newRequestBuilder(client); 

脚本脚本=新脚本(ctx._source.List = [\项1 \,\\项2 \]);

BulkIndexByScrollResponse r = ubqrb.source(twitter)
.script(脚本)
.filter(termQuery(user,kimchy))
。得到();新新旗新新新旗新旗旗新新新旗新旗新新旗旗新新旗新旗旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新新旗新旗新旗新旗新旗新新旗新旗新旗新旗新新旗新旗新旗新旗新新旗新旗新旗新旗新新旗新旗新旗新旗新新旗新旗新旗新旗新新旗新旗新旗新旗新旗新新旗新旗新旗新旗新旗新新旗新旗新旗新旗新旗新新旗新旗新旗新旗新旗新新旗新旗新旗新旗新p>如果您需要指定更新应该关注的类型,可以这样做:

  ubqrb.source ( 推)源()setTypes( TYPE1)。。 
BulkIndexByScrollResponse r = ubqrb.script(脚本)
.filter(termQuery(user,kimchy))
.get();


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"
    }
  }
}

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.

Update:

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();

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.

解决方案

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();

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();

这篇关于使用Java更新在Elasticsearch中的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆