Hard search elasticsearch

Why is deep pagination with from and size expensive, and when do you use search_after, a point in time or scroll?

The 30 second answer

For page from plus size, every shard must find its top from-plus-size hits and the coordinating node merges them all, so cost grows with depth. That's why index.max_result_window defaults to 10,000. For deep or infinite scrolling I use search_after with a unique tiebreaker, plus a point in time when pages must stay consistent. Scroll is no longer recommended for deep pagination; it's for batch exports.

Could you say this in an interview?

Why depth costs. An index is split into shards. To return hits 9,980 to 10,000 sorted by score, Elasticsearch can't know which shard holds them, so each shard returns its own top 10,000, and the coordinating node merges shards times 10,000 entries to keep 20. Page 500 costs far more than page 1.

The guardrail. The docs: "By default, you cannot use from and size to page through more than 10,000 hits. This limit is a safeguard set by the index.max_result_window index setting." Raising it moves the cost, it doesn't remove it. Zalando's outage write-up names max_result_window as one of the guardrails so no single request can ask for a "scroll the universe"-sized result.

The three options.

  • **from/size**: fine for the first pages a human looks at.
  • **search_after**: the request passes the sort values of the last hit, so each shard only needs size hits after that point. Cost stays flat with depth. You can't jump to page 37, only go to the next page. The sort needs a unique tiebreaker; with a PIT, Elasticsearch adds an implicit _shard_doc tiebreaker.
  • Point in time (PIT): "If a refresh occurs between these requests, the order of your results may change." A PIT preserves the index state across requests. It keeps old segments alive, so use a short keep_alive and close it when done.
  • Scroll: the docs say "We no longer recommend using the scroll API for deep pagination", and scrolling "is not intended for real time user requests". Use it, or better search_after with a PIT, for batch exports and reindex jobs.

Badly written. An app that lets shoppers or a crawler hit page 400 directly:

GET products/_search
{ "from": 19152, "size": 48, "sort": [ { "price": "asc" } ],
  "query": { "term": { "category_id": "shoes" } } }

Better. Open a PIT once, then page with search_after:

POST products/_pit?keep_alive=1m

GET _search
{ "size": 48,
  "query": { "term": { "category_id": "shoes" } },
  "pit": { "id": "<pit id>", "keep_alive": "1m" },
  "sort": [ { "price": "asc" } ],
  "search_after": [ 4995, 4294967298 ] }

The second value in search_after is the implicit _shard_doc tiebreaker returned in the previous page's last hit.

Trade-offs.

  • No random page access. Product teams often accept "load more" or cap visible pages (for example only the first N pages are linkable).
  • PITs and scrolls hold resources on the data nodes. Many concurrent long-lived PITs, one per shopper, can be a memory problem; often plain search_after without a PIT is acceptable for shoppers, keeping the tiebreaker.
  • Clamp page size and page depth in the search API layer, not only in Elasticsearch.

Alternate interview scenarios

"A partner needs a nightly export of the whole catalog. How?"

search_after with a PIT, sorted by _shard_doc or a unique key, large pages, _source limited to the needed fields, run against replicas or off-peak. Not from/size, and not scroll for new code.

"Bots crawl page 900 of every category and the cluster suffers."

Enforce max_result_window, clamp page depth in the API, rate-limit the client, and give crawlers a sitemap or feed instead of deep listing pages. A Postgres version of the same problem, with real numbers from Shopify, is in the OFFSET question on the API latency page.

Sources (3)
  1. Elasticsearch docs: Paginate search results (from/size limit, search_after, PIT, scroll)elastic.co
  2. Elasticsearch docs: Open point in time APIelastic.co
  3. Zalando Engineering: The Day Our Own Queries DoS'ed Us: Inside Zalando Search (December 2025)engineering.zalando.com