最近使用一套数据加工中间工具,查看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. Constructing Roads HDU 1102

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  2. vs 模板更新

    vs 模板更新,执行命令: dotnet new --install McMaster.DotNet.GlobalTool.Templates

  3. python爬虫-模拟微博登录

    微博模拟登录 这是本次爬取的网址:https://weibo.com/ 一.请求分析 找到登录的位置,填写用户名密码进行登录操作 看看这次请求响应的数据是什么 这是响应得到的数据,保存下来 exect ...

  4. ajax 发送json数据时为什么需要设置contentType: "application/json”

    1. ajax发送json数据时设置contentType: "application/json”和不设置时到底有什么区别? contentType: "application/j ...

  5. android Action中的data属性

    (2) 根据Action和Data匹配 <activity android:name=".MyActivityTwo" android:label="@string ...

  6. 计时器Chronometer

    布局文件很简单 <Chronometer android:id="@+id/test" android:layout_width="wrap_content&quo ...

  7. JSP获取网络IP地址

    import javax.servlet.http.HttpServletRequest; public class RemoteAddress { public static String getR ...

  8. jmeter性能分析

    1.硬件要求:包括客户端和服务端的cpu,mem,network,disk等,这些硬件设备必须满足性能测试的前提下,才能进行性能测试,否则得到的各项指标不一定是正确的 2.场景分析: 测试前的准备工作 ...

  9. 基于python-django框架的支付宝支付案例

    目录 @ 一. 开发前的准备 1. 必须了解的知识 SDK:软件开发工具包,可以为开发者提供快速开发的工具 沙箱环境:也就是测试环境 支付宝支付金额的精度:小数点后两位(面试) 支付宝用的什么加密方式 ...

  10. 给Xshell增加快速命令集

    一.显示快速命令栏 二.配置快速命令集 在工具中找到快速命令集 添加快速命令集 三.使用快速命令集