记录一次es商品门店查询,关键字SearchSourceBuilder

SearchSourceBuilder 使用说明
参考:https://www.cnblogs.com/reycg-blog/p/9946821.html

@Override
public List<String> baseProductShopNotExistEsSearch(ReqBaseProductShopPageEsQueryVO reqVO) {
    try {
        SearchRequest searchRequest = new SearchRequest(EsOperateTables.BASE_PRODUCT_SHOP_SAP.getAlias());
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.fetchSource("shop_code", null);//过滤门店编码为null数据
        Collection<String> ids = reqVO.getIds();
        if (CollectionUtils.isNotEmpty(ids)) {
            ids=ids.stream().distinct().collect(Collectors.toList());
            sourceBuilder.query(QueryBuilders.termsQuery(EsUtils.ID, ids));//根据ID查询
        }else{
            throw new RRException("请求参数不能为空");
        }
        sourceBuilder.timeout(TimeValue.timeValueMinutes(2L));
        sourceBuilder.size(new Long(ConstantsUtils.QueryPartams.MAX_RESULT_COUNT).intValue());//分页量
        sourceBuilder.sort(EsUtils.ID, SortOrder.DESC);//排序
        searchRequest.source(sourceBuilder);
        //scroll 分页搜索
        Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
        searchRequest.scroll(scroll);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        String scrollId = searchResponse.getScrollId();
        SearchHit[] searchHits = searchResponse.getHits().getHits();
        List<String> mapList = Lists.newArrayList();
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        while (true) {
            if(StringUtils.isNotBlank(scrollId)){
                clearScrollRequest.addScrollId(scrollId);
            }
            if(searchHits == null || searchHits.length == 0){
                break;
            }
            Arrays.stream(searchHits).forEach(hit -> mapList.add(hit.getId()));
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scroll = new Scroll(TimeValue.timeValueMinutes(1L));
            scrollRequest.scroll(scroll);
            searchResponse = restHighLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
            scrollId = searchResponse.getScrollId();
            searchHits = searchResponse.getHits().getHits();
        }
        ClearScrollResponse clearScrollResponse = restHighLevelClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
        boolean succeeded = clearScrollResponse.isSucceeded();
        if(!succeeded){
            throw new RRException("清除es游标失败");
        }
        if(CollectionUtils.isNotEmpty(mapList)){
            return Lists.newArrayList(CollectionUtils.disjunction(ids,mapList));
        }else{
            return Lists.newArrayList(ids);
        }

    } catch (Exception e) {
       log.error(e.getMessage());
        throw new RRException(e.getMessage());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值