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);
    }
相关推荐
码蜂窝编程官方1 分钟前
【含开题报告+文档+PPT+源码】基于SpringBoot+Vue的虎鲸旅游攻略网的设计与实现
java·vue.js·spring boot·后端·spring·旅游
Viktor_Ye18 分钟前
高效集成易快报与金蝶应付单的方案
java·前端·数据库
hummhumm20 分钟前
第 25 章 - Golang 项目结构
java·开发语言·前端·后端·python·elasticsearch·golang
一二小选手24 分钟前
【Maven】IDEA创建Maven项目 Maven配置
java·maven
J老熊30 分钟前
JavaFX:简介、使用场景、常见问题及对比其他框架分析
java·开发语言·后端·面试·系统架构·软件工程
猿java35 分钟前
什么是 Hystrix?它的工作原理是什么?
java·微服务·面试
AuroraI'ncoding36 分钟前
时间请求参数、响应
java·后端·spring
所待.3831 小时前
JavaEE之线程初阶(上)
java·java-ee
Winston Wood1 小时前
Java线程池详解
java·线程池·多线程·性能
手握风云-1 小时前
数据结构(Java版)第二期:包装类和泛型
java·开发语言·数据结构