【问题标题】:ElasticsearchRestTemplate scroll get next pagesElasticsearchRestTemplate 滚动获取下一页
【发布时间】:2023-03-18 14:49:02
【问题描述】:

我正在使用 spring data elasticsearch (https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference)。

这是我的 pom.xml

<properties>
        <spring-data-elasticsearch.version>3.2.6</spring-data-elasticsearch.version>
</properties>
...
<!-- Elasticsearch -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>${spring-data-elasticsearch.version}.RELEASE</version>
        </dependency>
    </properties>

我有密码

 private ElasticsearchRestTemplate template;
    
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                        .withPageable(PageRequest.of(0, 2/*, Sort.Direction.ASC, "meta.time_created"*/))
                        .build();
        
                Page<myData> scroll = this.template.startScroll(30000, searchQuery, myData.class);
                while(scroll.hasNext()) {
                    LOGGER.info("Scroll!");
                    LOGGER.info(String.valueOf(scroll.getSize()));
                    System.out.println(scroll.nextPageable());
                    List<myData> l = scroll.getContent();
                    System.out.println(l.size());
                    Pageable p = scroll.nextPageable();
        
                    page++;
        
                    searchQuery = new NativeSearchQueryBuilder()
                            .withPageable(PageRequest.of(page, 2/*, Sort.Direction.ASC, "meta.time_created"*/))
                            .build();
        
                    scroll = this.template.startScroll(30000, searchQuery, myData.class);
        }

我无法检索滚动中的下一页。卷轴在同一页码 1 上无限循环。我该如何移动这个卷轴?

【问题讨论】:

    标签: spring-boot elasticsearch spring-data-jpa


    【解决方案1】:

    您的代码中缺少continueScroll 调用。以下程序将为您提供所需的东西

    private ElasticsearchRestTemplate template;
    
    SearchQuery searchQuery = new NativeSearchQueryBuilder()
                    .withPageable(PageRequest.of(0, 2/*, Sort.Direction.ASC, "meta.time_created"*/))
                    .build();
    
    ScrolledPage<myData> scroll = this.template.startScroll(30000, searchQuery, myData.class);
    // First page
    List<myData> l = scroll.getContent();
    System.out.println(l.size());
    
    while(scroll.hasNext()) {
        // Advance the scroll to next page now
        scroll = this.template.continueScroll(scroll.getScrollId(), 30000, myData.class);
        
        LOGGER.info("Scroll!");
        LOGGER.info(String.valueOf(scroll.getSize()));
        System.out.println(scroll.nextPageable());
        l = scroll.getContent();
        System.out.println(l.size());            
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多