Elasticsearch增删改查 之 —— Update更新

更新操作,一般用这个的,应该不会很多吧!ES本身还是一个倾向于查询检索的框架,对于这种更新的操作,太过频繁总归是不好的。
不过阅读本篇后,你可以使用Script对所有的文档执行更新操作,也可以使用doc对部分文档执行更新,也可以使用upsert对不存在的文档执行添加操作。

更新

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等字段信息。

也可以添加某个字段:

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以及数组。

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
}'

参数

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

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

version & version_type

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

外部版本号

更新操作是不支持外部版本号的,因为本来外部版本号就脱离系统的版本控制,如果再执行更新操作,那就彻底乱了。如果使用了外部版本号,可以使用Index代替更新操作,重新索引文档。

posted @ 2016-03-28 21:48  xingoo  阅读(111310)  评论(1编辑  收藏  举报