gpt4 book ai didi

elasticsearch - 将元素附加到 Elasticsearch 字段

转载 作者:行者123 更新时间:2023-12-02 22:43:31 24 4
gpt4 key购买 nike

如果类型不匹配,是否可以将一些元素附加到 elasticsearch 字段?如果我有这样的文档:

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

我想像这样附加另一个标签字段 f.e“blue”:

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

我想得到类似的结果:

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

我知道这个:

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

用于将字符串附加到另一个字符串

还有这个:

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

用于将另一个元素附加到列表中。那么有什么办法可以将另一个元素附加到字符串字段吗?感谢您的任何建议。

最佳答案

您可以做的是为您的 tags 字段类型添加一个测试,如果不是,则将其转换为数组。此脚本应该有所帮助。

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

关于elasticsearch - 将元素附加到 Elasticsearch 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48171317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com