|
7 | 7 | Scroll API的创建并不是为了实时的用户响应,而是为了处理大量的数据(Scrolling is not intended for real time user requests, but rather for processing large amounts of data)。从 scroll 请求返回的结果只是反映了 search 发生那一时刻的索引状态,就像一个快照(The results that are returned from a scroll request reflect the state of the index at the time that the initial search request was made, like a snapshot in time)。后续的对文档的改动(索引、更新或者删除)都只会影响后面的搜索请求。
|
8 | 8 |
|
9 | 9 |
|
10 | | - |
11 | | - |
12 | 10 | ```
|
13 | 11 | import static org.elasticsearch.index.query.QueryBuilders.*;
|
| 12 | +``` |
14 | 13 |
|
| 14 | +``` |
15 | 15 | QueryBuilder qb = termQuery("multi", "test");
|
16 | 16 |
|
17 | 17 | SearchResponse scrollResp = client.prepareSearch(test)
|
@@ -48,6 +48,7 @@ Caused by: SearchContextMissingException[No search context found for id [2861]]
|
48 | 48 | ```
|
49 | 49 | > 虽然当滚动有效时间已过,搜索上下文(Search Context)会自动被清除,但是一值保持滚动代价也是很大的,所以当我们不在使用滚动时要尽快使用Clear-Scroll API进行清除。
|
50 | 50 |
|
| 51 | +## 清除Scroll |
51 | 52 |
|
52 | 53 | ```
|
53 | 54 | /**
|
@@ -80,4 +81,49 @@ Caused by: SearchContextMissingException[No search context found for id [2861]]
|
80 | 81 |
|
81 | 82 | ```
|
82 | 83 |
|
83 | | -``` |
| 84 | +public class ScrollsAPI extends ElasticsearchClientBase { |
| 85 | + |
| 86 | + private String scrollId; |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testScrolls() throws Exception { |
| 90 | + |
| 91 | + SearchResponse scrollResp = client.prepareSearch("twitter") |
| 92 | + .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC) |
| 93 | + .setScroll(new TimeValue(60000)) //为了使用 scroll,初始搜索请求应该在查询中指定 scroll 参数,告诉 Elasticsearch 需要保持搜索的上下文环境多长时间(滚动时间) |
| 94 | + .setQuery(QueryBuilders.termQuery("user", "kimchy")) // Query 查询条件 |
| 95 | + .setSize(5).get(); //max of 100 hits will be returned for each scroll |
| 96 | + //Scroll until no hits are returned |
| 97 | + |
| 98 | + scrollId = scrollResp.getScrollId(); |
| 99 | + do { |
| 100 | + for (SearchHit hit : scrollResp.getHits().getHits()) { |
| 101 | + //Handle the hit... |
| 102 | + |
| 103 | + System.out.println("" + hit.getSource().toString()); |
| 104 | + } |
| 105 | + |
| 106 | + scrollResp = client.prepareSearchScroll(scrollId).setScroll(new TimeValue(60000)).execute().actionGet(); |
| 107 | + } |
| 108 | + while (scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop. |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void tearDown() throws Exception { |
| 113 | + ClearScrollRequestBuilder clearScrollRequestBuilder = client.prepareClearScroll(); |
| 114 | + clearScrollRequestBuilder.addScrollId(scrollId); |
| 115 | + ClearScrollResponse response = clearScrollRequestBuilder.get(); |
| 116 | + |
| 117 | + if (response.isSucceeded()) { |
| 118 | + System.out.println("成功清除"); |
| 119 | + } |
| 120 | + |
| 121 | + super.tearDown(); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +- [ScrollsAPI.java](https://gitee.com/quanke/elasticsearch-java-study/blob/master/src/test/java/name/quanke/es/study/search/ScrollsAPI.java) |
| 128 | + |
| 129 | +- [本手册完整实例](https://gitee.com/quanke/elasticsearch-java-study) |
0 commit comments