Is there any way to update multiple fields by update_by_query

can I update multiple fields using update_by_query at once something like this?

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"
      }
  }

Hi @tanimoto,

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

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

4 Likes

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.

Hi @tanimoto,

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

Daniel

When I execute this query

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

{
  "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.

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

3 Likes

It worked!

Seems I didn't quite understand Painless syntax.

Thanks anyway!

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