一、修改部分字段By  UpdateRequest

UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("index");
updateRequest.type("type");
updateRequest.id("1");
updateRequest.doc(jsonBuilder().startObject().field("gender", "male").endObject());
client.update(updateRequest).get();

client.prepareUpdate("ttl", "doc", "1")
.setScript(new Script("ctx._source.gender = \"male\"" , ScriptService.ScriptType.INLINE, null, null))
.get();

client.prepareUpdate("ttl", "doc", "1")
.setDoc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject())
.get();

UpdateRequest updateRequest = new UpdateRequest("ttl", "doc", "1").script(new Script("ctx._source.gender = \"male\""));
client.update(updateRequest).get();

UpdateRequest updateRequest = new UpdateRequest("index", "type", "1").doc(jsonBuilder().startObject().field("gender", "male").endObject());
client.update(updateRequest).get();

二、乐观锁方式 指定Version 版本更新(es 5)

RefreshPolicy.IMMEDIATE 更新策略为立即更新,ElasticSearch 实际上是伪实时的,所有分片之间默认1s同步更新

IndexResponse response = client.prepareIndex().setRefreshPolicy(RefreshPolicy.IMMEDIATE).setIndex(indexName)
.setType(tableName).setId(statid).setVersion(version).setSource(jsondata).execute().actionGet();

三、批量更新(es 5)

/**
* 批量索引
*
* @param indexName
* @param tableName
* @param maps
*/
public void bulkIndex(String indexName, String tableName, Map<String, String> maps) {

if (maps == null || maps.isEmpty()) {
return;
}

logger.info("[[title=bulkIndex]] start");
BulkRequestBuilder bulkRequest = client.prepareBulk().setRefreshPolicy(RefreshPolicy.IMMEDIATE);

int i = 1;
for (Map.Entry<String, String> entry : maps.entrySet()) {

bulkRequest.add(client.prepareIndex().setIndex(indexName).setType(tableName).setId(entry.getKey())
.setSource(entry.getValue()));

if (i % 100 == 0) {
bulkRequest.execute().actionGet();
}
i++;
}

bulkRequest.execute().actionGet();

String fieldString = SensitiveInfoMask.maskJsonSensitiveField(JSON.toJSONString(maps),
"(contactMobile|clientName|contactName|idCardNo)");
logger.info("[[title=bulkIndex]]批量索引信息={}", fieldString);
logger.info("[[title=bulkIndex]] end");
}

Upsertedit
There is also support for upsert. If the document does not exist, the content of the upsert element will be used to index the fresh doc:

IndexRequest indexRequest = new IndexRequest("index", "type", "1")
.source(jsonBuilder()
.startObject()
.field("name", "Joe Smith")
.field("gender", "male")
.endObject());
UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
.doc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject())
.upsert(indexRequest);
client.update(updateRequest).get();

If the document does not exist, the one in indexRequest will be added

If the document index/type/1 already exists, we will have after this operation a document like:

{
"name" : "Joe Dalton",
"gender": "male"
}

This field is added by the update request

If it does not exist, we will have a new document:

{
"name" : "Joe Smith",
"gender": "male"
}



转载于:https://www.cnblogs.com/xiaocandou/p/8118638.html

ElasticSearch(六) Update API相关推荐

  1. python elasticsearch 提示 ‘update‘ API and will be removed

    环境: Elasticsearch 版本:7.10.1 elasticsearch-analysis-ik 版本:7.10.1 Elasticsearch 操作的 Python 库版本:7.16.1 ...

  2. elasticsearch使用指南之Elasticsearch Document Index API详解、原理与示例

    作者介绍:<RocketMQ技术内幕>作者,中间件兴趣圈微信公众号维护者. 本节将重点介绍ElasticSearch Doucment Index API(新增索引). 从上节可知,Ela ...

  3. es Update API

    2019独角兽企业重金招聘Python工程师标准>>> es Update API 博客分类: 搜索引擎,爬虫 The update API allows to update a d ...

  4. elasticsearch中的API

    elasticsearch中的API es中的API按照大类分为下面几种: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作 查看A ...

  5. Elasticsearch——使用Java API实现ES中的索引、映射、文档操作

    文章目录: 1.开篇 2.案例详解 2.1 创建ES客户端:完成与ES服务端的连接 2.2 创建索引 2.3 查看索引 2.4 删除索引 2.5 创建文档 2.6 修改文档 2.7 查看文档 2.8 ...

  6. ElasticSearch之Java Api 测试

    增加Maven依赖 <dependency><groupId>org.elasticsearch</groupId><artifactId>elasti ...

  7. ElasticSearch 使用Java Api访问集群

    ElasticSearch 使用Java Api访问集群 1.创建maven工程导入pom依赖 <dependencies><dependency><groupId> ...

  8. 【Elasticsearch】Elasticsearch通过reroute api 重新分配分片

    1.概述 转载:[重新分配分片]Elasticsearch通过reroute api重新分配分片 elasticsearch可以通过reroute api来手动进行索引分片的分配. 不过要想完全手动, ...

  9. 【Elasticsearch】使用 Elasticsearch Freeze index API 创建冻结索引

    1.概述 首先官网已经写得很清楚了 使用 Elasticsearch Freeze index API 创建冻结索引 下面说说怎么获取冻结的索引 GET _cat/indices 执行get后,获取所 ...

最新文章

  1. C4D样条曲线建模大师班 Cinema 4D MasterClass: Master Modelling using Splines
  2. python读取excel一列-Python从Excel中读取日期一列的方法
  3. [转载红鱼儿]kbmmw 开发点滴:kbmMW:Unknown property:indexes
  4. 卡尔曼滤波器原理和matlab实现
  5. 腾讯“死守”版权,网易云“再强”社区,各筑“孤峰”对战在线音乐下半场
  6. iOS之UI--涂鸦画板实例 (有待更新)
  7. 自定义JSP中的Taglib标签之四自定义标签中的Function函数
  8. es6(五):函数的扩展
  9. 博客迁移至“零一积流|it-refer.com”
  10. 教你轻松截获 Selenium 中的 Ajax 数据
  11. Dubbo学习总结(9)——Apache Dubbo Roadmap 2019
  12. 文件夹选择对话框 JS实现(转)
  13. Java 11 中 11 个不为人知的瑰宝
  14. SQL Server 2005中的CLR(2)
  15. python入门程序例子_Python 爬虫从入门到放弃(11 个有趣的 Python 爬虫例子)
  16. NPOI导出 The maximum column width for an individual cell is 255 characters
  17. Hadoop入门集群环境搭建
  18. python选择时间窗口_对pandas中时间窗函数rolling的使用详解
  19. 电脑没有长截图功能,一招即可截下来!如何用电脑做长截图
  20. 企业视频直播如何嵌入企业微信公众号?

热门文章

  1. 2022-2028年中国专用化学品行业投资分析及前景预测报告
  2. kali2020进入单模式_蚂蚁集团技术专家山丘:性能优化的常见模式及趋势
  3. docfetcher,filelocator使用
  4. jieba分词流程及部分源码解读(一)
  5. ONNX 实时graph优化方法
  6. android中getMeasuredHeigh()为0的问题
  7. 客快物流大数据项目(八):Docker的安装和启动
  8. Laravel Dcat-admin 详情页多栏布局开发
  9. python 字符串拼接
  10. Android ListView item设置分割线以及分割线宽度