11

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.

2 Answers 2

16

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();
Sign up to request clarification or add additional context in comments.

13 Comments

You need to add import static org.elasticsearch.index.query.QueryBuilders.termQuery;
Hi @Val, it worked but this way(using filter in BulkIndexByScrollResponse) I can only update one document that has a user named "Kimchy". Is it possible to update multiple documents using setQuery in BulkIndexByScrollResponse? or possibly any other way?
Of course, you can craft any kind of query. That was just an example. What's your query?
Hi @Val, Is their a way to set the doc type in this type of query.
Just a hint for those who'd use ES version 5.5.0 or similar: The plugin has to be added as a second parameter in PreBuiltTransportClient's constructor.
|
3

In ES 7.9 this also works using 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.