# Is there any way to update multiple fields by update\_by\_query

**URL:** https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644
**Category:** Elasticsearch
**Created:** [January 5, 2017, 7:57am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644 "2017-01-05T07:57:06Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![tanimoto](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@tanimoto](https://discuss.elastic.co/u/tanimoto)
#### Post date: [January 5, 2017, 7:57am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644/1 "2017-01-05T07:57:07Z")

</div>

can I update multiple fields using update\_by\_query at once something like this?

```auto
POST /e-trend-web/items/_update_by_query
{
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "_id" : [1138081, 1138083, 1138089, 123456] 
        }
      }
    }
  },
    "script" : {
      "inline" : "ctx._source.item_name= new_name;", ⬅︎ like this
      "inline" : "ctx._source.item_price= 10000;", ⬅︎ like this
      "lang" : "groovy"
      }
  }

```

---

<div class="post-metadata">

### Author: ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)
#### Post date: [January 5, 2017, 10:18am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644/2 "2017-01-05T10:18:46Z")

</div>

Hi @tanimoto,

sure, just specify all statements in one script, i.e.:

```auto
POST /e-trend-web/items/_update_by_query
{
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "_id" : [1138081, 1138083, 1138089, 123456] 
        }
      }
    }
  },
    "script" : {
      "inline" : "ctx._source.item_name= 'new_name'; ctx._source.item_price= 10000;", 
      "lang" : "painless"
      }
  }

```

Note that usage of Groovy is discouraged for security reasons so I changed the scripting language to Painless (which has the same syntax for your example).

Daniel

---

<div class="post-metadata">

### Author: ![tanimoto](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@tanimoto](https://discuss.elastic.co/u/tanimoto)
#### Post date: [January 5, 2017, 10:34am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644/3 "2017-01-05T10:34:30Z")

</div>

Thanks. It worked!!

About language, painless seems not supporting japanese language.  
And I need to use Japanese language so I explicitly declare groovy instead of painless.

Maybe I need to create another issue for this...

thanks anyway.

---

<div class="post-metadata">

### Author: ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)
#### Post date: [January 5, 2017, 10:41am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644/4 "2017-01-05T10:41:07Z")

</div>

Hi @tanimoto,

> [@tanimoto](#):
>
> About language, painless seems not supporting japanese language.

This isn't good and we should look into this. Can you please provide an example of what is not working?

Daniel

---

<div class="post-metadata">

### Author: ![tanimoto](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@tanimoto](https://discuss.elastic.co/u/tanimoto)
#### Post date: [January 6, 2017, 1:39am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644/5 "2017-01-06T01:39:28Z")

</div>

When I execute this query

```auto
POST /index/type/_update_by_query
{
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "_id" : [1138081, 1138083, 1138089, 123456] 
        }
      }
    }
  },
    "script" : {
      "inline" : "ctx._source.商品名称= 'new_name'; ctx._source.item_price= 10000;", 
      "lang" : "painless"
      }
}

```

this error happens

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "ctx._source.商品名称= 'new_name'; ctx._so ...",
          " ^---- HERE"
        ],
        "script": "ctx._source.商品名称= 'new_name'; ctx._source.item_price= 10000;",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "unexpected character [商].",
      "caused_by": {
        "type": "lexer_no_viable_alt_exception",
        "reason": null
      }
    },
    "script_stack": [
      "ctx._source.商品名称= 'new_name'; ctx._so ...",
      " ^---- HERE"
    ],
    "script": "ctx._source.商品名称= 'new_name'; ctx._source.item_price= 10000;",
    "lang": "painless"
  },
  "status": 500
}

```

If I use Groovy, this error doesn't happen with same query.

---

<div class="post-metadata">

### Author: ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)
#### Post date: [January 6, 2017, 2:04am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644/6 "2017-01-06T02:04:36Z")

</div>

Could you use `ctx._source['商品名称']` instead of `ctx._source.商品名称`?

---

<div class="post-metadata">

### Author: ![tanimoto](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@tanimoto](https://discuss.elastic.co/u/tanimoto)
#### Post date: [January 6, 2017, 2:08am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644/7 "2017-01-06T02:08:55Z")

</div>

It worked!

Seems I didn't quite understand Painless syntax.

Thanks anyway!

---

<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: [February 3, 2017, 2:09am UTC](https://discuss.elastic.co/t/is-there-any-way-to-update-multiple-fields-by-update-by-query/70644/8 "2017-02-03T02:09:31Z")

</div>

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