scroll.asciidoc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. [[java-rest-high-search-scroll]]
  2. === Search Scroll API
  3. The Scroll API can be used to retrieve a large number of results from
  4. a search request.
  5. In order to use scrolling, the following steps need to be executed in the
  6. given order.
  7. ==== Initialize the search scroll context
  8. An initial search request with a `scroll` parameter must be executed to
  9. initialize the scroll session through the <<java-rest-high-search>>.
  10. When processing this `SearchRequest`, Elasticsearch detects the presence of
  11. the `scroll` parameter and keeps the search context alive for the
  12. corresponding time interval.
  13. ["source","java",subs="attributes,callouts,macros"]
  14. --------------------------------------------------
  15. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-init]
  16. --------------------------------------------------
  17. <1> Create the `SearchRequest` and its corresponding `SearchSourceBuilder`.
  18. Also optionally set the `size` to control how many results to retrieve at
  19. a time.
  20. <2> Set the scroll interval
  21. <3> Read the returned scroll id, which points to the search context that's
  22. being kept alive and will be needed in the following search scroll call
  23. <4> Retrieve the first batch of search hits
  24. ==== Retrieve all the relevant documents
  25. As a second step, the received scroll identifier must be set to a
  26. `SearchScrollRequest` along with a new scroll interval and sent through the
  27. `searchScroll` method. Elasticsearch returns another batch of results with
  28. a new scroll identifier. This new scroll identifier can then be used in a
  29. subsequent `SearchScrollRequest` to retrieve the next batch of results,
  30. and so on. This process should be repeated in a loop until no more results are
  31. returned, meaning that the scroll has been exhausted and all the matching
  32. documents have been retrieved.
  33. ["source","java",subs="attributes,callouts,macros"]
  34. --------------------------------------------------
  35. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll2]
  36. --------------------------------------------------
  37. <1> Create the `SearchScrollRequest` by setting the required scroll id and
  38. the scroll interval
  39. <2> Read the new scroll id, which points to the search context that's
  40. being kept alive and will be needed in the following search scroll call
  41. <3> Retrieve another batch of search hits
  42. <4>
  43. ==== Clear the scroll context
  44. Finally, the last scroll identifier can be deleted using the <<java-rest-high-clear-scroll>>
  45. in order to release the search context. This happens automatically when the
  46. scroll expires, but it's good practice to do it as soon as the scroll session
  47. is completed.
  48. ==== Optional arguments
  49. The following arguments can optionally be provided when constructing
  50. the `SearchScrollRequest`:
  51. ["source","java",subs="attributes,callouts,macros"]
  52. --------------------------------------------------
  53. include-tagged::{doc-tests}/SearchDocumentationIT.java[scroll-request-arguments]
  54. --------------------------------------------------
  55. <1> Scroll interval as a `TimeValue`
  56. <2> Scroll interval as a `String`
  57. If no `scroll` value is set for the `SearchScrollRequest`, the search context will
  58. expire once the initial scroll time expired (ie, the scroll time set in the
  59. initial search request).
  60. [[java-rest-high-search-scroll-sync]]
  61. ==== Synchronous Execution
  62. ["source","java",subs="attributes,callouts,macros"]
  63. --------------------------------------------------
  64. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-execute-sync]
  65. --------------------------------------------------
  66. [[java-rest-high-search-scroll-async]]
  67. ==== Asynchronous Execution
  68. The asynchronous execution of a search scroll request requires both the `SearchScrollRequest`
  69. instance and an `ActionListener` instance to be passed to the asynchronous
  70. method:
  71. ["source","java",subs="attributes,callouts,macros"]
  72. --------------------------------------------------
  73. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-execute-async]
  74. --------------------------------------------------
  75. <1> The `SearchScrollRequest` to execute and the `ActionListener` to use when
  76. the execution completes
  77. The asynchronous method does not block and returns immediately. Once it is
  78. completed the `ActionListener` is called back using the `onResponse` method
  79. if the execution successfully completed or using the `onFailure` method if
  80. it failed.
  81. A typical listener for `SearchResponse` looks like:
  82. ["source","java",subs="attributes,callouts,macros"]
  83. --------------------------------------------------
  84. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-execute-listener]
  85. --------------------------------------------------
  86. <1> Called when the execution is successfully completed. The response is
  87. provided as an argument
  88. <2> Called in case of failure. The raised exception is provided as an argument
  89. [[java-rest-high-search-scroll-response]]
  90. ==== Response
  91. The search scroll API returns a `SearchResponse` object, same as the
  92. Search API.
  93. [[java-rest-high-search-scroll-example]]
  94. ==== Full example
  95. The following is a complete example of a scrolled search.
  96. ["source","java",subs="attributes,callouts,macros"]
  97. --------------------------------------------------
  98. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-example]
  99. --------------------------------------------------
  100. <1> Initialize the search context by sending the initial `SearchRequest`
  101. <2> Retrieve all the search hits by calling the Search Scroll api in a loop
  102. until no documents are returned
  103. <3> Create a new `SearchScrollRequest` holding the last returned scroll
  104. identifier and the scroll interval
  105. <4> Process the returned search results
  106. <5> Clear the scroll context once the scroll is completed
  107. [[java-rest-high-clear-scroll]]
  108. === Clear Scroll API
  109. The search contexts used by the Search Scroll API are automatically deleted when the scroll
  110. times out. But it is advised to release search contexts as soon as they are not
  111. necessary anymore using the Clear Scroll API.
  112. [[java-rest-high-clear-scroll-request]]
  113. ==== Clear Scroll Request
  114. A `ClearScrollRequest` can be created as follows:
  115. ["source","java",subs="attributes,callouts,macros"]
  116. --------------------------------------------------
  117. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-request]
  118. --------------------------------------------------
  119. <1> Create a new `ClearScrollRequest`
  120. <2> Adds a scroll id to the list of scroll identifiers to clear
  121. ==== Providing the scroll identifiers
  122. The `ClearScrollRequest` allows to clear one or more scroll identifiers in a single request.
  123. The scroll identifiers can be added to the request one by one:
  124. ["source","java",subs="attributes,callouts,macros"]
  125. --------------------------------------------------
  126. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-add-scroll-id]
  127. --------------------------------------------------
  128. Or all together using:
  129. ["source","java",subs="attributes,callouts,macros"]
  130. --------------------------------------------------
  131. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-add-scroll-ids]
  132. --------------------------------------------------
  133. [[java-rest-high-clear-scroll-sync]]
  134. ==== Synchronous Execution
  135. ["source","java",subs="attributes,callouts,macros"]
  136. --------------------------------------------------
  137. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-execute]
  138. --------------------------------------------------
  139. [[java-rest-high-clear-scroll-async]]
  140. ==== Asynchronous Execution
  141. The asynchronous execution of a clear scroll request requires both the `ClearScrollRequest`
  142. instance and an `ActionListener` instance to be passed to the asynchronous
  143. method:
  144. ["source","java",subs="attributes,callouts,macros"]
  145. --------------------------------------------------
  146. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-execute-async]
  147. --------------------------------------------------
  148. <1> The `ClearScrollRequest` to execute and the `ActionListener` to use when
  149. the execution completes
  150. The asynchronous method does not block and returns immediately. Once it is
  151. completed the `ActionListener` is called back using the `onResponse` method
  152. if the execution successfully completed or using the `onFailure` method if
  153. it failed.
  154. A typical listener for `ClearScrollResponse` looks like:
  155. ["source","java",subs="attributes,callouts,macros"]
  156. --------------------------------------------------
  157. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-execute-listener]
  158. --------------------------------------------------
  159. <1> Called when the execution is successfully completed. The response is
  160. provided as an argument
  161. <2> Called in case of failure. The raised exception is provided as an argument
  162. [[java-rest-high-clear-scroll-response]]
  163. ==== Clear Scroll Response
  164. The returned `ClearScrollResponse` allows to retrieve information about the released
  165. search contexts:
  166. ["source","java",subs="attributes,callouts,macros"]
  167. --------------------------------------------------
  168. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-response]
  169. --------------------------------------------------
  170. <1> Return true if the request succeeded
  171. <2> Return the number of released search contexts