es~批量更新bulkIndex和bulkUpdate
重要说明
- bulkIndex 批量索引文档更新,文档不存在就建立,存在就覆盖,
如果文档原来有3个字段,批量更新时有2个字段,在bulkIndex之后,它最后会变成最新的2个字段
- bulkUpdate 批量更新文档字段,
如果文档原来有3个字段,批量更新时有2个字段,结果还是3个字段
依赖添加
<properties>
<fastjson.version>1.2.68</fastjson.version>
<elasticsearch.high.version>6.5.4</elasticsearch.high.version>
<elasticsearch.client.version>6.8.7</elasticsearch.client.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.high.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.client.version}</version>
</dependency>
</dependencies>
bulkIndex实例
/**
* 批量替换,不存在就建立,存在就替换,原字段会删除,新字段会添加
*
* @throws JsonProcessingException
*/
@Test
public void bulkIndex() throws JsonProcessingException {
List<IndexQuery> queries = new ArrayList<>();
IndexQuery indexQuery = new IndexQuery();
indexQuery.setId("552625903956398080");
Map<String, Object> sourceMap = new HashMap<>();
sourceMap.put("name", "xx1");
indexQuery.setSource(new ObjectMapper().writeValueAsString(sourceMap));
indexQuery.setType("esdto");
indexQuery.setIndexName("esdto");
queries.add(indexQuery);
indexQuery = new IndexQuery();
indexQuery.setId("552306605039816704");
sourceMap = new HashMap<>();
sourceMap.put("name", "xx2");
indexQuery.setSource(new ObjectMapper().writeValueAsString(sourceMap));
indexQuery.setType("esdto");
indexQuery.setIndexName("esdto");
queries.add(indexQuery);
elasticsearchTemplate.bulkIndex(queries,
BulkOptions.builder().withTimeout(TimeValue.MINUS_ONE).build());
}
bulkUpdate实例
/**
* 批量更新某些字段.
*
* @throws JsonProcessingException
*/
@Test
public void bulkUpdate() throws JsonProcessingException {
List<UpdateQuery> updateQueries = new ArrayList<>();
Map<String, Object> sourceMap = new HashMap<>();
sourceMap.put("name", "xx3");
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.doc(sourceMap);
UpdateQuery updateQuery = new UpdateQueryBuilder()
.withId("552629576220545024")
.withIndexName("esdto")
.withType("esdto")
.withUpdateRequest(updateRequest)
.build();
updateQueries.add(updateQuery);
sourceMap = new HashMap<>();
sourceMap.put("name", "xx4");
updateRequest = new UpdateRequest();
updateRequest.doc(sourceMap);
updateQuery = new UpdateQueryBuilder()
.withId("552629636035514368")
.withIndexName("esdto")
.withType("esdto")
.withUpdateRequest(updateRequest)
.build();
updateQueries.add(updateQuery);
elasticsearchTemplate.bulkUpdate(updateQueries);
}
合集:
elasticsearch
分类:
elasticsearch
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2019-06-24 TOTP算法实现二步验证
2016-06-24 Lind.DDD.UoW~方法回调完成原子化操作
2015-06-24 MongoDB学习笔记~管道中的分组实现group+distinct
2013-06-24 品味人生~咖啡与程序员
2012-06-24 说说设计模式~简单工厂模式(Factory)
2012-06-24 说说设计模式~单件模式(Singleton)
2011-06-24 JS函数的原型及对象,对象方法,对象属性的学习