# Unable to update the nested field in elasticsearch

**URL:** <https://discuss.elastic.co/t/unable-to-update-the-nested-field-in-elasticsearch/96959>\
**Category:** Elasticsearch\
**Created:** [August 14, 2017, 11:31am UTC](https://discuss.elastic.co/t/unable-to-update-the-nested-field-in-elasticsearch/96959 "2017-08-14T11:31:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chitrank\_Dixit](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@Chitrank\_Dixit](https://discuss.elastic.co/u/Chitrank_Dixit)\
**Post date:** [August 14, 2017, 11:31am UTC](https://discuss.elastic.co/t/unable-to-update-the-nested-field-in-elasticsearch/96959/1 "2017-08-14T11:31:46Z")

</div>

I am working to dump and update the Elasticsearch data, I am able to properly dump the data but I am not able to update the records for example

when I do

GET test/users/3985882

I would get the following response

{  
"\_index": "test",  
"\_type": "users",  
"\_id": "3985882",  
"\_version": 1,  
"found": true,  
"\_source": {  
"gender": null,  
"user\_mixpanel\_data": {  
"event\_name": "Detail",  
"time": "2017-07-01 01:37:43 IST",  
"email": "abc@gmail.com"  
},  
"email": "abc@gmail.com",  
"user\_invoices": [  
{  
"number": "54539",  
"orders": [  
{  
"order\_id": "54539C1"  
}  
]  
}  
]  
}  
}  
Now when I try to update the nested object user\_invoices and need to update this, I am trying the script from the following question asked on SO i.e. [https://stackoverflow.com/questions/36589963/elasticsearchuse-script-to-update-nested-field](https://stackoverflow.com/questions/36589963/elasticsearchuse-script-to-update-nested-field)

I modified the script according to my problem.

POST test/users/\_update  
{  
"script": "def updated = false; ctx.\_source.user\_invoices?.each { obj -\> if (obj.number == item.number) { obj.number = item.new\_number; updated = true;}; if (!updated) { ctx.\_source.user\_invoices = ((ctx.\_source.user\_invoices ?: []) + item)}",  
"lang": "painless",  
"params": {  
"item": {  
"number": "54539",  
"new\_number": "54540",  
"order\_id": "54539C1",  
"new\_order\_id": "54539C2"  
}  
}  
}  
the above script gives me the following response

{  
"\_index": "test",  
"\_type": "users",  
"\_id": "\_update",  
"\_version": 28,  
"result": "updated",  
"\_shards": {  
"total": 2,  
"successful": 1,  
"failed": 0  
},  
"created": false  
}  
Here the response shows the query was successful, but when I again see the same record I do not get the updated response.

I also have asked this question on Stack Overflow , you can check this here:

> <https://stackoverflow.com/questions/45505173/unable-to-update-the-nested-field-in-elasticsearch>

---

<div class="post-metadata">

**Author:** ![Chitrank\_Dixit](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@Chitrank\_Dixit](https://discuss.elastic.co/u/Chitrank_Dixit)\
**Post date:** [August 17, 2017, 2:07pm UTC](https://discuss.elastic.co/t/unable-to-update-the-nested-field-in-elasticsearch/96959/2 "2017-08-17T14:07:35Z")

</div>

I am stuck with this issue , Is there no solution present to resolve the nested object updation in elasticsearch, if not then what other alternative I can follow to deal with this issue ?

---

<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:** [September 14, 2017, 2:08pm UTC](https://discuss.elastic.co/t/unable-to-update-the-nested-field-in-elasticsearch/96959/3 "2017-09-14T14:08:18Z")

</div>

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