Using the search_after functionality
Elasticsearch's standard pagination, using from
and size
, performs very poorly on large datasets because, for every query, you need to compute and discard all of the results before the from
value. Scrolling doesn't have this problem, but it consumes a lot due to memory search contexts; therefore, it cannot be used for frequent user queries.
To bypass these problems, Elasticsearch 5.x, and greater, provides the search_after
functionality. This provides faster skipping for scrolling results.
Getting ready
You will need an up-and-running Elasticsearch installation, as described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
To execute these commands, any HTTP client can be used, such as Curl (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. I suggest using the Kibana console as it provides code completion and better character escaping for Elasticsearch...