Elastic update a field with json data

1.8k views Asked by At

POST cars/_update_by_query

{
  "query": {
    "match_all": {}
  },
  "script": {
    "inline": "ctx._source.addresses = [{country:'Country', countryCode : 'cr'}]",
    "lang": "painless"
  }
}

The script run successfully, no error raised, the output is bellow, but nothing gets updated.

{
  "took" : 18092,
  "timed_out" : false,
  "total" : 400000,
  "updated" : 400000,
  "deleted" : 0,
  "batches" : 400,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

Thanks

1

There are 1 answers

6
Val On BEST ANSWER

Your script needs to look like this instead:

"inline": "ctx._source.addresses = [['country':'Country', 'countryCode' : 'cr']]",

Note that the Painless doesn't handle JSON directly, you need to go through Arrays and Maps instead. As a proof, running your query above, I get the following error:

"script" : "ctx._source.addresses = [{country:'Country', countryCode : 'cr'}]",
"lang" : "painless",
"position" : {
  "offset" : 25,
  "start" : 0,
  "end" : 50
},
"caused_by" : {
  "type" : "illegal_argument_exception",
  "reason" : "invalid sequence of tokens near ['{'].",
  "caused_by" : {
    "type" : "no_viable_alt_exception",
    "reason" : null
  }
}