【问题标题】:Append to a Elasticsearch field list/array if it's not an existing element如果它不是现有元素,则附加到 Elasticsearch 字段列表/数组
【发布时间】:2015-07-01 17:41:01
【问题描述】:

假设我们有:

curl -XPOST "http://localhost:9200/t/t/1" -d'
{
     "hobbies" : ["a", "b"]
}'

我知道我可以这样做来附加到爱好:

curl -XPOST "http://localhost:9200/t/t/1/_update" -d'
{
     "script" : "ctx._source.hobbies += hobby",
     "params" : {
         "hobby" : "c"
     }
}'

但是,如果 hobby 不是 'c' 而是 'b',我该怎么做,我不会以 ["a", "b", "b"] 作为 hobby 结尾?因此,如果“c”还不是爱好,它只会附加“c”?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你可以通过稍微修改 OP 中的脚本来达到同样的效果

    例子:

    curl -XPOST "http://localhost:9200/t/t/1/_update -d'
    {
         "script" : " if(! ctx._source.hobbies.contains(hobby)){ ctx._source.hobbies += hobby }",
         "params" : {
             "hobby" : "c"
         }
    }'
    

    上面的例子假设字段 hobbies 存在并且是一个 List 否则你需要加入更多的逻辑来处理这些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      相关资源
      最近更新 更多