0
0
Elasticsearchquery~5 mins

Search after for efficient pagination in Elasticsearch

Choose your learning style9 modes available
Introduction

Search after helps you get the next pages of results quickly without slowing down. It avoids repeating work by remembering where you left off.

When you want to show many pages of search results without delay.
When your list of results is very large and normal pagination is slow.
When you want to avoid skipping or missing results between pages.
When you want consistent order in your search results across pages.
Syntax
Elasticsearch
{
  "query": { ... },
  "sort": [
    {"field1": "asc"},
    {"_id": "asc"}
  ],
  "search_after": [value1, value2],
  "size": 10
}

You must sort your results by one or more fields to use search_after.

The search_after array uses the last values from the previous page's sort fields.

Examples
This gets the first 5 results sorted by timestamp and then by ID.
Elasticsearch
{
  "query": { "match_all": {} },
  "sort": [
    {"timestamp": "asc"},
    {"_id": "asc"}
  ],
  "size": 5
}
This gets the next 5 results after the last one with timestamp '2023-06-01T12:00:00' and ID 'abc123'.
Elasticsearch
{
  "query": { "match_all": {} },
  "sort": [
    {"timestamp": "asc"},
    {"_id": "asc"}
  ],
  "search_after": ["2023-06-01T12:00:00", "abc123"],
  "size": 5
}
Sample Program

This example shows how to get the first 3 results sorted by date and ID, then use search_after with the last result's sort values to get the next 3 results.

Elasticsearch
POST /my-index/_search
{
  "query": { "match_all": {} },
  "sort": [
    {"date": "asc"},
    {"_id": "asc"}
  ],
  "size": 3
}

# Assume the last hit has sort values ["2023-06-01T00:00:00", "id3"]

POST /my-index/_search
{
  "query": { "match_all": {} },
  "sort": [
    {"date": "asc"},
    {"_id": "asc"}
  ],
  "search_after": ["2023-06-01T00:00:00", "id3"],
  "size": 3
}
OutputSuccess
Important Notes

Always include a unique field like _id in your sort to avoid duplicates.

Do not use from with large offsets; search_after is more efficient.

You need the sort values from the last hit of the previous page to use search_after.

Summary

Search after helps you page through large search results efficiently.

You must sort results and use the last sort values to get the next page.

This method avoids slow queries and missing results in deep pagination.