Java Api实现Elasticsearch的滚动查询

解决ES每次只能查询一万条数据的问题

java 复制代码
	@Override
    public List<ESHandleDto> getVisitorsNum(String startTime, String endTime, String schoolName, String typeFunction) throws IOException {
        List<ESHandleDto> esHandleDtos = new ArrayList<>();
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(ElasticEnum.FUNCTIONLOG_INDEX.getValue());

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (StringUtils.hasText(schoolName)) {
            boolQueryBuilder.must(QueryBuilders.termQuery("schoolName.keyword", schoolName));
        }
        if (StringUtils.hasText(typeFunction)) {
            boolQueryBuilder.must(QueryBuilders.termQuery("typeFunction.keyword", typeFunction));
        }
        if (StringUtils.hasText(startTime) && StringUtils.hasText(endTime)) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("createDate").gte(startTime + "T00:00:00.000Z").lte(endTime + "T23:59:59.000Z"));
        }
        BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery();
        shouldQuery.should().add(QueryBuilders.termQuery("description.keyword", "查询学生信息表"));
        shouldQuery.should().add(QueryBuilders.termQuery("description.keyword", "获取学校访客数据"));
        boolQueryBuilder.must(shouldQuery);

        sourceBuilder.sort("_doc", SortOrder.DESC);
        sourceBuilder.size(10000);
        sourceBuilder.query(boolQueryBuilder);
        searchRequest.scroll(TimeValue.timeValueMinutes(1));
        searchRequest.source(sourceBuilder);
//        System.out.println(sourceBuilder.toString());
        SearchResponse searchResponse = null;
        try {
            searchResponse = esConfig.restHighLevelClient().search(searchRequest, RequestOptions.DEFAULT);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        scrollHandle(searchResponse, esConfig, esHandleDtos, startTime, endTime);
        return esHandleDtos;
    }

    public static void scrollHandle(SearchResponse searchResponse, ESConfig esConfig, List<ESHandleDto> esHandleDtos, String startTime, String endTime) {
        SearchHits hits = searchResponse.getHits();
        String scrollId = searchResponse.getScrollId();
        SearchHit[] searchHits = hits.getHits();
        //对结果集处理
        List<FunctionLogElasticEntity> functionLogs = ESUtil.convertToFunctionLog(searchHits);
        visitorsResultHandle(functionLogs, esHandleDtos, ESUtil.isMoreThanMonth(startTime, endTime));

		//滚动查询部分,将从第10001笔数据开始
        while (searchHits != null && searchHits.length > 0) {
            SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId);
            searchScrollRequest.scroll(TimeValue.timeValueMinutes(1));
            try {
                searchResponse = esConfig.restHighLevelClient().scroll(searchScrollRequest, RequestOptions.DEFAULT);
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
            scrollId = searchResponse.getScrollId();
            hits = searchResponse.getHits();
            searchHits = hits.getHits();
            //对结果集处理
            functionLogs = ESUtil.convertToFunctionLog(searchHits);
            visitorsResultHandle(functionLogs, esHandleDtos, ESUtil.isMoreThanMonth(startTime, endTime));
        }

        //清除滚动,否则影响下次查询
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        clearScrollRequest.addScrollId(scrollId);
        ClearScrollResponse clearScrollResponse = null;
        try {
            clearScrollResponse = esConfig.restHighLevelClient().clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        boolean succeeded = clearScrollResponse.isSucceeded();
        System.out.println(succeeded);
    }
相关推荐
mghio7 小时前
Dubbo 中的集群容错
java·微服务·dubbo
咖啡教室11 小时前
java日常开发笔记和开发问题记录
java
咖啡教室12 小时前
java练习项目记录笔记
java
鱼樱前端12 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea13 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea13 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
李少兄14 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝15 小时前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖15 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信