4
4
The Scroll API can be used to retrieve a large number of results from
5
5
a search request.
6
6
7
- In order to use scrolling, several steps need to be executed in a given order:
7
+ In order to use scrolling, the following steps need to be executed in the
8
+ given order.
8
9
9
- * At first, an initial search request with a non-null `scroll` parameter must
10
- be executed. When processing this `SearchRequest`, Elasticsearch detects the
11
- presence of the `scroll` parameter and keeps the search context alive during
12
- the time defined by the parameter. Elasticsearch generates a scroll identifier
13
- associated to this search context and returns it with the first batch of search
14
- results in a `SearchResponse`.
15
10
16
- * As a second step, the initial scroll parameter and the new scroll identifier
17
- are set in a `SearchScrollRequest`. This request is executed using the Search
18
- Scroll API and Elasticsearch returns the second batch of results with a new
19
- scroll identifier. This new scroll id can then be used in another `SearchScrollRequest`
20
- to retrieve the next batch of results. This process can be repeated over and
21
- over until no more results are returned.
11
+ ==== Initialize the search scroll context
22
12
23
- * Finally, the last scroll identifier can be deleted using the <<java-rest-high-clear-scroll>>
24
- in order to release the search context.
13
+ An initial search request with a `scroll` parameter must be executed to
14
+ initialize the scroll session through the <<java-rest-high-search>>.
15
+ When processing this `SearchRequest`, Elasticsearch detects the presence of
16
+ the `scroll` parameter and keeps the search context alive for the
17
+ corresponding time interval.
25
18
26
- [[java-rest-high-search-scroll-example]]
27
- ==== Example of Execution
19
+ ["source","java",subs="attributes,callouts,macros"]
20
+ --------------------------------------------------
21
+ include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-init]
22
+ --------------------------------------------------
23
+ <1> Create the `SearchRequest` and its corresponding `SearchSourceBuilder`.
24
+ Also optionally set the `size` to control how many results to retrieve at
25
+ a time.
26
+ <2> Set the scroll interval
27
+ <3> Read the returned scroll id, which points to the search context that's
28
+ being kept alive and will be needed in the following search scroll call
29
+ <4> Retrieve the first batch of search hits
30
+
31
+ ==== Retrieve all the relevant documents
28
32
29
- Here is an example of a scrolled search:
33
+ As a second step, the received scroll identifier must be set to a
34
+ `SearchScrollRequest` along with a new scroll interval and sent through the
35
+ `searchScroll` method. Elasticsearch returns another batch of results with
36
+ a new scroll identifier. This new scroll identifier can then be used in a
37
+ subsequent `SearchScrollRequest` to retrieve the next batch of results,
38
+ and so on. This process should be repeated in a loop until no more results are
39
+ returned, meaning that the scroll has been exhausted and all the matching
40
+ documents have been retrieved.
30
41
31
42
["source","java",subs="attributes,callouts,macros"]
32
43
--------------------------------------------------
33
- include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-example ]
44
+ include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll2 ]
34
45
--------------------------------------------------
35
- <1> Define a scroll parameter as a `TimeValue` corresponding to one minute
36
- <2> Create a new `SearchRequest`. See <<java-rest-high-search>>
37
- for more information on how to build `SearchRequest`.
38
- <3> Set the `scroll` parameter to the `SearchRequest`
39
- <4> Execute the `SearchRequest`
40
- <5> Retrieve the first scroll id
41
- <6> Retrieve the first batch of search hits
42
- <7> Iterate until there are no more search hits to process
43
- <8> Create a new `SearchScrollRequest`
44
- <9> Set the `scroll` parameter again to tell Elasticsearch to keep the search context
45
- alive for another minute
46
- <10> Set the scroll id
47
- <11> Execute the `SearchScrollRequest` using the Search Scroll API
48
- <12> Retrieve the next scroll id to use in upcoming requests
49
- <13> Retrieve the next batch of search hits
50
- <14> Clear the scroll id using the <<java-rest-high-clear-scroll>>.
46
+ <1> Create the `SearchScrollRequest` by setting the required scroll id and
47
+ the scroll interval
48
+ <2> Read the new scroll id, which points to the search context that's
49
+ being kept alive and will be needed in the following search scroll call
50
+ <3> Retrieve another batch of search hits
51
+ <4>
52
+
53
+ ==== Clear the scroll context
54
+
55
+ Finally, the last scroll identifier can be deleted using the <<java-rest-high-clear-scroll>>
56
+ in order to release the search context. This happens automatically when the
57
+ scroll expires, but it's good practice to do it as soon as the scroll session
58
+ is completed.
51
59
52
60
==== Optional arguments
53
- The following argument can optionally be provided:
61
+
62
+ The following arguments can optionally be provided when constructing
63
+ the `SearchScrollRequest`:
54
64
55
65
["source","java",subs="attributes,callouts,macros"]
56
66
--------------------------------------------------
57
- include-tagged::{doc-tests}/SearchDocumentationIT.java[scroll-request-scroll ]
67
+ include-tagged::{doc-tests}/SearchDocumentationIT.java[scroll-request-arguments ]
58
68
--------------------------------------------------
59
- <1> Scroll value (ie, the time to keep alive the search context) as a `TimeValue`
60
- <2> Scroll value (ie, the time to keep alive the search context) as a `String`
69
+ <1> Scroll interval as a `TimeValue`
70
+ <2> Scroll interval as a `String`
61
71
62
- If no `scroll` value is set for the `SearchScrollRequest`, then the search context
63
- will expire once the initial scroll time expired (ie, the scroll time set in the
72
+ If no `scroll` value is set for the `SearchScrollRequest`, the search context will
73
+ expire once the initial scroll time expired (ie, the scroll time set in the
64
74
initial search request).
65
75
66
76
[[java-rest-high-search-scroll-sync]]
@@ -82,11 +92,33 @@ include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-execute-asy
82
92
provided as an argument
83
93
<2> Called in case of failure. The raised exception is provided as an argument
84
94
95
+ [[java-rest-high-search-scroll-response]]
96
+ ==== Response
97
+
98
+ The search scroll API returns a `SearchResponse` object, same as the
99
+ Search API.
100
+
101
+ [[java-rest-high-search-scroll-example]]
102
+ ==== Full example
103
+
104
+ The following is a complete example of a scrolled search.
105
+
106
+ ["source","java",subs="attributes,callouts,macros"]
107
+ --------------------------------------------------
108
+ include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-example]
109
+ --------------------------------------------------
110
+ <1> Initialize the search context by sending the initial `SearchRequest`
111
+ <2> Retrieve all the search hits by calling the Search Scroll api in a loop
112
+ until no documents are returned
113
+ <3> Create a new `SearchScrollRequest` holding the last returned scroll
114
+ identifier and the scroll interval
115
+ <4> Process the returned search results
116
+ <5> Clear the scroll context once the scroll is completed
85
117
86
118
[[java-rest-high-clear-scroll]]
87
119
=== Clear Scroll API
88
120
89
- The search contexts used by the Scroll API are automatically deleted when the scroll
121
+ The search contexts used by the Search Scroll API are automatically deleted when the scroll
90
122
times out. But it is advised to release search contexts as soon as they are not
91
123
necessary anymore using the Clear Scroll API.
92
124
0 commit comments