ElasticSearch: Updating nested document in array -
given document
curl -xput 'localhost:9200/test/me/here' -d '{ "top" : [ { "searchkey" : "change"}, { "searchkey" : "keep"} ] }'
i need update query add new field sub-document searchkey
equal change
, keep other sub-document intact. expected result then:
{ "top" : [ { "searchkey" : "change", "newfield" : "newvalue"}, { "searchkey" : "keep"} ] }
running query selects inner document index works, not know inner order in advance , quite fragile anyways:
curl -xpost 'localhost:9200/test/me/here/_update' -d '{ "script" : "ctx._source.top[0].newfield = v", "params" : { "v" : "newvalue" } }'
is there way tell es add new field inner document matches condition? 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, better , save headache if eliminate array , transform document to:
{ "change" : {}, "keep" : {} }
you can use update script. see example:
put test/data/3/ { "source": [ { "name": "a", "count": 1 }, { "name": "b", "count": 2 }, { "name": "c", "count": 3 } ] } test/data/3 post test/data/3/_update { "script": " (int = 0; < source.size(); i++) {boolean f = false;for (int j = 0; j < ctx._source.source.size(); j++) {if (ctx._source.source[j].name == source[i].name) {ctx._source.source[j].count = source[i].count;f=true;break;}}\nif(!f){ctx._source.source.add(source[i]);}}", "params": { "source": [ { "name": "a", "count": 10 }, { "name": "b", "count": 30 }, { "name": "d", "count": 50 } ] } } test/data/3
Comments
Post a Comment