# Update by query with a search template for the query

**URL:** <https://discuss.elastic.co/t/update-by-query-with-a-search-template-for-the-query/70310>\
**Category:** Elasticsearch\
**Created:** [December 31, 2016, 3:41am UTC](https://discuss.elastic.co/t/update-by-query-with-a-search-template-for-the-query/70310 "2016-12-31T03:41:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashokc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashokc/32/11892_2.png) [@ashokc](https://discuss.elastic.co/u/ashokc)\
**Post date:** [December 31, 2016, 3:41am UTC](https://discuss.elastic.co/t/update-by-query-with-a-search-template-for-the-query/70310/1 "2016-12-31T03:41:08Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![ashokc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashokc/32/11892_2.png) [@ashokc](https://discuss.elastic.co/u/ashokc)\
**Post date:** [January 3, 2017, 11:08pm UTC](https://discuss.elastic.co/t/update-by-query-with-a-search-template-for-the-query/70310/2 "2017-01-03T23:08:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 31, 2017, 11:08pm UTC](https://discuss.elastic.co/t/update-by-query-with-a-search-template-for-the-query/70310/3 "2017-01-31T23:08:37Z")

</div>

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