# Script example to update nested docs list

**URL:** <https://discuss.elastic.co/t/script-example-to-update-nested-docs-list/8215>\
**Category:** Elasticsearch\
**Created:** [June 25, 2012, 3:22pm UTC](https://discuss.elastic.co/t/script-example-to-update-nested-docs-list/8215 "2012-06-25T15:22:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rauan\_Maemirov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rauan_maemirov/32/826_2.png) [@Rauan\_Maemirov](https://discuss.elastic.co/u/Rauan_Maemirov)\
**Post date:** [June 25, 2012, 3:22pm UTC](https://discuss.elastic.co/t/script-example-to-update-nested-docs-list/8215/1 "2012-06-25T15:22:42Z")

</div>

Hi list. I'm looking for example of script to 'insert-or-update' nested  
docs list.

Let's say, we have the following mapping:

curl -XPUT '[http://localhost:9200/nestedtest/authors/\_mapping](http://localhost:9200/nestedtest/authors/_mapping)' -d '{  
"authors": {  
"properties": {  
"name": {"type": "string"},  
"books": {  
"type": "nested", "index": "not\_analyzed",  
"properties": {  
"isbn": {"type": "string"},  
"title": {"type": "string"},  
"published\_year": {"type": "integer", "index":  
"not\_analyzed"},  
"options": {"type": "string", "index": "not\_analyzed"},  
"ordered": {"type": "integer", "index": "not\_analyzed"}  
}  
}  
}  
}  
}' && echo

And sample data:

curl -XPUT '[http://localhost:9200/nestedtest/authors/1](http://localhost:9200/nestedtest/authors/1)' -d '{  
"name": "John Steinbeck",  
"books": [  
{  
"isbn": "abcdefg",  
"title": "The Grapes of Wrath", "published\_year": 1939,  
"options": ["genre#fiction", "country#american", "pulitzer#yes"],  
"ordered": 4  
}  
]  
}' && echo

I want to add book of this author and depending on unique isbn add nested  
doc to the main doc or replace the one that already exists.  
Any hints? Either default mval or javascript script will be ok.

---

<div class="post-metadata">

**Author:** ![Rauan\_Maemirov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rauan_maemirov/32/826_2.png) [@Rauan\_Maemirov](https://discuss.elastic.co/u/Rauan_Maemirov)\
**Post date:** [June 26, 2012, 12:15pm UTC](https://discuss.elastic.co/t/script-example-to-update-nested-docs-list/8215/2 "2012-06-26T12:15:17Z")

</div>

Can someone please show me example with traversing array in script, I  
was struggling with both mvel and js, but couldn't help it.

Le lundi 25 juin 2012 21:22:42 UTC+6, Rauan Maemirov a écrit :

> Hi list. I'm looking for example of script to 'insert-or-update' nested  
> docs list.
> 
> Let's say, we have the following mapping:
> 
> curl -XPUT '[http://localhost:9200/nestedtest/authors/\_mapping](http://localhost:9200/nestedtest/authors/_mapping)' -d '{  
> "authors": {  
> "properties": {  
> "name": {"type": "string"},  
> "books": {  
> "type": "nested", "index": "not\_analyzed",  
> "properties": {  
> "isbn": {"type": "string"},  
> "title": {"type": "string"},  
> "published\_year": {"type": "integer", "index":  
> "not\_analyzed"},  
> "options": {"type": "string", "index": "not\_analyzed"},  
> "ordered": {"type": "integer", "index": "not\_analyzed"}  
> }  
> }  
> }  
> }  
> }' && echo
> 
> And sample data:
> 
> curl -XPUT '[http://localhost:9200/nestedtest/authors/1](http://localhost:9200/nestedtest/authors/1)' -d '{  
> "name": "John Steinbeck",  
> "books": [  
> {  
> "isbn": "abcdefg",  
> "title": "The Grapes of Wrath", "published\_year": 1939,  
> "options": ["genre#fiction", "country#american", "pulitzer#yes"],  
> "ordered": 4  
> }  
> ]  
> }' && echo
> 
> I want to add book of this author and depending on unique isbn add nested  
> doc to the main doc or replace the one that already exists.  
> Any hints? Either default mval or javascript script will be ok.

---

<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, 3:22am UTC](https://discuss.elastic.co/t/script-example-to-update-nested-docs-list/8215/3 "2017-07-06T03:22:30Z")

</div>

Closed on July 6, 2017, 3:22am UTC


