AI Hints
Home›Elasticsearch Tutorial›Deep Paging, collapse and Geo
Elasticsearch · Lesson 85 of 120

Deep Paging with search_after

Past 10,000.

⏱ 2 min read▶ Video at 1:19:56🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

From and size work for the first few pages. But going to page five thousand means sorting and throwing away fifty thousand results. Search after jumps straight to the next page.

  • Page 5,000?
  • Throws away results
  • search_after

Next page with search_after

Sort by price, and by ID as a tie breaker. Each hit returns its sort values. For the next page, we pass the last hit's sort values in search after. Run it. Page two starts right after the last item of page one.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._source,hits.hits.sort
{
  "size": 2,
  "sort": [ { "price": "asc" }, { "id": "asc" } ],
  "_source": ["name"],
  "search_after": [28.99, 33]
}
Output
{
  "hits": {
    "hits": [
      {
        "_source": {"name": "Bright 400 Headlamp"},
        "sort": [32.99, 39]
      },
      {
        "_source": {"name": "Solar Camp Lantern"},
        "sort": [33.99, 42]
      }
    ]
  }
}
Also uses _load
catalog

Deep paging

Always sort by something unique as the last tie breaker. Pass the sort values of the last hit. For a stable view while paging, open a point in time first. And this is how you export a whole index, page by page.

keyvalue
Tie-breakerunique field last
search_afterlast hit's sort
PITstable snapshot
Exportpage through all

Summary

Search after pages by the sort values of the last hit, so it's fast at any depth. Next, one result per group with collapse.

Key takeaways
  • search_after
  • sort values
  • Point in time
Next up: collapse — one result per group