elasticsearch【更新】操作
基于上一篇博文基础上,进行es的操作,document的新增比较简单,就不说了,这里主要说说更新操作。
更新操作,有两大类,一个是Replace,一个是Update,就是说一个是替换,一个是更新。 替换,就是全文档更换,而更新可以只针对文档的局部字段。
1. 这里先说简单的Replace的操作。
先创建一个document,索引名为gengxin,文档类型为replace。
[water@CloudGame ES]$ curl -XPUT 'localhost:9200/gengxin/replace/1?pretty' -d '
{
"name": "shihuc",
"job": "System Software Dev Manager",
"date": "2016-10-19"
}'
{
"_index" : "gengxin",
"_type" : "replace",
"_id" : "",
"_version" : ,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"created" : true #注意,这里的信息状态是true
}
查询看看创建的结果:
[water@CloudGame ES]$ curl "localhost:9200/gengxin/replace/1?pretty"
{
"_index" : "gengxin",
"_type" : "replace",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"name" : "shihuc",
"job" : "System Software Dev Manager",
"date" : "2016-10-19"
}
}
没有什么问题。 然后我们对这个id为1的document进行replace操作:
[water@CloudGame ES]$ curl -XPUT 'localhost:9200/gengxin/replace/1?pretty' -d '
{
"name": "wdn",
"job": "TK CEO"
}'
{
"_index" : "gengxin",
"_type" : "replace",
"_id" : "",
"_version" : ,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"created" : false #注意,这里的状态是false哟,表示没有新建一个
}
再查看一下,这个被replace了的document内容:
[water@CloudGame ES]$ curl "localhost:9200/gengxin/replace/1?pretty"
{
"_index" : "gengxin",
"_type" : "replace",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"name" : "wdn",
"job" : "TK CEO"
}
}
发现没有,这个被Replace后的数据,与新建的时候字段数量都不一样了,就和一个新建很类似。内容完全被替换了。
2. 下面看看Update更新操作,其实,这个操作是针对已经建立的document修改field的内容,字段field的名字和数量是不能被修改的,是不是差别出来了???对的,就是这个差别。
[water@CloudGame ES]$ curl -XPUT 'localhost:9200/gengxin/update/1?pretty' -d '
{
"name": "water",
"job": "跑龙套的宝宝",
"date": "2016-10-19"
}'
{
"_index" : "gengxin",
"_type" : "update",
"_id" : "",
"_version" : ,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"created" : true
}
查看返回值:
[water@CloudGame ES]$ curl "localhost:9200/gengxin/update/1?pretty"
{
"_index" : "gengxin",
"_type" : "update",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"name" : "water",
"job" : "跑龙套的宝宝",
"date" : "2016-10-19"
}
}
再来看看Update的操作:
[water@CloudGame ES]$ curl -XPOST "localhost:9200/gengxin/update/1/_update?pretty" -d '
{
"doc": {"job": "奋斗者"}
}'
{
"_index" : "gengxin",
"_type" : "update",
"_id" : "",
"_version" : ,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
}
}
查看Update的结果:
[water@CloudGame ES]$ curl "localhost:9200/gengxin/update/1?pretty"
{
"_index" : "gengxin",
"_type" : "update",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"name" : "water",
"job" : "奋斗者",
"date" : "2016-10-19"
}
}
到此,是不是发现Replace操作和Update操作的不同了?
1. 指令不同,replace前后都是PUT指令,其实,用POST也可以实现replace,比如上面的replace操作的时候,将PUT换成POST也可以的。但是最好还是用PUT, http指令中POST是用来更新数据的,PUT是用来新增数据用的,虽然两个没有严格的限制。还有,在做Update操作时,指令中含有关键字_update.
2. replace操作时,指令的-d的内容就是最后的数据的内容,也就是查询结果中_source中的内容,但是Update的操作,修改的只是_source中的某些字段的内容。
另外,补充一下Update的操作,其实除了通过doc来修改指定的字段内容,还可以通过script来实现字段内容的修改。script的东西就比较多了,这里简单的演示一个inline的脚本处理,作为引子简单介绍下!
A. 创建一个文档,并查看结果, 文档的内容是说wangbaoqiang有几顶绿帽子,开始创建的时候写的是2个,后来发现其实只有马rong个jian人一个,于是乎要修改。。。
[water@CloudGame ES]$ curl -XPUT "localhost:9200/gengxin/update/2?pretty" -d '{
> "name": "wangbaoqiang",
> "green_hat": ""
> }'
{
"_index" : "gengxin",
"_type" : "update",
"_id" : "",
"_version" : ,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"created" : true
}
[water@CloudGame ES]$
[water@CloudGame ES]$ curl "localhost:9200/gengxin/update/2?pretty"
{
"_index" : "gengxin",
"_type" : "update",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"name" : "wangbaoqiang",
"green_hat" : ""
}
}
好吧,进行修改吧(行内脚本的方式实现):
[water@CloudGame ES]$ curl -XPOST "localhost:9200/gengxin/update/2/_update?pretty" -d '{
> "script": "ctx._source.green_hat = count",
> "inline": {
> "params" : {
> "count" :
> }
> }
> }'
{
"error" : {
"root_cause" : [ {
"type" : "remote_transport_exception",
"reason" : "[node-1][10.90.6.172:9300][indices:data/write/update[s]]"
} ],
"type" : "illegal_argument_exception",
"reason" : "failed to execute script",
"caused_by" : {
"type" : "script_exception",
"reason" : "scripts of type [inline], operation [update] and lang [groovy] are disabled"
}
},
"status" :
}
报错了,意思是说行内脚本通过groovy语言执行update操作是被禁用了的。 查看帮助文档吧,发现script章节有介绍,需要修改elasticsearch.yml配置文件,再其最后,加入下面的内容,重新启动es:
script.inline: true
script.indexed: true
script.engine.groovy.inline.aggs: true
重启ES后,再次操作上述script方式的update指令:
[water@CloudGame ES]$ curl -XPOST "localhost:9200/gengxin/update/2/_update?pretty" -d '{
"script": "ctx._source.green_hat = count",
"inline": {
"params" : {
"count" :
}
}
}'
{
"_index" : "gengxin",
"_type" : "update",
"_id" : "",
"_version" : ,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
}
}
[water@CloudGame ES]$
[water@CloudGame ES]$ curl "localhost:9200/gengxin/update/2?pretty"
{
"_index" : "gengxin",
"_type" : "update",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"name" : "wangbaoqiang",
"green_hat" :
}
}
是不是也还不错,比较容易实现。
补充一点tip,若update操作中doc和script关键字都出现了,那么doc将被忽略!
elasticsearch【更新】操作的更多相关文章
- elasticsearch更新操作问题
elasticsearch在更新的时候,是通过id进行管理的,我们在前台传入id操作,id如果与elasticsearch相同,则覆盖,否则新增一条记录.且elasticsearch中的插入一条记录和 ...
- ElasticSearch Index操作源码分析
ElasticSearch Index操作源码分析 本文记录ElasticSearch创建索引执行源码流程.从执行流程角度看一下创建索引会涉及到哪些服务(比如AllocationService.Mas ...
- es之对文档进行更新操作
5.7.1:更新整个文档 ES中并不存在所谓的更新操作,而是用新文档替换旧文档: 在内部,Elasticsearch已经标记旧文档为删除并添加了一个完整的新文档并建立索引.旧版本文档不会立即消失 ,但 ...
- es 查询更新操作
# es 查询更新操作# _*_ coding: utf-8 _*_ import time import datetime import pymysql from elasticsearch imp ...
- 使用Spring Data ElasticSearch+Jsoup操作集群数据存储
使用Spring Data ElasticSearch+Jsoup操作集群数据存储 1.使用Jsoup爬取京东商城的商品数据 1)获取商品名称.价格以及商品地址,并封装为一个Product对象,代码截 ...
- MongoDB 文档的更新操作
在MongoDB中,更新单个doc的操作是原子性的.默认情况下,如果一个update操作更新多个doc,那么对每个doc的更新是原子性的,但是对整个update 操作而言,不是原子性的,可能存在前面的 ...
- 关于SubSonic3.0插件使用Json反序列化获得的实体进行更新操作时,只能执行添加而不能执行修改(编辑)操作的处理
由于目前开发的项目使用云计算技术,客户端只进行UI与相关事件的功能开发,而所有的计算与处理都放到了服务器端,客户端与数据库没有任何关联,所以服务器端与客户端使用我们自己开发的通讯加密方式进行,而具体的 ...
- java-int类型:int默认为0导致更新操作未赋值的情况下将值更新为0
日常开发中,做更新操作的时候的处理方法为:当这个字段有值则更新,没有值就不更新,在mybatis的xml中表现为: <!-- 修改记录,只修改只不为空的字段 --> <update ...
- 对oracle数据库进行增删改更新操作,executeUpdate()执行卡住了
原因是:oracle数据库更新数据后需要commit,不然会堵塞,就会卡住 那么每次调用executeUpdate()完后,数据库要自动commit才可以. 我的基类加了一下,注意红色字体部分代码: ...
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
随机推荐
- 如何低成本的打造HTC Vive虚拟演播室直播MR视频?
http://m.toutiao.com/i6298923859378700802/?tt_from=weixin&utm_campaign=client_share&from=gro ...
- Find a point on a 'line' between two Vector3
Find a point on a 'line' between two Vector3http://forum.unity3d.com/threads/find-a-point-on-a-line- ...
- css学习笔记 2
css中的简写: 颜色的简写有三种:十六进制的形式.rgb(a).颜色名称. 单位的省略:当属性值为0px时,可以简写为0. margin和padding的简写,不多说了. border的简写:bor ...
- Number plate recognition with Tensorflow
2015年5月 在此处 http://matthewearl.github.io/2016/05/06/cnn-anpr/#rd 寻觅出 使用TenserFlow的车牌号识别 技术. 感觉很有必要 ...
- 关于makefile
0 Makefile概述 -------------------------------------------------------------------------------- 什么是mak ...
- Lua知识备忘录
最近对Lua很感兴趣,以下是本阶段学习的总结,包含三部分,一部分是基础语法,一部分是扩展和解释器嵌入,最后一部分是Lua小练习. 知识涉及:Lua语言编程基础:Lua&C++:Lua扩展.嵌入 ...
- OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- 1172. Ship Routes
http://acm.timus.ru/problem.aspx?space=1&num=1172 水题DP 大整数直接上java 代码: import java.math.BigInte ...
- MySQLdb 1031 Error
Python import MySQLdb 有可能报:site-packages/pkg_resources.py:1031: UserWarning: /home/***/.python-eggs ...
- 2014年4月份第4周51Aspx源码发布详情
精灵豆会员管理系统源码 2014-4-21 [VS2010]功能介绍:精灵豆会员管理系统业务管理平台采用微软选进的C#语言开发,采用大型数据库,具有比较高的执行效率和高安全性.系统分为消费管理,会员 ...