最近使用一套数据加工中间工具,查看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 doc is specified its value is merged with the existing _source. By default the document is only reindexed if the new _source field differs from the old. Setting detect_noop to false will 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 upsert element will be inserted as a new document. If the document does exist, then the script will 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_upsertedit

If 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 upsert element — then set scripted_upsert to 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" : {}
}'

doc_as_upsertedit

Instead of sending a partial doc plus an upsert doc, setting doc_as_upsert to true will use the contents of doc as the upsert value:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
"name" : "new_name"
},
"doc_as_upsert" : true
}'

  上边描述:upsert参数的使用,有三种方式:

  1. 指定upsert内容
  2. 指定打开脚本upsert开关使用脚本处理upsert
  3. 使用文档内容做为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的更多相关文章

  1. elastic search安装与本地测试

    elastic search安装与本地测试 elastic search是一个全文搜索引擎 教程: 综合:http://www.ruanyifeng.com/blog/2017/08/elastics ...

  2. Update操作浅析,一定是先Delete再Insert吗?

    Update操作一定是先Delete再Insert吗? Update在数据库中的执行是怎么样的?“Update操作是先把数据删除,然后再插入数据”.在网上看了很多也都是这么认为的.但在查阅到一些不同看 ...

  3. Elastic Search 小调研

    一.概况: Elastic Search 是一个基于Apache Lucene™工具包的开源搜索引擎.无论在开源还是专有领域,Lucene 可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库 ...

  4. Elastic Search快速上手(2):将数据存入ES

    前言 在上手使用前,需要先了解一些基本的概念. 推荐 可以到 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htm ...

  5. elastic search book [ ElasticSearch book es book]

    谁在使用ELK 维基百科, github都使用 ELK (ElasticSearch es book) ElasticSearch入门 Elasticsearch入门,这一篇就够了==>http ...

  6. Elastic Search中Document的CRUD操作

    一. 新增Document在索引中增加文档.在index中增加document.ES有自动识别机制.如果增加的document对应的index不存在.自动创建,如果index存在,type不存在自动创 ...

  7. elastic search(以下简称es)

    参考博客园https://www.cnblogs.com/Neeo/p/10304892.html#more 如何学好elasticsearch 除了万能的百度和Google 之外,我们还有一些其他的 ...

  8. Elastic Search操作入门

    前言 Elastic Search是基于Lucene这个非常成熟的索引方案,另加上一些分布式的实现:集群,sharding,replication等.具体可以参考我同事写的文章. 本文主要介绍ES入门 ...

  9. elastic search&logstash&kibana 学习历程(一)es基础环境的搭建

    elastic search 6.1.x 常用框架: 1.Lucene Apache下面的一个开源项目,高性能的.可扩展的工具库,提供搜索的基本架构: 如果开发人员需用使用的话,需用自己进行开发,成本 ...

随机推荐

  1. 模板汇总——快读 fread

    struct FastIO { ; int wpos; char wbuf[S]; FastIO() : wpos() { } inline int xchar() { static char buf ...

  2. Springboot源码分析之Spring循环依赖揭秘

    摘要: 若你是一个有经验的程序员,那你在开发中必然碰到过这种现象:事务不生效.或许刚说到这,有的小伙伴就会大惊失色了.Spring不是解决了循环依赖问题吗,它是怎么又会发生循环依赖的呢?,接下来就让我 ...

  3. js中的this介绍

    今天跟大家一起简单的来了解一下js中一个有趣的东西,this. 在js中我们用面向对象的思想去编写的时候,各个模块之间的变量就不那么容易获取的到了,当然也可以通过闭包的方式拿到其他函数的变量,如果说每 ...

  4. TypeScript + React + Redux 实战简单天气APP全套完整项目

    下载链接:https://www.yinxiangit.com/171.html 目录: 从面向过程的js到面向对象的js,让web前端更加高大尚.让你的前端步步日上,紧跟技术发展的前沿.让你构建更加 ...

  5. SpringCloud超简单的入门(1)--一些简单的介绍

    简介 简单来说,springcloud的就是由一组springboot应用(服务)组成,相互之间通过REST等方式进行通信. 两个springboot应用,其中一个作为服务提供者,一个作为服务消费者, ...

  6. 解决The mysql extension is deprecated and will be removed

    今天打开网站突然发现报错:“PHP Deprecated:  mysql_connect(): The mysql extension is deprecated and will be remove ...

  7. Android的消息循环与Handler机制理解

    一.概念 1.事件驱动型 什么是事件驱动?就是有事了才去处理,没事就躺着不动.假如把用户点击按钮,滑动页面等这些都看作事件,事件产生后程序就执行相应的处理方法,就是属于事件驱动型. 2.消息循环 把需 ...

  8. cobbler高可用方案

    一.环境准备 主网IP 私网IP 主机名 角色 VIP 10.203.178.125 192.168.10.2 cnsz22VLK12919 主 10.203.178.137,192.168.10.1 ...

  9. J2EE简单的分页器

    J2EE项目特别是后台管理,或者一部分前台展示数据比较多,通常需要分页把展示折叠在数据区内. 一般有几种方式来实现分页, 一是官方分页插件,二是自己写,三是网上找(类似于第一种) 这里就介绍第二种, ...

  10. springboot 定时器 Schdule

    定时器:定时启动任务,执行代码 1.在启动类中加入注解: 2.创建一个类,并且在这个类上加入注解:@Component 3.定义一个方法,在方法上加入注解:@Scheduled(cron=" ...