当前位置:   article > 正文

spring data elasticsearch批量更新文档的某个字段_elasticsearchtemplate 批量更新

elasticsearchtemplate 批量更新

因为使用的是ElasticsearchTemplate模板的方法,故性能上可能会有点差,哪位大神有更好的见解,可以交流一下哈。

java方法如下:

  1. @Autowired
  2. private ElasticsearchTemplate template;
  3. /**
  4. * 批量更新
  5. * @param request
  6. */
  7. public void updateQueryByField(Map<String,Map<String,String>> map) {
  8. List<UpdateQuery> queries = new ArrayList<>();
  9. Iterable<EsModel> list = esRepository.findAll();
  10. for(EsModel esmodel : list) {
  11. UpdateQuery query = new UpdateQuery();
  12. for (Map.Entry<String, Map<String,String>> entry : map.entrySet()) {
  13. String field = entry.getKey();
  14. String oldValue = "";
  15. String newValue = "";
  16. Map<String, String> value = (Map<String, String>) entry.getValue();
  17. for (Map.Entry<String, String> map2 : value.entrySet()) {
  18. oldValue = map2.getKey();
  19. newValue = map2.getValue();
  20. }
  21. String idOrCode = "ctx._source."+field;
  22. UpdateRequest updateRequest = new UpdateRequest();
  23. updateRequest.script(new Script(ScriptType.INLINE,
  24. "painless",
  25. "if("+idOrCode+"== "+"'"+oldValue+"'"+"){"+idOrCode+"= "+"'"+newValue+"'"+"}",
  26. Collections.emptyMap()));
  27. query.setUpdateRequest(updateRequest);
  28. query.setIndexName("item");
  29. query.setType("docs");
  30. query.setId(esmodel.getId());
  31. template.update(query);
  32. //queries.add(query);
  33. }
  34. }
  35. //template.bulkUpdate(queries);
  36. }

注释:EsModel为实体类可自行定义

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @Document(indexName = "item",type = "docs", shards = 1, replicas = 0)
  5. public class EsModel{
  6. @Id
  7. private String id;
  8. @Field(type = FieldType.Text, analyzer = "ik_max_word")
  9. private String title; //标题
  10. @Field(type = FieldType.Text, analyzer = "ik_max_word") //不需要分词类型
  11. private String keyword;// 关键词
  12. }

测试类如下:

  1. @RequestMapping("updateQueryByField")
  2. public String updateQueryByField() {
  3. Map<String,Map<String,String>> map = new HashMap<String, Map<String,String>>();//key:字段名称,value:原值和待修改的值
  4. Map<String,String> mapValue = new HashMap<>();
  5. mapValue.put("张三1111", "张三111"); //key:原数据,value:待更新的新数据
  6. map.put("title", mapValue);
  7. Map<String,String> mapValue2 = new HashMap<>();
  8. mapValue2.put("王五1111", "王五111");
  9. map.put("keyword", mapValue2);
  10. esUtils.updateQueryByField(map);
  11. return "success";
  12. }

如果一次只更新一个字段的值,可以使用以下方法

  1. /**
  2. * 批量更新
  3. */
  4. public void updateQueryByField2() {
  5. List<UpdateQuery> queries = new ArrayList<>();
  6. Iterable<EsModel> list = esRepository.findAll();
  7. String idOrCode = "ctx._source."+"title";
  8. for(EsModel esmodel : list) {
  9. UpdateQuery query = new UpdateQuery();
  10. UpdateRequest updateRequest = new UpdateRequest();
  11. updateRequest.script(new Script(ScriptType.INLINE,
  12. "painless",
  13. "if("+idOrCode+"!= '张三111'){"+idOrCode+"= '张三111'}",
  14. Collections.emptyMap()));
  15. query.setUpdateRequest(updateRequest);
  16. query.setIndexName("bdms3");
  17. query.setType("docs");
  18. query.setId(esmodel.getId());
  19. queries.add(query);
  20. }
  21. template.bulkUpdate(queries);
  22. }

以上是java方法实现es的批量更新。此外也可以使用kibana语句进行更新:

  1. POST item/_update_by_query
  2. {
  3. "script": {
  4. "lang": "painless",
  5. "inline": "if(ctx._source.title== '张三111'){ctx._source.title= '张三1111'}"
  6. }
  7. }

期间也查阅了很多资料,可以参考如下博客:

https://blog.csdn.net/wwd0501/article/details/83274930

https://blog.csdn.net/zhou_shaowei/article/details/80079162

https://blog.csdn.net/zhou_shaowei/article/details/80078877

https://blog.csdn.net/pianpiannia/article/details/88944376

https://blog.csdn.net/wangh92/article/details/82854775

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/67926
推荐阅读
相关标签
  

闽ICP备14008679号