Pagination works fine until a user (or a crawler, or an export job) reaches page 501:
"type": "illegal_argument_exception",
"reason": "Result window is too large, from + size must be less than or equal to: [10000]
but was [10020]. See the scroll api for a more efficient way to request large data sets.
This limit can be set by changing the [index.max_result_window] index level setting."
The error suggests raising the limit. Don't. There are better tools for deep pagination and for reading everything.
Why from + size has a limit
A search with from: 10000, size: 20 has to find the top 10,020 hits. On a sharded index, every shard returns its own top 10,020, and the coordinating node sorts them all together to keep 20. Cost and memory grow with the page depth times the number of shards. index.max_result_window (10,000 by default) keeps one deep page from overloading the cluster.
Raising it to 1,000,000 removes the protection, and then the deep-page requests still get slower and more expensive with each page.
For users: limit the depth
People rarely go past page 10 of search results. Common UX answers:
- show "10,000+ results" and cap navigation (Google does this too);
- encourage refining the search with filters and facets instead of paging deeper;
- use infinite scroll or "load more" with
search_after(below) instead of numbered pages.
For deep or sequential paging: search_after + point in time
search_after works like keyset pagination: pass the sort values of the last hit instead of an offset. Each page costs the same, however deep it is.
Open a point in time (PIT) so that all pages see the same snapshot of the index, even while documents are being written:
POST /products/_pit?keep_alive=2m
→ { "id": "46ToAwMDaWR5BXV1aWQy..."
