# How to update all the documents in elastic seach

**URL:** <https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018>\
**Category:** Elasticsearch\
**Created:** [January 4, 2018, 6:24am UTC](https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018 "2018-01-04T06:24:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![happy0088](https://avatars.discourse-cdn.com/v4/letter/h/71c47a/32.png) [@happy0088](https://discuss.elastic.co/u/happy0088)\
**Post date:** [January 4, 2018, 6:24am UTC](https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018/1 "2018-01-04T06:24:37Z")

</div>

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 !!

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 4, 2018, 6:32am UTC](https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018/2 "2018-01-04T06:32:36Z")

</div>

You can use the [update by query API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html) 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": {}
  }
}'
```

---

<div class="post-metadata">

**Author:** ![happy0088](https://avatars.discourse-cdn.com/v4/letter/h/71c47a/32.png) [@happy0088](https://discuss.elastic.co/u/happy0088)\
**Post date:** [January 4, 2018, 9:52am UTC](https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018/3 "2018-01-04T09:52:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 4, 2018, 10:01am UTC](https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018/4 "2018-01-04T10:01:18Z")

</div>

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": {} }}'`

---

<div class="post-metadata">

**Author:** ![happy0088](https://avatars.discourse-cdn.com/v4/letter/h/71c47a/32.png) [@happy0088](https://discuss.elastic.co/u/happy0088)\
**Post date:** [January 4, 2018, 10:21am UTC](https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018/5 "2018-01-04T10:21:06Z")

</div>

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

Really appreciate your prompt reply @val .

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 4, 2018, 10:24am UTC](https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018/6 "2018-01-04T10:24:30Z")

</div>

Cool, glad it worked out!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 1, 2018, 10:24am UTC](https://discuss.elastic.co/t/how-to-update-all-the-documents-in-elastic-seach/114018/7 "2018-02-01T10:24:35Z")

</div>

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