scroll.asciidoc 11 KB

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