scroll.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. [discrete]
  2. [[scroll-search-results]]
  3. === Scroll search results
  4. While a `search` request returns a single ``page'' of results, the `scroll`
  5. API can be used to retrieve large numbers of results (or even all results)
  6. from a single search request, in much the same way as you would use a cursor
  7. on a traditional database.
  8. Scrolling is not intended for real time user requests, but rather for
  9. processing large amounts of data, e.g. in order to reindex the contents of one
  10. data stream or index into a new data stream or index with a different
  11. configuration.
  12. .Client support for scrolling and reindexing
  13. *********************************************
  14. Some of the officially supported clients provide helpers to assist with
  15. scrolled searches and reindexing:
  16. Perl::
  17. See https://metacpan.org/pod/Search::Elasticsearch::Client::5_0::Bulk[Search::Elasticsearch::Client::5_0::Bulk]
  18. and https://metacpan.org/pod/Search::Elasticsearch::Client::5_0::Scroll[Search::Elasticsearch::Client::5_0::Scroll]
  19. Python::
  20. See https://elasticsearch-py.readthedocs.org/en/master/helpers.html[elasticsearch.helpers.*]
  21. JavaScript::
  22. See {jsclient-current}/client-helpers.html[client.helpers.*]
  23. *********************************************
  24. NOTE: The results that are returned from a scroll request reflect the state of
  25. the data stream or index at the time that the initial `search` request was made, like a
  26. snapshot in time. Subsequent changes to documents (index, update or delete)
  27. will only affect later search requests.
  28. In order to use scrolling, the initial search request should specify the
  29. `scroll` parameter in the query string, which tells Elasticsearch how long it
  30. should keep the ``search context'' alive (see <<scroll-search-context>>), eg `?scroll=1m`.
  31. [source,console]
  32. --------------------------------------------------
  33. POST /twitter/_search?scroll=1m
  34. {
  35. "size": 100,
  36. "query": {
  37. "match": {
  38. "title": "elasticsearch"
  39. }
  40. }
  41. }
  42. --------------------------------------------------
  43. // TEST[setup:twitter]
  44. The result from the above request includes a `_scroll_id`, which should
  45. be passed to the `scroll` API in order to retrieve the next batch of
  46. results.
  47. [source,console]
  48. --------------------------------------------------
  49. POST /_search/scroll <1>
  50. {
  51. "scroll" : "1m", <2>
  52. "scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==" <3>
  53. }
  54. --------------------------------------------------
  55. // TEST[continued s/DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==/$body._scroll_id/]
  56. <1> `GET` or `POST` can be used and the URL should not include the `index`
  57. name -- this is specified in the original `search` request instead.
  58. <2> The `scroll` parameter tells Elasticsearch to keep the search context open
  59. for another `1m`.
  60. <3> The `scroll_id` parameter
  61. The `size` parameter allows you to configure the maximum number of hits to be
  62. returned with each batch of results. Each call to the `scroll` API returns the
  63. next batch of results until there are no more results left to return, ie the
  64. `hits` array is empty.
  65. IMPORTANT: The initial search request and each subsequent scroll request each
  66. return a `_scroll_id`. While the `_scroll_id` may change between requests, it doesn’t
  67. always change — in any case, only the most recently received `_scroll_id` should be used.
  68. NOTE: If the request specifies aggregations, only the initial search response
  69. will contain the aggregations results.
  70. NOTE: Scroll requests have optimizations that make them faster when the sort
  71. order is `_doc`. If you want to iterate over all documents regardless of the
  72. order, this is the most efficient option:
  73. [source,console]
  74. --------------------------------------------------
  75. GET /_search?scroll=1m
  76. {
  77. "sort": [
  78. "_doc"
  79. ]
  80. }
  81. --------------------------------------------------
  82. // TEST[setup:twitter]
  83. [discrete]
  84. [[scroll-search-context]]
  85. ==== Keeping the search context alive
  86. A scroll returns all the documents which matched the search at the time of the
  87. initial search request. It ignores any subsequent changes to these documents.
  88. The `scroll_id` identifies a _search context_ which keeps track of everything
  89. that {es} needs to return the correct documents. The search context is created
  90. by the initial request and kept alive by subsequent requests.
  91. The `scroll` parameter (passed to the `search` request and to every `scroll`
  92. request) tells Elasticsearch how long it should keep the search context alive.
  93. Its value (e.g. `1m`, see <<time-units>>) does not need to be long enough to
  94. process all data -- it just needs to be long enough to process the previous
  95. batch of results. Each `scroll` request (with the `scroll` parameter) sets a
  96. new expiry time. If a `scroll` request doesn't pass in the `scroll`
  97. parameter, then the search context will be freed as part of _that_ `scroll`
  98. request.
  99. Normally, the background merge process optimizes the index by merging together
  100. smaller segments to create new, bigger segments. Once the smaller segments are
  101. no longer needed they are deleted. This process continues during scrolling, but
  102. an open search context prevents the old segments from being deleted since they
  103. are still in use.
  104. TIP: Keeping older segments alive means that more disk space and file handles
  105. are needed. Ensure that you have configured your nodes to have ample free file
  106. handles. See <<file-descriptors>>.
  107. Additionally, if a segment contains deleted or updated documents then the
  108. search context must keep track of whether each document in the segment was live
  109. at the time of the initial search request. Ensure that your nodes have
  110. sufficient heap space if you have many open scrolls on an index that is subject
  111. to ongoing deletes or updates.
  112. NOTE: To prevent against issues caused by having too many scrolls open, the
  113. user is not allowed to open scrolls past a certain limit. By default, the
  114. maximum number of open scrolls is 500. This limit can be updated with the
  115. `search.max_open_scroll_context` cluster setting.
  116. You can check how many search contexts are open with the
  117. <<cluster-nodes-stats,nodes stats API>>:
  118. [source,console]
  119. ---------------------------------------
  120. GET /_nodes/stats/indices/search
  121. ---------------------------------------
  122. [discrete]
  123. [[clear-scroll]]
  124. ==== Clear scroll
  125. Search context are automatically removed when the `scroll` timeout has been
  126. exceeded. However keeping scrolls open has a cost, as discussed in the
  127. <<scroll-search-context,previous section>> so scrolls should be explicitly
  128. cleared as soon as the scroll is not being used anymore using the
  129. `clear-scroll` API:
  130. [source,console]
  131. ---------------------------------------
  132. DELETE /_search/scroll
  133. {
  134. "scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ=="
  135. }
  136. ---------------------------------------
  137. // TEST[catch:missing]
  138. Multiple scroll IDs can be passed as array:
  139. [source,console]
  140. ---------------------------------------
  141. DELETE /_search/scroll
  142. {
  143. "scroll_id" : [
  144. "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==",
  145. "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAABFmtSWWRRWUJrU2o2ZExpSGJCVmQxYUEAAAAAAAAAAxZrUllkUVlCa1NqNmRMaUhiQlZkMWFBAAAAAAAAAAIWa1JZZFFZQmtTajZkTGlIYkJWZDFhQQAAAAAAAAAFFmtSWWRRWUJrU2o2ZExpSGJCVmQxYUEAAAAAAAAABBZrUllkUVlCa1NqNmRMaUhiQlZkMWFB"
  146. ]
  147. }
  148. ---------------------------------------
  149. // TEST[catch:missing]
  150. All search contexts can be cleared with the `_all` parameter:
  151. [source,console]
  152. ---------------------------------------
  153. DELETE /_search/scroll/_all
  154. ---------------------------------------
  155. The `scroll_id` can also be passed as a query string parameter or in the request body.
  156. Multiple scroll IDs can be passed as comma separated values:
  157. [source,console]
  158. ---------------------------------------
  159. DELETE /_search/scroll/DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==,DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAABFmtSWWRRWUJrU2o2ZExpSGJCVmQxYUEAAAAAAAAAAxZrUllkUVlCa1NqNmRMaUhiQlZkMWFBAAAAAAAAAAIWa1JZZFFZQmtTajZkTGlIYkJWZDFhQQAAAAAAAAAFFmtSWWRRWUJrU2o2ZExpSGJCVmQxYUEAAAAAAAAABBZrUllkUVlCa1NqNmRMaUhiQlZkMWFB
  160. ---------------------------------------
  161. // TEST[catch:missing]
  162. [discrete]
  163. [[sliced-scroll]]
  164. ==== Sliced Scroll
  165. For scroll queries that return a lot of documents it is possible to split the scroll in multiple slices which
  166. can be consumed independently:
  167. [source,console]
  168. --------------------------------------------------
  169. GET /twitter/_search?scroll=1m
  170. {
  171. "slice": {
  172. "id": 0, <1>
  173. "max": 2 <2>
  174. },
  175. "query": {
  176. "match": {
  177. "title": "elasticsearch"
  178. }
  179. }
  180. }
  181. GET /twitter/_search?scroll=1m
  182. {
  183. "slice": {
  184. "id": 1,
  185. "max": 2
  186. },
  187. "query": {
  188. "match": {
  189. "title": "elasticsearch"
  190. }
  191. }
  192. }
  193. --------------------------------------------------
  194. // TEST[setup:big_twitter]
  195. <1> The id of the slice
  196. <2> The maximum number of slices
  197. The result from the first request returned documents that belong to the first slice (id: 0) and the result from the
  198. second request returned documents that belong to the second slice. Since the maximum number of slices is set to 2
  199. the union of the results of the two requests is equivalent to the results of a scroll query without slicing.
  200. By default the splitting is done on the shards first and then locally on each shard using the _id field
  201. with the following formula:
  202. `slice(doc) = floorMod(hashCode(doc._id), max)`
  203. For instance if the number of shards is equal to 2 and the user requested 4 slices then the slices 0 and 2 are assigned
  204. to the first shard and the slices 1 and 3 are assigned to the second shard.
  205. Each scroll is independent and can be processed in parallel like any scroll request.
  206. NOTE: If the number of slices is bigger than the number of shards the slice filter is very slow on the first calls, it has a complexity of O(N) and a memory cost equals
  207. to N bits per slice where N is the total number of documents in the shard.
  208. After few calls the filter should be cached and subsequent calls should be faster but you should limit the number of
  209. sliced query you perform in parallel to avoid the memory explosion.
  210. To avoid this cost entirely it is possible to use the `doc_values` of another field to do the slicing
  211. but the user must ensure that the field has the following properties:
  212. * The field is numeric.
  213. * `doc_values` are enabled on that field
  214. * Every document should contain a single value. If a document has multiple values for the specified field, the first value is used.
  215. * The value for each document should be set once when the document is created and never updated. This ensures that each
  216. slice gets deterministic results.
  217. * The cardinality of the field should be high. This ensures that each slice gets approximately the same amount of documents.
  218. [source,console]
  219. --------------------------------------------------
  220. GET /twitter/_search?scroll=1m
  221. {
  222. "slice": {
  223. "field": "date",
  224. "id": 0,
  225. "max": 10
  226. },
  227. "query": {
  228. "match": {
  229. "title": "elasticsearch"
  230. }
  231. }
  232. }
  233. --------------------------------------------------
  234. // TEST[setup:big_twitter]
  235. For append only time-based indices, the `timestamp` field can be used safely.
  236. NOTE: By default the maximum number of slices allowed per scroll is limited to 1024.
  237. You can update the `index.max_slices_per_scroll` index setting to bypass this limit.