当前位置:   article > 正文

springboot-es 单条数据的修改与批量数据修改_springboot es ctx._source更改多个字段

springboot es ctx._source更改多个字段

一.单条:通过主键id,直接对修改字段进行操作即可,不用再附带已有值和修改字段一起修改。

  1. public void updateInfo(){
  2. String id="344364895434851641";
  3. OrderDetailEntity od= orderService.queryOrderDetailEntityById(id);
  4. if(od==null){
  5. System.out.println("od为null");
  6. }
  7. System.out.println("od "+od.getOrderNo()+" id:"+od.getId());
  8. // od.setMiAmount(new BigDecimal(0));
  9. //od.setPayMethod(7);
  10. // od.setPayMoney(new BigDecimal(1.5));
  11. //341224197805034914
  12. od.setIdCard("341224197805034914");
  13. od.setRegisterTime(new Date());
  14. od.setAnalysisFlag(1);
  15. orderService.add(od);
  16. }
  17. /**
  18. * 添加
  19. * @param oe
  20. */
  21. public void add(OrderDetailEntity oe){
  22. ordersSearchRepository.save(oe);
  23. }

二。批量修改:使用主键更新,携带修改的字段即可

  1. public void batchUpdataData(){
  2. List<OrderDetailEntity> orderList=new ArrayList<OrderDetailEntity>();
  3. OrderDetailEntity ot1=new OrderDetailEntity();
  4. ot1.setOrderNo("344363949099720272");
  5. ot1.setIdCard("210204199303306485");
  6. ot1.setRegisterTime(new Date());
  7. ot1.setAnalysisFlag(1);
  8. orderList.add(ot1);
  9. OrderDetailEntity ot2=new OrderDetailEntity();
  10. ot2.setOrderNo("344371376360840270");
  11. ot2.setIdCard("500222199801010320");
  12. ot2.setRegisterTime(new Date());
  13. ot2.setAnalysisFlag(1);
  14. orderList.add(ot2);
  15. OrderDetailEntity ot3=new OrderDetailEntity();
  16. ot3.setOrderNo("344337535029511645");
  17. ot3.setIdCard("210202199505250720");//344337535029511645
  18. ot3.setRegisterTime(new Date());
  19. ot3.setAnalysisFlag(1);
  20. orderList.add(ot3);
  21. orderService.bulkUpdateData(orderList);
  22. }
  23. /**
  24. * 批量修改
  25. * @param orderNoList
  26. */
  27. @Override
  28. public void bulkUpdateData(List<OrderDetailEntity> orderNoList) {
  29. List<UpdateQuery> updateList = new ArrayList<UpdateQuery>();
  30. for(OrderDetailEntity orderDetailEntity:orderNoList){
  31. IndexRequest indexRequest = new IndexRequest();
  32. Map<String,Object> map=new HashMap<String,Object>();
  33. map.put("idCard",orderDetailEntity.getIdCard());
  34. map.put("registerTime",new Date().getTime());
  35. map.put("analysisFlag", orderDetailEntity.getAnalysisFlag());
  36. System.out.println("orderno:"+orderDetailEntity.getOrderNo());
  37. //这里只能用map,不能json串
  38. indexRequest.source(map);
  39. UpdateQuery updateQuery = new UpdateQueryBuilder().withId(orderDetailEntity.getOrderNo())
  40. .withClass(OrderDetailEntity.class).withIndexRequest(indexRequest).build();
  41. updateList.add(updateQuery);
  42. }
  43. // when
  44. elasticsearchTemplate.bulkUpdate(updateList);
  45. elasticsearchTemplate.refresh(OrderDetailEntity.class);
  46. System.out.println("修改完成!!!");
  47. }
  48. private <T> Map<String,Object> beanToMap(T bean){
  49. Map<String, Object> map = new HashMap<String,Object>();;
  50. if (null != bean){
  51. BeanMap beanMap = BeanMap.create(bean);
  52. for (Object key : beanMap.keySet()){
  53. map.put(key+"",beanMap.get(key));
  54. }
  55. }
  56. return map;
  57. }

 

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

闽ICP备14008679号