0
0
ElasticsearchHow-ToBeginner · 4 min read

How to Use from and size for Pagination in Elasticsearch

In Elasticsearch, use the from parameter to skip a number of results and size to limit how many results to return. Together, they enable pagination by controlling which slice of the search results you get.
📐

Syntax

The from parameter sets the starting point (offset) of the results to return, and size sets how many results to fetch from that point.

Example parameters:

  • from: 10 skips the first 10 results.
  • size: 5 returns 5 results after skipping.
json
{
  "from": 10,
  "size": 5,
  "query": {
    "match_all": {}
  }
}
💻

Example

This example fetches the second page of results assuming 5 results per page. It skips the first 5 results (from: 5) and returns the next 5 (size: 5).

json
{
  "from": 5,
  "size": 5,
  "query": {
    "match": {
      "title": "database"
    }
  }
}
Output
{ "hits": { "total": { "value": 23, "relation": "eq" }, "hits": [ {"_id": "6", "_source": {"title": "Database Basics"}}, {"_id": "7", "_source": {"title": "Advanced Database"}}, {"_id": "8", "_source": {"title": "Database Optimization"}}, {"_id": "9", "_source": {"title": "NoSQL Database"}}, {"_id": "10", "_source": {"title": "SQL Database"}} ] } }
⚠️

Common Pitfalls

Common mistakes when using from and size include:

  • Setting from too high, which can cause slow queries because Elasticsearch must skip many results internally.
  • Not setting size, which defaults to 10 and may return fewer results than expected.
  • Using from and size for deep pagination (very large from), which is inefficient and can degrade performance.

For deep pagination, consider using search_after or scroll APIs instead.

json
/* Wrong: deep pagination with large from */
{
  "from": 10000,
  "size": 10,
  "query": { "match_all": {} }
}

/* Better: use search_after for deep pagination */
{
  "size": 10,
  "query": { "match_all": {} },
  "search_after": ["last_sort_value"]
}
📊

Quick Reference

ParameterDescriptionDefault
fromNumber of results to skip (offset)0
sizeNumber of results to return10

Key Takeaways

Use from to skip results and size to limit how many results you get.
Avoid very large from values to prevent slow queries.
Default size is 10 if not specified.
For deep pagination, prefer search_after or scroll APIs over large from.
Together, from and size let you page through search results easily.