ElasticSearch High Level REST API(3)Scroll 滚屏

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

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

完整的示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@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);
}
1
2
3
4
5
6
7
8
9
-----首页-----
{"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

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :