scroll.asciidoc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. ["source","java",subs="attributes,callouts,macros"]
  69. --------------------------------------------------
  70. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-execute-async]
  71. --------------------------------------------------
  72. <1> Called when the execution is successfully completed. The response is
  73. provided as an argument
  74. <2> Called in case of failure. The raised exception is provided as an argument
  75. [[java-rest-high-search-scroll-response]]
  76. ==== Response
  77. The search scroll API returns a `SearchResponse` object, same as the
  78. Search API.
  79. [[java-rest-high-search-scroll-example]]
  80. ==== Full example
  81. The following is a complete example of a scrolled search.
  82. ["source","java",subs="attributes,callouts,macros"]
  83. --------------------------------------------------
  84. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-example]
  85. --------------------------------------------------
  86. <1> Initialize the search context by sending the initial `SearchRequest`
  87. <2> Retrieve all the search hits by calling the Search Scroll api in a loop
  88. until no documents are returned
  89. <3> Create a new `SearchScrollRequest` holding the last returned scroll
  90. identifier and the scroll interval
  91. <4> Process the returned search results
  92. <5> Clear the scroll context once the scroll is completed
  93. [[java-rest-high-clear-scroll]]
  94. === Clear Scroll API
  95. The search contexts used by the Search Scroll API are automatically deleted when the scroll
  96. times out. But it is advised to release search contexts as soon as they are not
  97. necessary anymore using the Clear Scroll API.
  98. [[java-rest-high-clear-scroll-request]]
  99. ==== Clear Scroll Request
  100. A `ClearScrollRequest` can be created as follows:
  101. ["source","java",subs="attributes,callouts,macros"]
  102. --------------------------------------------------
  103. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-request]
  104. --------------------------------------------------
  105. <1> Create a new `ClearScrollRequest`
  106. <2> Adds a scroll id to the list of scroll identifiers to clear
  107. ==== Providing the scroll identifiers
  108. The `ClearScrollRequest` allows to clear one or more scroll identifiers in a single request.
  109. The scroll identifiers can be added to the request one by one:
  110. ["source","java",subs="attributes,callouts,macros"]
  111. --------------------------------------------------
  112. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-add-scroll-id]
  113. --------------------------------------------------
  114. Or all together using:
  115. ["source","java",subs="attributes,callouts,macros"]
  116. --------------------------------------------------
  117. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-add-scroll-ids]
  118. --------------------------------------------------
  119. [[java-rest-high-clear-scroll-sync]]
  120. ==== Synchronous Execution
  121. ["source","java",subs="attributes,callouts,macros"]
  122. --------------------------------------------------
  123. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-execute]
  124. --------------------------------------------------
  125. [[java-rest-high-clear-scroll-async]]
  126. ==== Asynchronous Execution
  127. ["source","java",subs="attributes,callouts,macros"]
  128. --------------------------------------------------
  129. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-execute-async]
  130. --------------------------------------------------
  131. <1> Called when the execution is successfully completed. The response is
  132. provided as an argument
  133. <2> Called in case of failure. The raised exception is provided as an argument
  134. [[java-rest-high-clear-scroll-response]]
  135. ==== Clear Scroll Response
  136. The returned `ClearScrollResponse` allows to retrieve information about the released
  137. search contexts:
  138. ["source","java",subs="attributes,callouts,macros"]
  139. --------------------------------------------------
  140. include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-response]
  141. --------------------------------------------------
  142. <1> Return true if the request succeeded
  143. <2> Return the number of released search contexts