【问题标题】:Elasticsearch Update API if a field does not exist如果字段不存在,则 Elasticsearch 更新 API
【发布时间】:2013-04-12 09:32:51
【问题描述】:

upsert 的例子是:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : "ctx._source.counter += count",
    "params" : {
        "count" : 4
    },
    "upsert" : {
        "counter" : 1
    }
}'

如果该文档以前不存在,则有效。

假设我想更新一个不一定存在但文档存在的字段。 例如,文档可能还没有计数器字段。

我该怎么做?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以使用更新脚本检查字段是否存在:

    curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
        "script" : "if( ctx._source.containsKey(\"counter\") ){ ctx._source.counter += count; } else { ctx._source.counter = 1; }",
        "params" : {
            "count" : 4
        },
        "upsert" : {
            "counter" : 1
        }
    }'
    

    【讨论】:

    • 太棒了!您是否碰巧知道是否有一些网站可以真正解释 MVEL 语言?我尽力学习它,但找不到任何像样的资源。 mvel.codehaus.org/Language+Guide+for+2.0 太可怕了,几乎什么都没有
    • 这是我所知道的最好的资源。我的建议是安装 javascript plugin 并切换到 javascript 作为您的脚本语言。
    • 来自 elasticsearch 关于脚本语言的文档:The scripting module uses by default groovy (previously mvel in 1.3.x and earlier)
    猜你喜欢
    • 2022-11-18
    • 2018-09-17
    • 2021-04-24
    • 2016-01-25
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多