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);
    }
相关推荐
xlsw_2 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹2 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭3 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫3 小时前
泛型(2)
java
超爱吃士力架3 小时前
邀请逻辑
java·linux·后端
南宫生3 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石3 小时前
12/21java基础
java
李小白663 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp4 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
woshiabc1114 小时前
windows安装Elasticsearch及增删改查操作
大数据·elasticsearch·搜索引擎