赞
踩
因为使用的是ElasticsearchTemplate模板的方法,故性能上可能会有点差,哪位大神有更好的见解,可以交流一下哈。
java方法如下:
- @Autowired
- private ElasticsearchTemplate template;
-
- /**
- * 批量更新
- * @param request
- */
- public void updateQueryByField(Map<String,Map<String,String>> map) {
- List<UpdateQuery> queries = new ArrayList<>();
- Iterable<EsModel> list = esRepository.findAll();
- for(EsModel esmodel : list) {
- UpdateQuery query = new UpdateQuery();
- for (Map.Entry<String, Map<String,String>> entry : map.entrySet()) {
- String field = entry.getKey();
- String oldValue = "";
- String newValue = "";
- Map<String, String> value = (Map<String, String>) entry.getValue();
- for (Map.Entry<String, String> map2 : value.entrySet()) {
- oldValue = map2.getKey();
- newValue = map2.getValue();
- }
- String idOrCode = "ctx._source."+field;
- UpdateRequest updateRequest = new UpdateRequest();
- updateRequest.script(new Script(ScriptType.INLINE,
- "painless",
- "if("+idOrCode+"== "+"'"+oldValue+"'"+"){"+idOrCode+"= "+"'"+newValue+"'"+"}",
- Collections.emptyMap()));
- query.setUpdateRequest(updateRequest);
- query.setIndexName("item");
- query.setType("docs");
- query.setId(esmodel.getId());
- template.update(query);
- //queries.add(query);
- }
- }
- //template.bulkUpdate(queries);
- }
注释:EsModel为实体类可自行定义
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- @Document(indexName = "item",type = "docs", shards = 1, replicas = 0)
- public class EsModel{
- @Id
- private String id;
- @Field(type = FieldType.Text, analyzer = "ik_max_word")
- private String title; //标题
- @Field(type = FieldType.Text, analyzer = "ik_max_word") //不需要分词类型
- private String keyword;// 关键词
- }
测试类如下:
- @RequestMapping("updateQueryByField")
- public String updateQueryByField() {
-
- Map<String,Map<String,String>> map = new HashMap<String, Map<String,String>>();//key:字段名称,value:原值和待修改的值
- Map<String,String> mapValue = new HashMap<>();
- mapValue.put("张三1111", "张三111"); //key:原数据,value:待更新的新数据
- map.put("title", mapValue);
-
- Map<String,String> mapValue2 = new HashMap<>();
- mapValue2.put("王五1111", "王五111");
- map.put("keyword", mapValue2);
-
- esUtils.updateQueryByField(map);
- return "success";
- }
如果一次只更新一个字段的值,可以使用以下方法
- /**
- * 批量更新
- */
- public void updateQueryByField2() {
- List<UpdateQuery> queries = new ArrayList<>();
- Iterable<EsModel> list = esRepository.findAll();
- String idOrCode = "ctx._source."+"title";
- for(EsModel esmodel : list) {
- UpdateQuery query = new UpdateQuery();
- UpdateRequest updateRequest = new UpdateRequest();
- updateRequest.script(new Script(ScriptType.INLINE,
- "painless",
- "if("+idOrCode+"!= '张三111'){"+idOrCode+"= '张三111'}",
- Collections.emptyMap()));
- query.setUpdateRequest(updateRequest);
- query.setIndexName("bdms3");
- query.setType("docs");
- query.setId(esmodel.getId());
- queries.add(query);
- }
- template.bulkUpdate(queries);
- }
以上是java方法实现es的批量更新。此外也可以使用kibana语句进行更新:
- POST item/_update_by_query
- {
- "script": {
- "lang": "painless",
- "inline": "if(ctx._source.title== '张三111'){ctx._source.title= '张三1111'}"
- }
- }
期间也查阅了很多资料,可以参考如下博客:
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。