分页查询:
if (item.getItem().getEndDate() != null) {
item.getItem().setEndDate(item.getItem().getEndDate().plusDays(1));
}
Pageable pageable = PageRequest.of(item.getPageNo() - 1, item.getPageSize());
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
DrugManageQueryDto searchItem = item.getItem();
if(!StringUtils.isEmpty(searchItem.getWmsCode())) {
boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("wmsCode", searchItem.getWmsCode()));
}
if(!StringUtils.isEmpty(searchItem.getMaterielCode())) {
boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("materielCode", searchItem.getMaterielCode()));
}
if(searchItem.getStartDate() != null) {
long startDate = searchItem.getStartDate().atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
boolQueryBuilder.must(rangeQuery("createDate").gte(startDate));
}
if(searchItem.getEndDate() != null) {
long endDate = searchItem.getEndDate().atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
boolQueryBuilder.must(rangeQuery("createDate").lte(endDate));
}
nativeSearchQueryBuilder.withQuery(boolQueryBuilder).withSort(SortBuilders.fieldSort("createDate").order(SortOrder.DESC));
SearchQuery searchQuery = nativeSearchQueryBuilder.withPageable(pageable).build();
Page<StockOrderPickDetailEs> search = stockOrderPickDetailRepository.search(searchQuery);
PageInfos result;
if(search.getTotalElements() > 0) {
result = new PageInfos(item.getPageNo(), item.getPageSize(), search.getTotalElements(), search.getTotalPages(), search.getContent());
} else {
result = new PageInfos(item.getPageNo(), item.getPageSize(), 0, 0, new ArrayList<StockOrderPickDetailEs>());
}
return result;
不分页查询条件下所有的数据:
List<StockDrugCodeEs> result = new ArrayList<>();
Criteria criteria = new Criteria("epc").is(epc);
CriteriaQuery criteriaQuery = new CriteriaQuery(criteria);
CloseableIterator<StockDrugCodeEs> stockDrugCodes = elasticsearchTemplate.stream(criteriaQuery, StockDrugCodeEs.class);
while (stockDrugCodes.hasNext()) {
result.add(stockDrugCodes.next());
}
return result;