0
0
Elasticsearchquery~3 mins

Why Search after for efficient pagination in Elasticsearch? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could flip through millions of items instantly without waiting?

The Scenario

Imagine you have a huge list of products in an online store. You want to show customers page by page, but each time you ask the system to find page 10 or 20, it has to count and skip all the previous items first.

The Problem

This manual way is slow because the system must look through all earlier pages again and again. It also uses a lot of memory and can cause delays, making customers wait longer and feel frustrated.

The Solution

Using search after lets the system remember where it left off. Instead of counting from the start every time, it jumps directly to the next set of results, making pagination fast and smooth even with millions of items.

Before vs After
Before
GET /products/_search
{
  "from": 1000, "size": 10,
  "query": { "match_all": {} }
}
After
GET /products/_search
{
  "size": 10,
  "search_after": ["last_sort_value"],
  "sort": ["price"]
}
What It Enables

This technique enables lightning-fast page navigation through huge data sets without slowing down or crashing.

Real Life Example

Think of scrolling through thousands of social media posts or product reviews where you want instant loading of the next page without waiting.

Key Takeaways

Manual pagination with offsets is slow and resource-heavy.

Search after jumps directly to the next page using the last item's sort value.

This makes browsing large data sets fast and user-friendly.