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作为版本号,那么更新操作将不会再改变版本号。注意,这样就无法保证文档是否被修改了。

最新文章

  1. 设计模式C#实现(十五)——命令模式
  2. C++11 之 scoped enum
  3. 苹果5S指纹扫描识别传感器Touch ID有利于iPhone的安全性
  4. linux vsftpd搭建
  5. 夺命雷公狗---DEDECMS----10dedecms双标签
  6. django - 修改 自增长id,起始值
  7. 自定义Adapter
  8. 【制作镜像Win*】系统配置
  9. C#对word、excel、pdf等格式文件的操作总结
  10. phpcms v9 分页
  11. AutoFac使用方法总结:Part III
  12. [转]Iphone m3u8 segmenter from ffmpeg for video streaming
  13. 使用Func<T1, T2, TResult>
  14. Elasticsearch笔记七之setting,mapping,分片查询方式
  15. 期货大赛项目|十,MVC对js和css的压缩
  16. Sketch 画原型比 Axure 好用吗?为什么?
  17. python对象的不同参数集合
  18. Centos7安装Tomcat并部署DubboAdmin的War包并配置自动启动
  19. Fiddler4入门--手机抓包工具安装和使用说明
  20. C语言:结构体,共用体

热门文章

  1. 70种简单常用的JS代码
  2. hihoCoder 1015KMP
  3. Java中同一个类中不同的synchronized方法是否可以并发执行?
  4. hdu1024 Max Sum Plus Plus的另一种解法
  5. bzoj 3289 Mato的文件管理 区间逆序对数(离线) 莫队
  6. OpenGL “太阳、地球和月亮”天体运动动画 例子
  7. 配置wpa_supplicant调试wifi linux下命令行连接wifi
  8. 28.Implement strStr()---kmp
  9. spark streaming 异常No output streams registered, so nothing to execute
  10. 【原创】SQL Server Job邮件详细配置