0

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

SexyMF
  • 10,657
  • 33
  • 102
  • 206

1 Answers1

4

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
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • How to achieve what? – Val Apr 22 '21 at 05:52
  • The "you need to go through Arrays and Maps instead" thing. thanks – SexyMF Apr 22 '21 at 06:04
  • That's what the script I provided at the top does :-) I've also added some links – Val Apr 22 '21 at 06:21
  • Ohhh. now I get it. sorry. didnt see it correctly, I will check today and mark it as resolved. thanks! – SexyMF Apr 22 '21 at 06:48
  • Awesome, let me know how it works when you can – Val Apr 22 '21 at 06:52
  • Ok, your solution did solve it, I had another problem with my script. here is the full working script, please update your answer with it so it would be valid: https://pastebin.com/RwJCvVb5 Also, can you please write if its possible to do it in batches so it wont throw time out? thanks – SexyMF Apr 22 '21 at 08:16