简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM