Update by query with a search template for the query

In trying to keep the update by query simple, I would like to use a search template for the query part. Updates are being made to matching nested objects so the script & query pieces are verbose like:

{
        "script": {
                "inline" : "for (int i = 0; i < ctx._source['Items'].length; ++i) { if (ctx._source['Items'][i].id == params.id) { for (entry in params.changes.entrySet()) { ctx._source['Items'][i][entry.getKey()] = entry.getValue() } } }",
                "lang" : "painless",
                "params": {
                        "id": "a7f168ce207080641b87098708433c4b",
                         "changes" : {
                        "Key0": "val00",
                        "Key1": "val01",
                        "Key2": "val02",
                        "Key3": "val03",
                        }
                }
        },
        "query": {
                "bool": {
                        "filter": {
                                "nested": {
                                        "path": "Items",
                                        "query": {
                                                "bool": {
                                                        "filter": [{
                                                                "script" : {
                                                                        "script" : {
                                                                                "inline": "doc['Items.id'].value == params.id",
                                                                                "lang": "painless",
                                                                                "params": {
                                                                                        "id": "a7f168ce207080641b87098708433c4b"
                                                                                }
                                                                        }
                                                                }
                                                        }]
                                                }
                                        }
                                }
                        }
                }
        }
}

There are 3 ways this can hopefully be simplified

  • first replace the inline script with a file. Straightforward.
  • second, replace the query part to engage a search template. ???
  • third, the 'params' object is being specified twice once in the update part and again in the query part, Can we just use one 'params' object? ???

Any ideas on 2 & 3 ?

I guess we can do this in two hits to ES.

(1) Create a mustache search template for the query part, and hit the _render api first to 'generate' the query. This 'hides' the query part from the end user. They would simply call the _render api with some data like id here to have the query generated for them.

Move the inline script to a file, and the have the user build 'script' part as it is essentially their data - no query dsl needed.

(2) User combines the 'script' & 'query' pieces and hit the _update_by_query api

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.