ES 全量数据查询

1、场景引入


queryForList默认查询10条每次,最多可设置为10000条,意味着该api最多只能查询10000条数数据
elasticsearchTemplate.queryForList(query, ComparisonPlatformGoodsItemSearchView.class);

类似同步之类的逻辑,es特定的业务索引几百万乃至上千万的数据,显然,仅仅通过elasticsearchTemplate.queryForList()是无法满足的。

2、解决方案:

第一种:通过scroll(类似mysql游标)

官方手册:https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.scroll

(注意:spring-data-elasticsearch不同版本api名称不一样,4.x.x和3.x.x存在不一样,诸如:searchScrollStart/scrollStart、searchScrollContinue/scrollContinue等)

代码示例:

public void createPurchaseActivityConfigurationGoodsSearchData(PurchaseActivityConfiguration paramBean) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("brandId", paramBean.getBrandId()))
                .must(QueryBuilders.matchQuery("comparisonCategoryId", paramBean.getCategoryId()));

        NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQuery).build();
        List<ComparisonPlatformGoodsItemSearchView> goodsItemViews = new ArrayList<>();


        ScrolledPage scroll = elasticsearchTemplate.startScroll(SCROLL_TIME, query, ComparisonPlatformGoodsItemSearchView.class);
        while (scroll.hasContent()) {
            goodsItemViews.addAll(scroll.getContent());
            scroll = elasticsearchTemplate.continueScroll(scroll.getScrollId(), SCROLL_TIME, ComparisonPlatformGoodsItemSearchView.class);
        }
        elasticsearchTemplate.clearScroll(scroll.getScrollId());



        if (ObjectUtil.isNotNull(paramBean.getCommissionRatioThreshold())) {
            purchaseHighSubsidyGoodsSearchRepository.saveAll(covertPurchaseHighSubsidyGoodsSearchView(goodsItemViews));
            log.info(">>>>>> createPurchaseHighSubsidyGoodsSearchData success");
        }
    }

 

第二种:通过Stream

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值