es分页查询

warning: 这篇文章距离上次修改已过257天,其中的内容可能已经有所变动。
// 页标
sourceBuilder.from((interfaceDTO.getPageIndex() - 1) * interfaceDTO.getPageSize());
// 页数
sourceBuilder.size(interfaceDTO.getPageSize());
// 获取超过1w条数据,需要加上track_total_hits: true ,不然只能显示出9999条
sourceBuilder.trackTotalHits(true);
@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);
}

0

版权声明 ▶ 本网站名称:我的学习笔记
▶ 本文链接:https://ooolo.net/article/464.html
▶ 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行核实删除。
▶ 转载本站文章需要遵守:商业转载请联系站长,非商业转载请注明出处!!!

none
最后修改于:2024年04月08日 18:43

添加新评论

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png