Elasticsearch 的 Update更新

Update更新操作允许ES获得某个指定的文档,可以通过脚本等操作对该文档进行更新。可以把它看成是先删除再索引的原子操作,只是省略了返回的过程,这样即节省了来回传输的网络流量,也避免了中间时间造成的文档修改冲突。
下面就是更新的例子:

curl -XPUT localhost:9200/test/type1/1 -d '{
    "counter" : 1,
    "tags" : ["red"]
}'

脚本更新

Es支持通过脚本更改文档的信息:

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

上面就是通过参数来为每个counter加4.
也可以添加某个标记:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : {
        "inline": "ctx._source.tags += tag",
        "params" : {
            "tag" : "blue"
        }
    }
}'

除了_source字段,可以通过ctx来获得_index、_type、_id、_version、_parent、_timestamp、_ttl等字段信息。

也可以添加某个字段:文章来源地址:https://www.yii666.com/article/764305.html

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : "ctx._source.name_of_new_field = \"value_of_new_field\""
}'

移除字段:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : "ctx._source.remove(\"name_of_field\")"
}'

也支持稍微复杂点的逻辑,比如根据某个标记执行不同的操作。比如如果有blue这个标记,则删除该文档;否则什么也不做:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : {
        "inline": "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : ctx.op = \"none\"",
        "params" : {
            "tag" : "blue"
        }
    }
}'

只更新部分文档

上面的脚本是对所有的文档都起作用,这里讲解下如何只对部分文档进行修改。使用doc可以实现简单的递归合并、内部合并、替换KV以及数组。文章来源地址https://www.yii666.com/article/764305.html

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "doc" : {
        "name" : "new_name"
    }
}'

如果同时使用了doc和script,那么doc的操作会自动忽略。因此最好是把特殊的操作也放在脚本中。
更新检测

如果使用doc,那么会自动合并到现有的文档中。如果doc中定义的部分与现在的文档相同,则默认不会执行任何动作。设置detect_noop=false,就会无视是否修改,强制合并到现有的文档。

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "doc" : {
        "name" : "new_name"
    },
    "detect_noop": false
}'

上面的例子中,如果name字段为new_name,无论当前的文档是否与doc中定义的相同,都会把doc合并到文档中。
upsert插入

这个参数主要用于当文档不存在时,ES的操作。

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

在上面的例子中,当文档存在时,执行脚本;当文档不存在时,upsert中的内容就会插入到对应的文档中。

如果你想无论文档是否存在都执行脚本操作,那么可以使用参数scripted_upsert为true。

curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{
    "scripted_upsert":true,
    "script" : {
        "id": "my_web_session_summariser",
        "params" : {
            "pageViewEvent" : {
                "url":"foo.com/bar",
                "response":404,
                "time":"2014-01-01 12:32"
            }
        }
    },
    "upsert" : {}
}'

相对于之前的使用Upsert中的内容添加到不存在的文档,使用doc_as_upsert可以在文档不存在的时候,把doc中的内容插入到文档中。

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "doc" : {
        "name" : "new_name"
    },
    "doc_as_upsert" : true
}'网址:yii666.com

参数
retry_on_conflict

当执行索引和更新的时候,有可能另一个进程正在执行更新。这个时候就会造成冲突,这个参数就是用于定义当遇到冲突时,再过多长时间执行操作。
routing

Routing is used to route the update request to the right shard and sets the routing for the upsert request if the document being updated doesn’t exist. Can’t be used to update the routing of an existing document.
parent

Parent is used to route the update request to the right shard and sets the parent for the upsert request if the document being updated doesn’t exist. Can’t be used to update the parent of an existing document.
timeout

当分片不可用的时候,等待多长时间
consistency

The write consistency of the index/delete operation.
索引/删除操作的写一致性!不知道怎么用
refresh

当执行操作的时候,会自动刷新索引。
fields文章地址https://www.yii666.com/article/764305.html网址:yii666.com<

执行完更新后,返回的字段
version & version_type

更新操作会使用版本号来确定 拿到文档到执行更新期间,文档是否被修改过。也可以通过特定的版本号,更新文档。如果使用force作为版本号,那么更新操作将不会再改变版本号。注意,这样就无法保证文档是否被修改了。

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

Elasticsearch 的 Update更新-相关文章

  1. pymysql基本的使用方法

  2. SQL中使用UPDATE更新数据时一定要记得WHERE子句

  3. Elasticsearch 的 Update更新

  4. SVN提示update更新成功,但是本地文件却没有更新

  5. 认识oracle的update更新

    这两天给新同事安排了一个工作,即做一个update 的级联更新,在实际操作中发现了一个问题。就是对于Oracle的更新的语法,大部分人尤其是学过SqlServer的人在使用oracle的时候对于oracle的更新会有许多的疑问。就此记下,以便日后查阅update a set a.col1 = (select b.col1 from b where b.co

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png