# Update by query with params

**URL:** <https://discuss.elastic.co/t/update-by-query-with-params/239413>\
**Category:** Elasticsearch\
**Created:** [July 1, 2020, 6:50am UTC](https://discuss.elastic.co/t/update-by-query-with-params/239413 "2020-07-01T06:50:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AsfK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asfk/32/58995_2.png) [@AsfK](https://discuss.elastic.co/u/AsfK)\
**Post date:** [July 1, 2020, 6:50am UTC](https://discuss.elastic.co/t/update-by-query-with-params/239413/1 "2020-07-01T06:50:35Z")

</div>

[ESK 7.6.2]

We're set country name by phone prefix. if for some reason we hanve't the prefix we're putting "Missing Country Name" string.

I built a simple update by query in Kiban dev-tool and it's worked, now I want to upgrade the script by adding variables:

```
POST mydata/_update_by_query
{
"script" : {
    "params": {
      "phonePrefix" : 31,
      "newCountryName" : "Netherlands"
    },
    "source": "ctx._source.countryName = params.newCountryName"
  },
  "query" : {
    "bool" : { 
      "must" : [
        { 
          "match": {
            "countryName.keyword": "Missing Country Name"
          }
        },
        { 
          "prefix": {
            "internationalPrefix": {
              "value": "params.phonePrefix"
            }
          }
        }
      ]
    }
  }
}

```

But it's not working with params (not find documents to update)

Can you help me please? Thank you!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 1, 2020, 1:54pm UTC](https://discuss.elastic.co/t/update-by-query-with-params/239413/2 "2020-07-01T13:54:17Z")

</div>

Please provide a **full reproducible** example including index creation, sample document indexing and reindexing request.

My assumption here is, that your query does not return any results because you are searching for `params.phonePrefix` as field content in `internationalPrefix`, try using `31` hardcoded in there, the parameters are only valid within your script.

---

<div class="post-metadata">

**Author:** ![AsfK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asfk/32/58995_2.png) [@AsfK](https://discuss.elastic.co/u/AsfK)\
**Post date:** [July 1, 2020, 2:19pm UTC](https://discuss.elastic.co/t/update-by-query-with-params/239413/3 "2020-07-01T14:19:56Z")

</div>

Thank you Alexander.

index is created via code, but both fields are of text type:

```
 "countryName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }

```

Without params all works fine, like this:

```
POST cdrdata*/_update_by_query
{
  "script" : {
    "source": "ctx._source.countryName = 'Netherlands'"
  },
  "query" : {
    "bool" : { 
      "must" : [
        { 
          "match": {
            "countryName.keyword": "Missing Country Name"
          }
        },
        { 
          "prefix": {
            "internationalPrefix": {
              "value": 31
            }
          }
        }
      ]
    }
  }
}

```

But I want to use variables in the source and in the query (a friend from the support should use it and I'll be glad to make it easy as I can to him)

Thank you!

---

<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:** [July 29, 2020, 2:22pm UTC](https://discuss.elastic.co/t/update-by-query-with-params/239413/4 "2020-07-29T14:22:24Z")

</div>

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