How to update all the documents in elastic seach

Hi,
I need to update all the value of the keys named as "per_name" (key is the document Json key:value pair entry ) in all the documents in all the indexes present in my Elastic Search .
I have an update curl which takes index/Type/id
like this :

curl -XPOST 'localhost:9200/customer/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
"doc": { "per_name": "Jane Doe" }
}
'
For the above one , i would need to iterate on each document in each Types and then do a POST .
Is there any other way that i can directly replace the value of per_name in all the indexes in one go ?

Thanks in advance !!

1 Like

You can use the update by query API to achieve this:

curl -XPOST localhost:9200/customer/_update_by_query -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "ctx._source.per_name = 'Jane Doe'",
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}'
2 Likes

Thanks val for the quick reply .
I am trying the below curl :

curl -XPOST localhost:9200/restored_index/_update_by_query?pretty -d'{ "script": {"inline": "ctx._source.per_name = 'happy'" }, "query": { "match_all": {} }}'

But it gives the below error:

"script" : "ctx._source.per_name = happy",
"lang" : "painless",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Variable [happy] is not defined."

Can u help me with this ?

Thanks

You actually need to double the single quotes around happy since this is also the delimiter you use to surround your query

curl -XPOST localhost:9200/restored_index/_update_by_query?pretty -d'{ "script": {"inline": "ctx._source.per_name = ''happy''" }, "query": { "match_all": {} }}'

or more simply escape double quotes inside the query

curl -XPOST localhost:9200/restored_index/_update_by_query?pretty -d'{ "script": {"inline": "ctx._source.per_name = \"happy\"" }, "query": { "match_all": {} }}'

2 Likes

hurrey !!.. the second one works like a charm !!!!!!!!

Really appreciate your prompt reply @val .

1 Like

Cool, glad it worked out!

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