I am little bit confused over Elasticsearch by its scroll functionality. In elasticsearch is it possible to call search API everytime whenever the user scrolls on the result set? From documentation
"search_type" => "scan", // use search_type=scan
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 50, // how many results *per shard* you want back
Is that mean it will perform search for every 30 seconds and returns all the sets of results until there is no records?
For example my ES returns total 500 records. I am getting an data from ES as two sets of records each with 250 records. Is there any way I can display first set of 250 records first, when user scrolls then second set of 250 records.Please suggest
What you described as an example use case is actually search results pagination, which is available for any search query and is limited by 10k results.
scroll
requests are needed for the cases when you need to go over that 10k limit, withscroll
query you can fetch even the entire collection of documents.Probably the source of confusion here is that
scroll
term is ambiguous: it means the type of a query, and also it is a name of a parameter of such query (as was mentioned in other comments, it is time ES will keep waiting for you to fetch next chunk of scrolling).scroll
queries are heavy, and should be avoided until absolutely necessary. In fact, in the docs it says:Now regarding your another question:
Yes, even several parallel scroll requests are possible:
The documentation of the Scroll API at elastic explains this behaviour also.
The result size of 10k is a default value and can be overwritten during runtime, if necessary:
The life time of the scroll id is defined in each scroll request with the parameter "scroll", e.g.
What you are looking for is pagination.
You can achieve your objective by querying for a fixed size and setting the
from
parameter. Since you want to set display in batches of 250 results, you can setsize = 250
and with each consecutive query, increment the value offrom
by250
.On the contrary,
Scan & scroll
lets you retrieve a large set of results with a single search and is ideally meant for operations like re-indexing data into a new index. Using it for displaying search results in real-time is not recommended.To explain
Scan & scroll
briefly, what it essentially does is that it scans the index for the query provided with the scan request and returns ascroll_id
. Thisscroll_id
can be passed to the next scroll request to return the next batch of results.Consider the following example-
In above example, following events happen-
scroll_id
(received in the previous scroll request) is sent and next batch of results is returned.You are understanding wrong the purpose of the
scroll
property. It does not mean that elasticsearch will fetch next page data after 30 seconds. When you are doing first scroll request you need to specify when scroll context should be closed.scroll
parameter is telling to close scroll context after 30 seconds.After doing first scroll request you will get back
scroll_id
parameter in response. For next pages you need to pass that value to get next page of the scroll response. If you will not do the next scroll request within 30 seconds, the scroll request will be closed and you will not be able to get next pages for that scroll request.