Is there any way to update multiple fields by update_by_query


(tanimoto) #1

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

(Daniel Mitterdorfer) #2

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


(tanimoto) #3

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.


(Daniel Mitterdorfer) #4

Hi @tanimoto,

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

Daniel


(tanimoto) #5

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.


(Jun Ohtani) #6

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


(tanimoto) #7

It worked!

Seems I didn't quite understand Painless syntax.

Thanks anyway!


(system) #8

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