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等字段信息。
也可以添加某个字段:
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作为版本号,那么更新操作将不会再改变版本号。注意,这样就无法保证文档是否被修改了。
Elasticsearch 的 Update更新的更多相关文章
- Elasticsearch增删改查 之 —— Update更新
		
更新操作,一般用这个的,应该不会很多吧!ES本身还是一个倾向于查询检索的框架,对于这种更新的操作,太过频繁总归是不好的. 不过阅读本篇后,你可以使用Script对所有的文档执行更新操作,也可以使用do ...
 - 在UPDATE中更新TOP条数据以及UPDATE更新中使用ORDER BY
		
正常查询语句中TOP的运用: SELECT TOP 1000 * FROM MP_MemberGrade 随意更新一张表中满足条件的前N条数据: UPDATE TOP (1) MP_Member ...
 - apt-get update更新源时,出现“Hash Sum mismatch”问题
		
当使用apt-get update更新源时,出现下面"Hash Sum mismatch"的报错,具体如下:root@localhost:~# apt-get update.... ...
 - Linux/CentOS配置:使用yum update更新时不升级内核的方法
		
RedHat/CentOS/Fedora使用 yum update 更新时,默认会升级内核.但有些服务器硬件(特别是组装的机器)在升级内核后,新的内核可能会认不出某些硬件,要重新安装驱动,很麻烦.所以 ...
 - update更新两个字段
		
update更新两个字段时的sql语句: update tj_record set is_recycle_reprint_guide='1' , recycle__guide_date=now() w ...
 - 由于物化视图定义为on commit导致update更新基表慢的解决方案
		
由于物化视图定义为on commit导致update更新基表慢的解决方案 以下是模拟和解决测试过程: (模拟update慢的过程) 1.首先基于基表创建物化视图日志: create materiali ...
 - apt-get update 更新 ubuntu时出现Hash sum mismatch的原因及解决方法
		
$ sudo apt-get update ...... Hit http://mirrors.163.com trusty/main Sources ...
 - 4.update更新和delete删除用法
		
一.update更新 UserMapper.java package tk.mybatis.simple.mapper; import org.apache.ibatis.annotations.Pa ...
 - [置顶]
        ubuntu版本很老,apt-get update更新失败时(W: Failed to fetch ...)------如何创建新的sources.list
		
在说这个解决方案之前,我先说下,目前遇到的问题: 我使用 sudo apt-get update 之后,更新失败.具体原因如下: W: Failed to fetch http://cn.archiv ...
 
随机推荐
- freemarker的简单入门程序
			
本文主要介绍了freemarker的常用标签<#list> <#import> <#assign> <#if> <#else> &l ...
 - js得到时间戳(10位数)
			
//从1970年开始的毫秒数然后截取10位变成 从1970年开始的秒数 function timest() { var tmp = Date.parse( new Date() ).toString( ...
 - 基于Xen实现一种domain0和domainU的应用层数据交互高效机制
			
项目里有一个需求,domain0的应用层需要定时给domainU(hvm windows)的应用层传递一块数据,原来的方案是在domainU的应用层架设一个http服务器,监听在某个端口,然后需要塞数 ...
 - UVA 11125 Arrange Some Marbles
			
dp[i][j][m][n][s]表示最初选择j个i号颜色大理石.当前选择n个m号颜色大理石.剩余大理石状态(8进制数状压表示)最开始没看出状压..sad #include <map> # ...
 - cp2102通过GPIO连接树莓派
			
此博客不在更新,我的博客新地址:www.liuquanhao.com ----------------------------------------------------------------- ...
 - blog postman 翻译(2):postman中新增request attributes 的描述(desc)
			
blog的原文链接是:http://blog.getpostman.com/2017/06/03/descriptions-for-request-attributes-in-postman/ pos ...
 - AC日记——[HAOI2007]覆盖问题 bzoj 1052
			
1052 思路: 二分答案: 二分可能的长度: 然后递归判断长度是否可行: 先求出刚好覆盖所有点的矩形: 可行的第一个正方形在矩形的一个角上: 枚举四个角上的正方形,然后删去点: 删去一个正方形后,递 ...
 - block方法加入无效的问题
			
+(LDLabel *)labelWithFrame:(CGRect)frame withTitle:(NSString *)title withFont:(float)font withtextCo ...
 - selenium  IE自动化问题汇总
			
驱动下载:http://selenium-release.storage.googleapis.com/index.html 没有修改IE的保护模式设置导致,通常看到报错信息如下: selenium. ...
 - 2018 JUST Programming Contest 1.0 题解
			
题目链接 gym101778 Problem A 转化成绝对值之后算一下概率.这个题有点像 2018 ZOJ Monthly March Problem D ? 不过那个题要难一些~ #includ ...