核心代码文章来源:https://www.toymoban.com/news/detail-638199.html
// 页标
sourceBuilder.from((interfaceDTO.getPageIndex() - 1) * interfaceDTO.getPageSize());
// 页数
sourceBuilder.size(interfaceDTO.getPageSize());
// 获取超过1w条数据,需要加上track_total_hits: true ,不然只能显示出9999条
sourceBuilder.trackTotalHits(true);
分页查询示例文章来源地址https://www.toymoban.com/news/detail-638199.html
@Override
public RequestResult getRequestLogs(InterfaceDTO interfaceDTO) throws Exception {
Date queryRequestLogStartTime = DateUtil.toDate(interfaceDTO.getQueryRequestLogStartTime(), "yyyy-MM-dd HH:mm:ss");
Date queryRequestLogEndTime = DateUtil.toDate(interfaceDTO.getQueryRequestLogEndTime(), "yyyy-MM-dd HH:mm:ss");
List<Date> betweenDates = DateUtil.getBetweenDates(queryRequestLogStartTime, queryRequestLogEndTime);
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
if (StringUtil.isNotEmpty(interfaceDTO.getPartnerId())) {
queryBuilder.must(QueryBuilders.termQuery("partnerId", interfaceDTO.getPartnerId().toLowerCase()));
}
if (StringUtil.isNotEmpty(interfaceDTO.getAppId())) {
queryBuilder.must(QueryBuilders.termQuery("appId", interfaceDTO.getAppId().toLowerCase()));
}
if (interfaceDTO.getServiceId() != null) {
queryBuilder.must(QueryBuilders.termQuery("serviceId", interfaceDTO.getServiceId()));
}
if (interfaceDTO.getId() != null) {
queryBuilder.must(QueryBuilders.termQuery("interfaceId", interfaceDTO.getId()));
}
if (interfaceDTO.getResponseState() != null) {
queryBuilder.must(QueryBuilders.termQuery("responseState", interfaceDTO.getResponseState()));
}
if (interfaceDTO.getRequestDuration() != null) {
queryBuilder.must(QueryBuilders.rangeQuery("downStreamRequestDuration").gte(interfaceDTO.getRequestDuration()));
}
queryBuilder.must(QueryBuilders.rangeQuery("downStreamRequestTime").from(queryRequestLogStartTime.getTime()).to(queryRequestLogEndTime.getTime()));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder);
sourceBuilder.from((interfaceDTO.getPageIndex() - 1) * interfaceDTO.getPageSize());
sourceBuilder.size(interfaceDTO.getPageSize());
sourceBuilder.trackTotalHits(true);
sourceBuilder.sort("downStreamRequestTime", SortOrder.DESC);
sourceBuilder.fetchSource(new String[] {}, new String[] {});
SearchRequest searchRequest = new SearchRequest(getEsIndices(betweenDates));
searchRequest.indicesOptions(IndicesOptions.fromOptions(true, true, true, false));
searchRequest.types("_doc");
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = eslClient.search(searchRequest, ElasticsearchConfig.COMMON_OPTIONS);
SearchHits hits = searchResponse.getHits();
TotalHits totalHits = hits.getTotalHits();
SearchHit[] searchHits = hits.getHits();
PageUtil<InterfaceLogDTO> interfaceLogs = pageInterfaceLogs(totalHits, searchHits, interfaceDTO);
return RequestResult.success(interfaceLogs);
}
private PageUtil<InterfaceLogDTO> pageInterfaceLogs(TotalHits totalHits, SearchHit[] searchHits, InterfaceDTO interfaceDTO) {
List<InterfaceLogDTO> interfaceLogDTOS = new ArrayList<>();
for (SearchHit hit: searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
JSONObject jsonObject = new JSONObject();
jsonObject.putAll(sourceAsMap);
InterfaceLogDTO interfaceLogDTO = jsonObject.toJavaObject(InterfaceLogDTO.class);
interfaceLogDTOS.add(interfaceLogDTO);
}
int totalPage = Math.toIntExact(totalHits.value / interfaceDTO.getPageSize()) + 1;
return new PageUtil<>(interfaceDTO.getPageIndex(), interfaceDTO.getPageSize(), Long.valueOf(totalHits.value).intValue(), totalPage, interfaceLogDTOS);
}
到了这里,关于Java Elasticsearch分页查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!