Given the document
curl -XPUT 'localhost:9200/test/me/here' -d '{
"top" : [
{ "searchkey" : "change"},
{ "searchkey" : "keep"}
]
}'
I need an update query that will add new field to sub-document with searchkey equal to change and will keep any other sub-document intact. The expected result is then:
{
"top" : [
{ "searchkey" : "change", "newfield" : "newvalue"},
{ "searchkey" : "keep"}
]
}
Running query that selects inner document by index works, but I do not know the inner order in advance and it is quite fragile anyways:
curl -XPOST 'localhost:9200/test/me/here/_update' -d '{
"script" : "ctx._source.top[0].newfield = v",
"params" : {
"v" : "newvalue"
}
}'
Is there a way to tell ES to add the new field to the inner document that matches some condition? Something like:
curl -XPOST 'localhost:9200/test/me/here/_update' -d '{
"script" : "ctx._source.top[ctx._source.top.searchkey == s].newfield = v",
"params" : {
"s" : "change",
"v" : "newvalue"
}
}'
Or, will I do better and save some headache if I eliminate the array and transform the document to:
{
"change" : {},
"keep" : {}
}