Partial update and nested type

Hello,

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

Thanks,
Alexandr Vasilenko

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.comwrote:

Hello,

How can I append values to nested document, using partial update
Elasticsearch Platform — Find real-time answers at scale | Elastic ?
My tries: Elasticsearch nested type update · GitHub . And it doesn't work

Thanks,
Alexandr Vasilenko

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 ?
My tries: Elasticsearch nested type update · GitHub . And it doesn't work

Thanks,
Alexandr Vasilenko

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 ?
My tries: Elasticsearch nested type update · GitHub . And it doesn't work

Thanks,
Alexandr Vasilenko

1 Like

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