ElasticSearch High Level REST API(3)Scroll 滚屏

ElasticSearch中提供了 FROM/SIZE 分页,但这种分页有性能瓶颈。

Scroll会以间隔时间滚屏的方式返回全部的查询数据,可以作为数据量很大的情况下,分页的一个替代方案

完整的示例如下:

@GetMapping("scroll")
public String scroll() {
    //初始化scroll
    final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L)); //设定滚动时间间隔
    SearchRequest searchRequest = new SearchRequest("test");
    searchRequest.scroll(scroll);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchSourceBuilder.size(2); //设定每次返回多少条数据
    searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = null;
    try {
        searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (searchResponse == null) {
        return "";
    }
    String scrollId = searchResponse.getScrollId();
    SearchHit[] searchHits = searchResponse.getHits().getHits();
    log.info("-----首页-----");
    for (SearchHit searchHit : searchHits) {
        log.info(searchHit.getSourceAsString());
    }
    //遍历搜索命中的数据,直到没有数据
    while (searchHits != null && searchHits.length > 0) {
        SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
        scrollRequest.scroll(scroll);
        try {
            searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        scrollId = searchResponse.getScrollId();
        searchHits = searchResponse.getHits().getHits();
        if (searchHits != null && searchHits.length > 0) {
            log.info("-----下一页-----");
            for (SearchHit searchHit : searchHits) {
                log.info(searchHit.getSourceAsString());
            }
        }
    }
    //清除滚屏
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
    clearScrollRequest.addScrollId(scrollId); //也可以选择setScrollIds()将多个scrollId一起使用
    ClearScrollResponse clearScrollResponse = null;
    try {
        clearScrollResponse = client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        e.printStackTrace();
    }
    boolean succeeded = clearScrollResponse.isSucceeded();
    log.info("succeeded:" + succeeded);
    return String.valueOf(succeeded);
}
-----首页-----
{"name":"Joe.Ye@AppBlog.CN","email":"yezhou@yezhou.org","homepage":"http://www.appblog.cn","age":18}
{"name":"Joe.Ye","email":"yezhou@yezhou.org","homepage":"http://www.appblog.cn"}
-----下一页-----
{"name":"Joe.Ye@AppBlog.CN","age":18,"homepage":"http://www.appblog.cn"}
{"name":"Joe.Ye","age":28,"homepage":"http://www.appblog.cn"}
-----下一页-----
{"name":"Joe.Ye","email":"yezhou@yezhou.org","homepage":"http://www.appblog.cn"}
succeeded:true

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/19/elasticsearch-high-level-rest-api-scroll-scrolling/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
ElasticSearch High Level REST API(3)Scroll 滚屏
ElasticSearch中提供了 FROM/SIZE 分页,但这种分页有性能瓶颈。 Scroll会以间隔时间滚屏的方式返回全部的查询数据,可以作为数据量很大的情况下,分页的一个……
<<上一篇
下一篇>>
文章目录
关闭
目 录