Challenge - 5 Problems
Search After Pagination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of search_after with sort keys
What will be the output of the following Elasticsearch query snippet when using
Assume the index contains documents sorted by
Given the documents:
Which documents will be returned?
search_after for pagination?Assume the index contains documents sorted by
timestamp ascending and id ascending.{
"size": 2,
"query": { "match_all": {} },
"sort": [
{ "timestamp": "asc" },
{ "id": "asc" }
],
"search_after": ["2023-01-01T00:00:00", "100"]
}Given the documents:
- {"timestamp": "2023-01-01T00:00:00", "id": "100"}
- {"timestamp": "2023-01-01T00:00:01", "id": "101"}
- {"timestamp": "2023-01-01T00:00:02", "id": "102"}
Which documents will be returned?
Elasticsearch
{
"size": 2,
"query": { "match_all": {} },
"sort": [
{ "timestamp": "asc" },
{ "id": "asc" }
],
"search_after": ["2023-01-01T00:00:00", "100"]
}Attempts:
2 left
💡 Hint
Remember that search_after returns documents after the given sort values, excluding the document with those exact values.
✗ Incorrect
The search_after parameter returns documents strictly after the given sort values. Since the sort is by timestamp ascending and then id ascending, documents with timestamp "2023-01-01T00:00:00" and id "100" are excluded. The next documents are those with timestamp "2023-01-01T00:00:01" and id "101", and then "2023-01-01T00:00:02" and id "102".
🧠 Conceptual
intermediate1:30remaining
Why use search_after instead of from/size for deep pagination?
Why is
search_after recommended over from and size for deep pagination in Elasticsearch?Attempts:
2 left
💡 Hint
Think about how Elasticsearch handles skipping documents with from/size.
✗ Incorrect
Using from/size for deep pagination requires Elasticsearch to skip many documents, which is slow and resource-intensive. search_after uses the last document's sort values to continue efficiently without skipping.
🔧 Debug
advanced2:00remaining
Identify the error in search_after usage
What error will this Elasticsearch query produce?
Assuming the
{
"size": 3,
"query": { "match_all": {} },
"sort": [
{ "date": "desc" }
],
"search_after": ["2023-05-01"]
}Assuming the
date field is of type date and the sort expects a date and a tie-breaker field.Elasticsearch
{
"size": 3,
"query": { "match_all": {} },
"sort": [
{ "date": "desc" }
],
"search_after": ["2023-05-01"]
}Attempts:
2 left
💡 Hint
search_after requires the sort fields and search_after values to match exactly in number and order.
✗ Incorrect
When using search_after, the sort array must include a tie-breaker field (like _id) to ensure unique sorting. The search_after array must have the same number of values as the sort fields. Missing the tie-breaker causes an error.
📝 Syntax
advanced1:30remaining
Correct search_after syntax for multi-field sort
Which of the following is the correct
search_after syntax for this sort?"sort": [
{ "price": "asc" },
{ "rating": "desc" },
{ "_id": "asc" }
]Attempts:
2 left
💡 Hint
The order of values in search_after must match the order of fields in sort.
✗ Incorrect
The search_after array must have values in the same order as the sort fields: price (asc), rating (desc), then _id (asc). So the values are [price_value, rating_value, _id_value].
🚀 Application
expert3:00remaining
Implementing efficient deep pagination with search_after
You have an Elasticsearch index with millions of documents sorted by
You want to fetch page 1000 with 10 documents per page efficiently.
Which approach correctly uses
created_at ascending and doc_id ascending.You want to fetch page 1000 with 10 documents per page efficiently.
Which approach correctly uses
search_after to get page 1000?Attempts:
2 left
💡 Hint
search_after requires the last document's sort values from the previous page to continue.
✗ Incorrect
search_after requires the last document's sort values from the previous page to fetch the next page. To get page 1000, you must iterate through pages 1 to 999, saving the last sort values each time, then use those values to query page 1000.