How to execute BULK UpdateByQuery using java api - Elasticsearch 5.x

Viewed 491

I have multiple updateByQuery requests, and looking for a way to group them together, and then execute (for performance reasons).


Example:

QueryByField || field11111 || field222222 || field333333 ||

xxxxxxxxxxxx || newValue || newValueee || newValuee ||

yyyyyyyyyyyy || newValue || newValueee || newValuee ||

zzzzzzzzzzzz || newValue || newValueee || newValuee ||


As of now, seems like I'll have to do the following query for each one of lines above:

UpdateByQueryRequestBuilder updateByQueryRequestBuilder = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
updateByQueryRequestBuilder
    .source("myIndexName")
    .filter(QueryBuilders.matchQuery("QueryByField","xxxxxxxxxxxx"))
    .script(new Script("ctx._source.field11111 = \"newValue\""  , ScriptService.ScriptType.INLINE, null, null))
    .get();

But I was wondering, is there something similar to what we do with UpdateRequest & BulkRequestBuilder that can be used for UpdateByQuery?

1 Answers

One solution is query by any of terms and verify in the script which condition is applied.

        String script = "if(ctx._source.QueryByField != null and ctx._source.QueryByField == \"xxxxxxxxxxxx\") { "
                + " ctx._source.field11111 = \"newValue\";"
                + " ctx._source.field222222  = \"newValueee\";"
                + " ctx._source.field333333  = \"newValuee\";"
                + "}"
                + ".. outher conditions";


        UpdateByQueryRequestBuilder requestx = UpdateByQueryAction.INSTANCE.newRequestBuilder(elastic)
                .source("myIndexName")
                .filter(QueryBuilders.termsQuery("QueryByField ", "xxxxxxxxxxxx", "yyyyyyyyyyyy", "zzzzzzzzzzzz"))
                .script(new Script(script, ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, null));
Related