|
| 1 | +[[java-rest-high-search-scroll]] |
| 2 | +=== Search Scroll API |
| 3 | + |
| 4 | +The Scroll API can be used to retrieve a large number of results from |
| 5 | +a search request. |
| 6 | + |
| 7 | +In order to use scrolling, several steps need to be executed in a given order: |
| 8 | + |
| 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 | + |
| 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. |
| 22 | + |
| 23 | +* Finally, the last scroll identifier can be deleted using the <<java-rest-high-clear-scroll>> |
| 24 | +in order to release the search context. |
| 25 | + |
| 26 | +[[java-rest-high-search-scroll-example]] |
| 27 | +==== Example of Execution |
| 28 | + |
| 29 | +Here is an example of a scrolled search: |
| 30 | + |
| 31 | +["source","java",subs="attributes,callouts,macros"] |
| 32 | +-------------------------------------------------- |
| 33 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-example] |
| 34 | +-------------------------------------------------- |
| 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>>. |
| 51 | + |
| 52 | +==== Optional arguments |
| 53 | +The following argument can optionally be provided: |
| 54 | + |
| 55 | +["source","java",subs="attributes,callouts,macros"] |
| 56 | +-------------------------------------------------- |
| 57 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[scroll-request-scroll] |
| 58 | +-------------------------------------------------- |
| 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` |
| 61 | + |
| 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 |
| 64 | +initial search request). |
| 65 | + |
| 66 | +[[java-rest-high-search-scroll-sync]] |
| 67 | +==== Synchronous Execution |
| 68 | + |
| 69 | +["source","java",subs="attributes,callouts,macros"] |
| 70 | +-------------------------------------------------- |
| 71 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-execute-sync] |
| 72 | +-------------------------------------------------- |
| 73 | + |
| 74 | +[[java-rest-high-search-scroll-async]] |
| 75 | +==== Asynchronous Execution |
| 76 | + |
| 77 | +["source","java",subs="attributes,callouts,macros"] |
| 78 | +-------------------------------------------------- |
| 79 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-scroll-execute-async] |
| 80 | +-------------------------------------------------- |
| 81 | +<1> Called when the execution is successfully completed. The response is |
| 82 | +provided as an argument |
| 83 | +<2> Called in case of failure. The raised exception is provided as an argument |
| 84 | + |
| 85 | + |
| 86 | +[[java-rest-high-clear-scroll]] |
| 87 | +=== Clear Scroll API |
| 88 | + |
| 89 | +The search contexts used by the Scroll API are automatically deleted when the scroll |
| 90 | +times out. But it is advised to release search contexts as soon as they are not |
| 91 | +necessary anymore using the Clear Scroll API. |
| 92 | + |
| 93 | +[[java-rest-high-clear-scroll-request]] |
| 94 | +==== Clear Scroll Request |
| 95 | + |
| 96 | +A `ClearScrollRequest` can be created as follows: |
| 97 | + |
| 98 | +["source","java",subs="attributes,callouts,macros"] |
| 99 | +-------------------------------------------------- |
| 100 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-request] |
| 101 | +-------------------------------------------------- |
| 102 | +<1> Create a new `ClearScrollRequest` |
| 103 | +<2> Adds a scroll id to the list of scroll identifiers to clear |
| 104 | + |
| 105 | +==== Providing the scroll identifiers |
| 106 | +The `ClearScrollRequest` allows to clear one or more scroll identifiers in a single request. |
| 107 | + |
| 108 | +The scroll identifiers can be added to the request one by one: |
| 109 | + |
| 110 | +["source","java",subs="attributes,callouts,macros"] |
| 111 | +-------------------------------------------------- |
| 112 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-add-scroll-id] |
| 113 | +-------------------------------------------------- |
| 114 | + |
| 115 | +Or all together using: |
| 116 | + |
| 117 | +["source","java",subs="attributes,callouts,macros"] |
| 118 | +-------------------------------------------------- |
| 119 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-add-scroll-ids] |
| 120 | +-------------------------------------------------- |
| 121 | + |
| 122 | +[[java-rest-high-clear-scroll-sync]] |
| 123 | +==== Synchronous Execution |
| 124 | + |
| 125 | +["source","java",subs="attributes,callouts,macros"] |
| 126 | +-------------------------------------------------- |
| 127 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-execute] |
| 128 | +-------------------------------------------------- |
| 129 | + |
| 130 | +[[java-rest-high-clear-scroll-async]] |
| 131 | +==== Asynchronous Execution |
| 132 | + |
| 133 | +["source","java",subs="attributes,callouts,macros"] |
| 134 | +-------------------------------------------------- |
| 135 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-execute-async] |
| 136 | +-------------------------------------------------- |
| 137 | +<1> Called when the execution is successfully completed. The response is |
| 138 | +provided as an argument |
| 139 | +<2> Called in case of failure. The raised exception is provided as an argument |
| 140 | + |
| 141 | +[[java-rest-high-clear-scroll-response]] |
| 142 | +==== Clear Scroll Response |
| 143 | + |
| 144 | +The returned `ClearScrollResponse` allows to retrieve information about the released |
| 145 | + search contexts: |
| 146 | + |
| 147 | +["source","java",subs="attributes,callouts,macros"] |
| 148 | +-------------------------------------------------- |
| 149 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[clear-scroll-response] |
| 150 | +-------------------------------------------------- |
| 151 | +<1> Return true if the request succeeded |
| 152 | +<2> Return the number of released search contexts |
0 commit comments