ES(Elastic Search)update操作设置无 docment时进行insert
最近使用一套数据加工中间工具,查看es操作中的update操作。其中方法命名为updateOrInsert。但是没发现代码中有ES的insert方法调用。于是仔细分析了代码逻辑。
经过一路追溯,直至ES java客户端请求发送代码。没找到insert相关内容。
于是到官网查看究竟,可官网对 java Client相关说明比较少。查看不到具体api的说明。于是回到代码调用处:
String jsonText = jsonBuild.endObject().string();
UpdateRequest request = (UpdateRequest)esClient.prepareUpdate(xxx.getDatabase(), xxx.getTable(), docId).setDoc(jsonText).setDetectNoop(true).setDocAsUpsert(true).setRetryOnConflict(this.retryOnConflict).request();
esClient.update(request).get();
代码中属于链式调用,由于太长没有换行,竟然没看到后边的setDetectNoop,setDocAsUpsert参数的调用,于是思考,javaClient只是封装和转换了调用请求,于是再回到官网查看Document APIs,找到update操作的说明,就有了下边关于 Detecting Noop Updates 以及 Upserts说明:
Detecting noop updatesedit
If
docis specified its value is merged with the existing_source. By default the document is only reindexed if the new_sourcefield differs from the old. Settingdetect_nooptofalsewill cause Elasticsearch to always update the document even if it hasn’t changed. For example:curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
"name" : "new_name"
},
"detect_noop": false
}'
上边这段的意思是当更新的文档发生变化时进行更新,如果为fasle,则始终更新。
Upsertsedit
If the document does not already exist, the contents of the
upsertelement will be inserted as a new document. If the document does exist, then thescriptwill be executed instead:curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script" : {
"inline": "ctx._source.counter += count",
"params" : {
"count" : 4
}
},
"upsert" : {
"counter" : 1
}
}'
scripted_upserteditIf you would like your script to run regardless of whether the document exists or not — i.e. the script handles initializing the document instead of the
upsertelement — then setscripted_upserttotrue: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" : {}
}'
doc_as_upserteditInstead of sending a partial
docplus anupsertdoc, settingdoc_as_upserttotruewill use the contents ofdocas theupsertvalue:curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
"name" : "new_name"
},
"doc_as_upsert" : true
}'
上边描述:upsert参数的使用,有三种方式:
- 指定upsert内容
- 指定打开脚本upsert开关使用脚本处理upsert
- 使用文档内容做为upsert参数,则打开 doc_as_upsert。显然我们上边所说的javaClient调用中就是使用的 doc_as_upsert,这样当文档不存在时候,就会将传递过来的文档内容insert进去。达到update or Insert 目的。
因此,对于ES java Clent使用不熟的完全可以参照 api命名查找官网的 api说明,java客户端是用java语言对其进行了封装。仔细阅读便知道调用代码逻辑的含义了。仅此记录,为不熟悉ES的其他使用者 引个路子。
官网内容链接:https://www.elastic.co/guide/en/elasticsearch/reference/2.1/docs-update.html#upserts
可以根据自己使用的ES版本进行切换查看。
ES(Elastic Search)update操作设置无 docment时进行insert的更多相关文章
- elastic search安装与本地测试
elastic search安装与本地测试 elastic search是一个全文搜索引擎 教程: 综合:http://www.ruanyifeng.com/blog/2017/08/elastics ...
- Update操作浅析,一定是先Delete再Insert吗?
Update操作一定是先Delete再Insert吗? Update在数据库中的执行是怎么样的?“Update操作是先把数据删除,然后再插入数据”.在网上看了很多也都是这么认为的.但在查阅到一些不同看 ...
- Elastic Search 小调研
一.概况: Elastic Search 是一个基于Apache Lucene™工具包的开源搜索引擎.无论在开源还是专有领域,Lucene 可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库 ...
- Elastic Search快速上手(2):将数据存入ES
前言 在上手使用前,需要先了解一些基本的概念. 推荐 可以到 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htm ...
- elastic search book [ ElasticSearch book es book]
谁在使用ELK 维基百科, github都使用 ELK (ElasticSearch es book) ElasticSearch入门 Elasticsearch入门,这一篇就够了==>http ...
- Elastic Search中Document的CRUD操作
一. 新增Document在索引中增加文档.在index中增加document.ES有自动识别机制.如果增加的document对应的index不存在.自动创建,如果index存在,type不存在自动创 ...
- elastic search(以下简称es)
参考博客园https://www.cnblogs.com/Neeo/p/10304892.html#more 如何学好elasticsearch 除了万能的百度和Google 之外,我们还有一些其他的 ...
- Elastic Search操作入门
前言 Elastic Search是基于Lucene这个非常成熟的索引方案,另加上一些分布式的实现:集群,sharding,replication等.具体可以参考我同事写的文章. 本文主要介绍ES入门 ...
- elastic search&logstash&kibana 学习历程(一)es基础环境的搭建
elastic search 6.1.x 常用框架: 1.Lucene Apache下面的一个开源项目,高性能的.可扩展的工具库,提供搜索的基本架构: 如果开发人员需用使用的话,需用自己进行开发,成本 ...
随机推荐
- java中多线程执行时,为何调用的是start()方法而不是run()方法
Thead类中start()方法和run()方法的区别 1,start()用来启动一个线程,当调用start()方法时,系统才会开启一个线程,通过Thead类中start()方法来启动的线程处于就绪状 ...
- SpringSecurity自定义UsernamePasswordAuthenticationFilter
UsernamePasswordAuthenticationFilter介绍 UsernamePasswordAuthenticationFilter是AbstractAuthenticationPr ...
- Redis小白入门系列
一.从NoSQL说起 NoSQL 是 Not only SQL 的缩写,大意为"不只是SQL",说明这项技术是传统关系型数据库的补充而非替代.在整个NoSQL技术栈中 MemCac ...
- Winform应用程序简介
1.winform应用程序是一种智能客户端技术,我们可以使用winform应用程序帮助我们获得信息或传输信息等. WPF技术——XAML美化界面. 2. F4快速属性 (1)Name:在后台要获得前 ...
- 任务分线程实现(java)
1.创建一个类,用户存储信息 public class Users { private String userid; private String username; public Users() { ...
- elasticsearch集群扩容和容灾
elasticsearch专栏:https://www.cnblogs.com/hello-shf/category/1550315.html 一.集群健康 Elasticsearch 的集群监控信息 ...
- Java门面模式
一.简介 隐藏系统的复杂性,对外提供统一的访问入口,外部系统访问只通过此暴露出的统一接口访问.是一种结构型模式.封装子系统接口的复杂性,提供统一的对外接口,能够使子系统更加简单的被使用. 二.结构及使 ...
- C++虚函数表和对象存储
C++虚函数表和对象存储 C++中的虚函数实现了多态的机制,也就是用父类型指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数,这种技术可以让父类的指针有"多种形态",这 ...
- mysql创建表时字段类型选择与优化
一.选择原则 1.应该尽量使用可以正确存储数据的最小字段类型 2.选用简单的数据类型,例如:一个是尽量用mysql内置的字段类型来存储日期和时间:另一个存储IP地址尽量用整型:能用整型的尽量不用字符串 ...
- Hive之行转列与列转行
行转列 原始数据: 需求: 把星座和血型一样的人归类到一起.结果如下: 射手座,A 大海|凤姐 白羊座,A 孙悟空|猪八戒 白羊座,B 宋宋 实现: vi person_info.txt 孙悟空 白羊 ...