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);
}