ES中提供了 FROM/SIZE 分页,但这种分页有性能瓶颈。
Scroll会以间隔时间滚屏的方式返回全部的查询数据,可以作为数据量很大的情况下,分页的一个替代方案
完整的示例如下:
public void scroll(){ RestHighLevelClient client = elasticClient.getRestHighLevelClient(); //初始化scroll final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L)); //设定滚动时间间隔 SearchRequest searchRequest = new SearchRequest(); searchRequest.scroll(scroll); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(matchAllQuery()); searchSourceBuilder.size(5); //设定每次返回多少条数据 searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = null; try { searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } String scrollId = searchResponse.getScrollId(); SearchHit[] searchHits = searchResponse.getHits().getHits(); System.out.println("-----首页-----"); for (SearchHit searchHit : searchHits) { System.out.println(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) { System.out.println("-----下一页-----"); for (SearchHit searchHit : searchHits) { System.out.println(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(); System.out.println("succeeded:" + succeeded); }