ElasticSearch(ES)JAVA批量更新批量插入——就一步

注:此Es处理方式是基于Spring-data 集成的

1. 批量更新

 

 public void allChangeCart(CartsSearchDTO cartsSearchDTO) {
        Assert.notNull(cartsSearchDTO, "对象不能为空!");
        UpdateByQueryRequestBuilder updateByQueryRequestBuilder = UpdateByQueryAction.INSTANCE.newRequestBuilder(elasticsearchTemplate.getClient());
        updateByQueryRequestBuilder.source("shop_cart_index");
        updateByQueryRequestBuilder.filter(QueryBuilders.termQuery("goodsId", cartsSearchDTO.getGoodsId()));
        updateByQueryRequestBuilder.script(
                new Script("ctx._source.goodsShow="+cartsSearchDTO.getGoodsShow()
                        +";ctx._source.goodsPrice="+cartsSearchDTO.getGoodsPrice()
                        +";ctx._source.goodsImages='"+cartsSearchDTO.getGoodsImages()
                        +"';ctx._source.goodsState="+cartsSearchDTO.getGoodsState()
                        +";ctx._source.oldGoodsPrice="+cartsSearchDTO.getOldGoodsPrice()
                        +";ctx._source.goodsTransfeeCharge="+cartsSearchDTO.getGoodsTransfeeCharge()
                        +";ctx._source.isIntereFree="+cartsSearchDTO.getIsIntereFree()
                        +";ctx._source.goodsNowStorage="+cartsSearchDTO.getGoodsNowStorage()
                        +";ctx._source.goodsName='"+cartsSearchDTO.getGoodsName()+"';"));
        long count =updateByQueryRequestBuilder.get().getUpdated();
        log.info("更新了:" + count + "条");

    }

简要说明:

1.source 为索引名称

2.filter 为条件(不理解的同学 可以理解为sql的where 条件)

3.script 为更新内容 切记若为字符类型需要加单引号 “ ' ” 拼接

4.count 为受影响条数

2.批量插入:

    @Override
    public void batchSave(List goodsSpecSearchDTOList) {
        Assert.notNull(goodsSpecSearchDTOList, "新增的商品规格集合不能为空!");

        // 获取索引文档对象
        Document document = GoodsSpecSearchPO.class.getAnnotation(Document.class);

        // 判断索引是否存在
        if (!elasticsearchTemplate.indexExists(document.indexName())) {
            elasticsearchTemplate.createIndex(document.indexName());
        }
        List queries = new ArrayList<>();

        goodsSpecSearchDTOList.forEach(goodsSpecSearchDTO -> {
            GoodsSpecSearchPO goodsSpecSearchPO = new GoodsSpecSearchPO();
            IndexQuery indexQuery = new IndexQuery();
            try {
                GoodsSpecSearchConverter.convert(goodsSpecSearchDTO, goodsSpecSearchPO);
                indexQuery.setId(goodsSpecSearchDTO.getGoodsSpecId());
                indexQuery.setObject(goodsSpecSearchPO);
                indexQuery.setIndexName(document.indexName());
                indexQuery.setType(document.type());
                queries.add(indexQuery);
                log.info("新增商品规格ID:{}", goodsSpecSearchDTO.getGoodsSpecId());
            } catch (Exception e) {
                log.error("生成商品规格{}索引异常:{}", goodsSpecSearchDTO.getGoodsSpecId(), e);
            }
        });
        elasticsearchTemplate.bulkIndex(queries);
        elasticsearchTemplate.refresh(document.indexName());
        log.info("已新增{}条类型规格索引", goodsSpecSearchDTOList.size());
    }

 

简要说明:

先获取对象的反射属性以xml格式形式解析,利用索引条件(indexQuery)进行汇总集合的形式,最终以ES原生Template批量插入()(bulkIndex)并刷新索引(refresh)

以上解决方案有疑问或者优化点 欢迎与博主留言 一起探讨进步!

转载请说明出处  https://blog.csdn.net/PanZiHao_BeiJing  

你可能感兴趣的:(elasticsearch)