【问题标题】:Append element to Eslasticsearch field将元素附加到 Elasticsearch 字段
【发布时间】:2018-01-09 15:18:44
【问题描述】:

如果类型不匹配,是否可以将一些元素附加到 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)"

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

【问题讨论】:

    标签: elasticsearch elasticsearch-painless


    【解决方案1】:

    您可以做的是为您的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"
         }
       }
    }
    

    【讨论】:

    • 非常感谢您的想法!
    • 酷,很高兴它有帮助!
    • 这应该在文档中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2021-10-26
    • 2021-12-26
    相关资源
    最近更新 更多