0
0
Elasticsearchquery~5 mins

Pagination (from/size) in Elasticsearch

Choose your learning style9 modes available
Introduction

Pagination helps you get a small part of many results at a time. It makes searching faster and easier to handle.

When you want to show search results page by page on a website.
When you have many records and want to load only a few at once to save memory.
When you want users to scroll through results without waiting for all data to load.
When you want to limit the number of results returned to avoid overload.
When you want to jump to a specific part of the search results.
Syntax
Elasticsearch
{
  "from": <start_index>,
  "size": <number_of_results>,
  "query": {
    "match_all": {}
  }
}

from is the starting point (0 means first result).

size is how many results to get from that point.

Examples
Get the first 5 results.
Elasticsearch
{
  "from": 0,
  "size": 5,
  "query": {
    "match_all": {}
  }
}
Skip first 10 results and get next 10 results where title matches 'book'.
Elasticsearch
{
  "from": 10,
  "size": 10,
  "query": {
    "match": {
      "title": "book"
    }
  }
}
Sample Program

This query asks Elasticsearch to skip the first 3 results and return the next 2 results from all documents.

Elasticsearch
{
  "from": 3,
  "size": 2,
  "query": {
    "match_all": {}
  }
}
OutputSuccess
Important Notes

Using a large from value can slow down your search because Elasticsearch still has to scan all skipped results.

For deep pagination, consider using search_after or scroll API for better performance.

Summary

Pagination controls which part of results you get using from and size.

from is the start index, size is how many results to return.

Use pagination to show results page by page and improve speed.