I want to add a object into the nested
field every update time.
For example,I have a doc:
{
\"test\":[{\"remark\":\"remark1\"}]
}
I suggest to try a script like this, which takes two parameters in argument. It will check if any of the nested objects already contains the given id:
remark
test
array.The script goes like this:
def updated = false
ctx._source.test?.each { obj ->
if (obj.id == item.id) {
obj.remark = item.remark
updated = true
}
}
if (!updated) {
ctx._source.test = ((ctx._source.test ?: []) + item)
}
After being inlined and with proper semicolons, the script looks like this:
{
"script": "def updated = false; ctx._source.test?.each { obj -> if (obj.id == item.id) { obj.remark = item.remark; updated = true } }; if (!updated) { ctx._source.test = ((ctx._source.test ?: []) + item)}",
"params": {
"item": {
"remark": "affffd",
"id": "1"
}
}
}