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?
Practice
What does the from parameter control in an Elasticsearch query?
Solution
Step 1: Understand the role of
Thefromfromparameter sets the offset, meaning it tells Elasticsearch where to start returning results from.Step 2: Differentiate from
Thesizesizeparameter controls how many results to return, not the starting point.Final Answer:
The starting index of the search results to return -> Option BQuick Check:
from= start index [OK]
- Confusing from with size
- Thinking from controls sorting
- Assuming from limits total results
Which of the following is the correct way to request the second page of results with 10 items per page using from and size?
{ "from": ?, "size": 10 }Solution
Step 1: Calculate the starting index for page 2
Page 1 has items 0-9, so page 2 starts at index 10 (10 items per page).Step 2: Set
This returns items 10 to 19, which is the second page.fromto 10 andsizeto 10Final Answer:
10 -> Option AQuick Check:
Page 2 start = (2-1)*10 = 10 [OK]
- Using 1 instead of 10 for from
- Using 11 which skips one item
- Using 20 which is page 3 start
Given this Elasticsearch query snippet, what range of results will be returned?
{ "from": 5, "size": 3 }Solution
Step 1: Identify the starting index
Thefromvalue is 5, so results start from index 5.Step 2: Calculate the ending index
Withsize3, results include indexes 5, 6, and 7.Final Answer:
Results 5 to 7 -> Option CQuick Check:
from=5, size=3 -> 5,6,7 [OK]
- Assuming from is count, not start
- Off-by-one errors in range
- Mixing up start and end indexes
Identify the error in this Elasticsearch pagination query:
{ "from": -5, "size": 10 }Solution
Step 1: Check the value of
Thefromfromparameter must be zero or positive; negative values are invalid.Step 2: Validate
Thesizevaluesizevalue 10 is valid and does not cause error.Final Answer:
"from" cannot be negative -> Option DQuick Check:
Negative from is invalid [OK]
- Allowing negative from values
- Confusing size limits
- Ignoring validation rules
You want to display search results page by page with 15 results each. If the user requests page 4, what should the from and size values be to get the correct results?
Solution
Step 1: Calculate the starting index for page 4
Using the formulafrom = (page_number - 1) * size, we get (4 - 1) * 15 = 45.Step 2: Set the size to 15
Since each page shows 15 results,sizeremains 15.Final Answer:
{ "from": 45, "size": 15 } -> Option AQuick Check:
from = 45, size = 15 for page 4 [OK]
- Using from = page * size
- Swapping from and size values
- Setting size to page number
Ready to prove it?
Pass the test and this pattern moves unknown
