SoFunction
Updated on 2025-03-06

Java Api implements the scroll query function of Elasticsearch

Java Api implements the scroll query function of Elasticsearch

Updated: August 14, 2023 10:25:50 Author: Cimbala
This article mainly introduces the scroll query of Java Api to implement Elasticsearch, solving the problem that ES can only query 10,000 pieces of data at a time. This article introduces it to you in detail through the example code. Friends who need it can refer to it.

Solve the problem that ES can only query 10,000 pieces of data at a time

@Override
    public List<ESHandleDto> getVisitorsNum(String startTime, String endTime, String schoolName, String typeFunction) throws IOException {
        List<ESHandleDto> esHandleDtos = new ArrayList<>();
        SearchRequest searchRequest = new SearchRequest();
        (ElasticEnum.FUNCTIONLOG_INDEX.getValue());
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = ();
        if ((schoolName)) {
            (("", schoolName));
        }
        if ((typeFunction)) {
            (("", typeFunction));
        }
        if ((startTime) && (endTime)) {
            (("createDate").gte(startTime + "T00:00:00.000Z").lte(endTime + "T23:59:59.000Z"));
        }
        BoolQueryBuilder shouldQuery = ();
        ().add(("", "Query student information table"));
        ().add(("", "Get school visitor data"));
        (shouldQuery);
        ("_doc", );
        (10000);
        (boolQueryBuilder);
        ((1));
        (sourceBuilder);
//        (());
        SearchResponse searchResponse = null;
        try {
            searchResponse = ().search(searchRequest, );
        } 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 = ();
        String scrollId = ();
        SearchHit[] searchHits = ();
        // Process the result set        List<FunctionLogElasticEntity> functionLogs = (searchHits);
        visitorsResultHandle(functionLogs, esHandleDtos, (startTime, endTime));
		//The scroll query part will start with the 10001st data        while (searchHits != null &&  > 0) {
            SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId);
            ((1));
            try {
                searchResponse = ().scroll(searchScrollRequest, );
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
            scrollId = ();
            hits = ();
            searchHits = ();
            // Process the result set            functionLogs = (searchHits);
            visitorsResultHandle(functionLogs, esHandleDtos, (startTime, endTime));
        }
        //Clear scrolling, otherwise it will affect the next query        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        (scrollId);
        ClearScrollResponse clearScrollResponse = null;
        try {
            clearScrollResponse = ().clearScroll(clearScrollRequest, );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        boolean succeeded = ();
        (succeeded);
    }

This is the end of this article about the scroll query of Java Api that implements Elasticsearch. For more related Java Elasticsearch scroll query content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!

  • Java
  • Elasticsearch
  • Scroll query

Related Articles

  • Java deadlock solution sequential lock and polling lock

    This article mainly introduces Java deadlock solution sequence lock and polling lock. The article focuses on the topic and provides a detailed introduction to the content, which has certain reference value. Friends who need it can refer to it.
    2022-05-05
  • MyBatis' nested query parsing

    This article mainly introduces MyBatis' nested query analysis. The editor thinks it is quite good. I will share it with you now and give you a reference. Let's take a look with the editor
    2017-05-05
  • Java has an in-depth understanding of the builder model of the creation design model

    The Builder model and the factory model have different concerns: the Builder model focuses on the assembly process of parts, while the factory method model focuses more on the creation process of parts, but the two can be used in combination.
    2022-02-02
  • JAVA Sample code for adding, modifying, and deleting PDF bookmarks

    This article mainly introduces JAVA's sample code for adding, modifying and deleting PDF bookmarks. It is very good and has certain reference value. Friends who need it can refer to it.
    2019-06-06
  • An example method of abstract modifying class in java

    In this article, the editor has shared with you an example method about abstract modification class in Java. Friends in need can learn it.
    2020-12-12
  • Detailed explanation of SpringBoot integration of Druid data source process

    This article mainly introduces a detailed explanation of the process of integrating SpringBoot's Druid data source. The example code is introduced in this article in detail, which has certain reference value for everyone's learning or work. Friends who need it can refer to it.
    2019-12-12
  • Java Source Code Reread Series HashMap

    This article mainly introduces the Java source code rereading series HashMap sample analysis. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get a promotion as soon as possible.
    2023-04-04
  • Detailed explanation of Java threads-daemon threads and user threads

    This article mainly introduces Java daemon threads and user threads. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.
    2019-04-04
  • Spring-AOP Static Ordinary Method Name Matching Face Operation

    This article mainly introduces Spring-AOP static ordinary method name matching section operation, which has good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2021-07-07
  • Summary of the problem of SpringController return value and exception automatic wrapping

    Today I encountered a requirement. Without changing the original system code, the Controller return value and exception are wrapped into a unified return object. The following article will introduce the problem of SpringController return value and exception automatic wrapping. Friends who need it can refer to it.
    2024-03-03

Latest Comments