将元素追加到Eslasticsearch字段 [英] Append element to Eslasticsearch field
问题描述
如果类型不匹配,是否可以将一些元素附加到elasticsearch字段?如果我有这样的文件:
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"
}
并且我想附加另一个标记字段,例如"blue":
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"]
}
我知道这一点:
"source": "ctx._source.counter += params.newTag"
用于将字符串追加到另一个字符串
is used for append string to another string
和这个:
"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.
推荐答案
您可以做的是为tags
字段的类型添加一个测试,如果不是,则将其转换为数组.该脚本应该有所帮助.
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"
}
}
}
这篇关于将元素追加到Eslasticsearch字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!