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 ...
随机推荐
- 修改内联CSS(点击按钮连续改变文字大小、位置,.animate()方法)
$(document).ready(function(){ $('#swticher-large').click(function(){ var $ ...
- spring.xml中的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 自定义N维空间数组
class Space : IEnumerable<Space> { public object Filler { get { return filler ?? (filler = Top ...
- spring随手笔记4:ref的属性
1.local属性 引用在同一个xml的bean 只能引用bean的id <bean id="HelloWord" class="com.ltf ...
- guava学习--File1
ByteSource:表示一个可读的字节.通常情况下,我们期望的字节来源是一个文件,但它也可以从一个字节数组读取字节. File f1 = new File("D:\\test2.txt&q ...
- PHP过滤外部链接及外部图片 添加rel="nofollow"属性
原来站内很多文章都是摘录的外部文章,文章里很多链接要么是时间久了失效了,要么就是一些测试的网址,如:http://localhost/ 之类的,链接多了的话,就形成站内很多死链接,这对SEO优化是很不 ...
- Halcon 10.0 Sample:完整性检查(圆形)
* ball.hdev: Inspection of Ball Bonding * 球接合检查 Comment Time:// *核心思想:.白色区域用作自动ROI,黑色区域是目标 * .Openin ...
- 【56测试】【字符串】【dp】【记忆化搜索】【数论】
第一题:神秘大门 大意: 两个字符串A,B,按字典序最大的顺序输出B 的每个字符在A 中的位置,如果B不全在A中,输出No,否则Yes. 解: 这道题就是一遍的扫描,因为要按字典序最大的输出,所以从后 ...
- 收集Github上的iOS控件和开发资料
文章来源:http://www.mobile-open.com/2015/85017.html 动画 awesome-ios-animation 收集了iOS平台下比较主流炫酷的几款动画框架 RCTR ...
- java的eclipse操作和常用类Object的使用
1.eclipse的快捷键: (1)alt + / 内容辅助. 如:main+alt + / 会出现完整的main方法. syso+alt+ / 会输出. 如编写某个方法时,只需写入方法名 + a ...