Append element to Eslasticsearch field

冷暖自知 提交于 2019-12-25 00:16:25

问题


please is possible to append some element to elasticsearch field if types doesn't match? If I have document like this:

{
"counter" : 1,
"tags" : "red"
}

and i want to append another tag field f.e "blue" like this:

{
"script" : {
    "source": "ctx._source.counter += params.newTag",
    "lang": "painless",
    "params" : {
        "newTag" : "blue"
    }
}
}

and i want to have result similar to:

{
"counter" : 1,
"tags" : ["red", "blue"]
 }

i know this:

"source": "ctx._source.counter += params.newTag"

is used for append string to another string

and this:

"source": "ctx._source.counter.add(params.newTag)"

for appending another element to list. So is there any way how to append another element to string field ? Thank You for any suggestions.


回答1:


What you can do is to add a test for the type of your tags field and transform it to an array if it is not. This script should help.

{
   "script" : {
     "source": "if (!(ctx._source.tags instanceof List}) {ctx._source.tags = [ctx._source.tags]} ctx._source.tags += params.newTag",
     "lang": "painless",
     "params" : {
        "newTag" : "blue"
     }
   }
}


来源:https://stackoverflow.com/questions/48171317/append-element-to-eslasticsearch-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!