# Partial update and nested type

**URL:** https://discuss.elastic.co/t/partial-update-and-nested-type/7549
**Category:** Elasticsearch
**Created:** [May 3, 2012, 12:30pm UTC](https://discuss.elastic.co/t/partial-update-and-nested-type/7549 "2012-05-03T12:30:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![avasilenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avasilenko/32/2901_2.png) [@avasilenko](https://discuss.elastic.co/u/avasilenko)
#### Post date: [May 3, 2012, 12:30pm UTC](https://discuss.elastic.co/t/partial-update-and-nested-type/7549/1 "2012-05-03T12:30:12Z")

</div>

Hello,

How can I append values to nested document, using partial update  
[http://www.elasticsearch.org/guide/reference/api/update.html](http://www.elasticsearch.org/guide/reference/api/update.html) ?  
My tries: [https://gist.github.com/2585300](https://gist.github.com/2585300) . And it doesn't work

Thanks,  
Alexandr Vasilenko

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [May 4, 2012, 1:20pm UTC](https://discuss.elastic.co/t/partial-update-and-nested-type/7549/2 "2012-05-04T13:20:19Z")

</div>

Can you post a full curl recreation? Create the index with the mapping,  
index sample doc, and issue the update that fails.

On Thu, May 3, 2012 at 3:30 PM, Alex Vasilenko [aa.vasilenko@gmail.com](mailto:aa.vasilenko@gmail.com)wrote:

> Hello,
> 
> How can I append values to nested document, using partial update  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update.html) ?  
> My tries: [Elasticsearch nested type update · GitHub](https://gist.github.com/2585300) . And it doesn't work
> 
> Thanks,  
> Alexandr Vasilenko

---

<div class="post-metadata">

### Author: ![Jeremy\_Daly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeremy_daly/32/2828_2.png) [@Jeremy\_Daly](https://discuss.elastic.co/u/Jeremy_Daly)
#### Post date: [July 3, 2012, 9:56pm UTC](https://discuss.elastic.co/t/partial-update-and-nested-type/7549/3 "2012-07-03T21:56:20Z")

</div>

Alex,

I was having the same problem as you and finally figured it out. A nested  
object is an array of objects, so when you append a new nested object it  
needs to be wrapped in an array construct. Notice the brackets instead  
of curly braces { } below for the comment param value.

{  
"script": "ctx.\_source.comments += comment",  
"params": {  
"comment": ["id": 123, "body": "boom"]  
}  
}

In my particular case I needed to check as to whether or not the "comments"  
field existed in the source document. Otherwise ES generates an error and  
cannot append the new object to an array that doesn't exist. This is easily  
accomplished with a simple if-else statement.

{  
"script": "if (ctx.\_source["comments"] == null) { ctx.\_source.comments = comment } else {

ctx.\_source.comments += comment }",

"params": {  
"comment": ["id": 123, "body": "boom"]  
}  
}

Hope that helps.

- Jeremy

On Thursday, May 3, 2012 8:30:12 AM UTC-4, Alex Vasilenko wrote:

> Hello,
> 
> How can I append values to nested document, using partial update  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update.html) ?  
> My tries: [Elasticsearch nested type update · GitHub](https://gist.github.com/2585300) . And it doesn't work
> 
> Thanks,  
> Alexandr Vasilenko

---

<div class="post-metadata">

### Author: ![Jeremy\_Daly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeremy_daly/32/2828_2.png) [@Jeremy\_Daly](https://discuss.elastic.co/u/Jeremy_Daly)
#### Post date: [July 3, 2012, 10:02pm UTC](https://discuss.elastic.co/t/partial-update-and-nested-type/7549/4 "2012-07-03T22:02:26Z")

</div>

Alex,

I was having the same problem as you and finally figured it out. A nested  
object is an array of objects, so when you append a new nested object it  
needs to be wrapped in an array construct. Notice the added brackets   
below for the comment param value.

{  
"script": "ctx.\_source.comments += comment",  
"params": {  
"comment": [{ "id": 123, "body": "boom" }]  
}  
}

In my particular case I needed to check as to whether or not the "comments"  
field existed in the source document. Otherwise ES generates an error and  
cannot append the new object to an array that doesn't exist. This is easily  
accomplished with a simple if-else statement.

{  
"script": "if (ctx.\_source["comments"] == null) { ctx.\_source.comments  
= comment } else {  
ctx.\_source.comments += comment }",  
"params": {  
"comment": [{"id": 123, "body": "boom"}]  
}  
}

Hope that helps.

- Jeremy

On Thursday, May 3, 2012 8:30:12 AM UTC-4, Alex Vasilenko wrote:

> Hello,
> 
> How can I append values to nested document, using partial update  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update.html) ?  
> My tries: [Elasticsearch nested type update · GitHub](https://gist.github.com/2585300) . And it doesn't work
> 
> Thanks,  
> Alexandr Vasilenko

---

<div class="post-metadata">

### Author: ![Get2sada](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@Get2sada](https://discuss.elastic.co/u/Get2sada)
#### Post date: [February 3, 2015, 10:47pm UTC](https://discuss.elastic.co/t/partial-update-and-nested-type/7549/5 "2015-02-03T22:47:15Z")

</div>

Hi: Below nested update does not insert a new nested object in the array, Notice the added brackets []

curl -XPOST '[http://localhost:9200/v2/Test/cd196e01/\_update](http://localhost:9200/v2/Test/cd196e01/_update)' -d'{"script":"ctx.\_source.Code += Code",  
"params":{"Code":[{  
"Family":"Family",  
"Code":"Raw",  
"Code":12345,  
"Role":"",  
"Rank":4,  
"Description":"Some updates",  
"SourceType":"human",  
"SourceSequence":1,  
"SourceValue":"some value",  
"Status":"active"  
}]  
}  
}'

Getting error msg:

{"error":"ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptExecutionException[ClassCastException[java.util.HashMap cannot be cast to java.util.Map$Entry]

However, removing the brackets [] will update the existing nested object. Need to insert nested object instead.

Any help is much appreciated !!

Thank You

---

<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: [July 6, 2017, 12:35am UTC](https://discuss.elastic.co/t/partial-update-and-nested-type/7549/6 "2017-07-06T00:35:06Z")

</div>


