ES深度分页

1,

public List<EmpRosterVo> getDataList() {
        EmpRosterQueryDto pageQueryDto = new EmpRosterQueryDto();
        List<EmpRosterVo> items = new ArrayList<>();
        PageResult<EmpRosterVo> pageList;
        pageQueryDto.setPageSize(BATCH_SIZE);
        int pageNo = 1;
        Long lastEmpId = null;
        do {
            pageQueryDto.setPageNo(pageNo);
            pageList = getPageList(pageQueryDto, lastEmpId);
            if (null != pageList.getItems() && !pageList.getItems().isEmpty()) {
                log.info("getDataList,items.addAll, lastEmpId={}", lastEmpId);
                items.addAll(pageList.getItems());
                lastEmpId = (pageList.getItems().get(pageList.getItems().size() - 1)).getWork$empId();
            }
            log.info("getDataList,pageNo={}, getTotal ={}, lastEmpId={}", pageNo, pageList.getTotal(), lastEmpId);
        } while (pageNo * BATCH_SIZE < pageList.getTotal());
        return items;
    }

2,只查询第一页,通过字段empId排序,从大到小来查询

public PageResult<EmpRosterVo> getPageList(EmpRosterQueryDto basePage, Long lastEmpId) {
        String tenantId = SecurityUserUtil.getSecurityUserInfo().getTenantId();
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(getIndex(tenantId));

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.size(basePage.getPageSize());
        sourceBuilder.from((basePage.getPageNo() - 1) * basePage.getPageSize());
        sourceBuilder.sort(SortBuilders.fieldSort("work$empId").order(SortOrder.DESC));
        sourceBuilder.sort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC));

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.matchQuery("tenantId", tenantId));
        if(null != lastEmpId){
            boolQuery.must(QueryBuilders.rangeQuery("work$empId").lt(lastEmpId));
        }
        sourceBuilder.query(boolQuery);

        searchRequest.source(sourceBuilder);

        SearchHits hits = esDataRepository.searchRequest(searchRequest);
        if (null == hits) {
            return new PageResult(new ArrayList(), basePage.getPageNo(), basePage.getPageSize(), 0);
        }

        List<EmpRosterVo> empRosterVos = new ArrayList<>();
        hits.forEach(hit -> {
            Map<String, Object> dataMap = hit.getSourceAsMap();
            if (null == dataMap || dataMap.isEmpty()) {
                return;
            }
            empRosterVos.add(map2DataSimple(dataMap));
        });
        return new PageResult(empRosterVos, basePage.getPageNo(), basePage.getPageSize(), (int) hits.getTotalHits().value);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值