Pagination (from/size) in Elasticsearch - Time & Space Complexity
When using pagination with from and size in Elasticsearch, we want to know how the time to get results changes as we ask for later pages.
We ask: How does the search time grow when we skip more results?
Analyze the time complexity of the following Elasticsearch pagination query.
GET /my_index/_search
{
"from": 1000,
"size": 10,
"query": {
"match_all": {}
}
}
This query skips the first 1000 results and returns the next 10 matching documents.
Look at what Elasticsearch does internally to get the results.
- Primary operation: It scans through documents to skip the first
fromresults. - How many times: It processes roughly
from + sizedocuments to return the page.
As from grows, Elasticsearch must skip more documents before returning results.
| Input Size (from) | Approx. Operations |
|---|---|
| 10 | About 20 documents processed (10 skipped + 10 returned) |
| 100 | About 110 documents processed |
| 1000 | About 1010 documents processed |
Pattern observation: The work grows roughly linearly as from increases.
Time Complexity: O(from + size)
This means the time to get a page grows roughly with how many results you skip plus how many you want to see.
[X] Wrong: "Pagination time stays the same no matter how far we go in the results."
[OK] Correct: Elasticsearch must still scan and skip all earlier results, so more skipping means more work and longer time.
Understanding how pagination affects search time helps you design better queries and explain performance in real projects.
What if we replaced from with a search_after parameter? How would the time complexity change?