Script example to update nested docs list

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' -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' -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.

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' -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' -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.