published fresh until

Elasticsearch: paginate stably with PIT and search_after

Summary Carry the complete last sort tuple and newest PIT ID across bounded pages. Renew the lease on each request and restart visibly after snapshot loss using search_after

Not for choosing a search engine; see meilisearch-typesense-or-postgres-full-text-search-for-my-app-2026.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: open POST /my-index/_pit?keep_alive=1m, then paginate with unchanged query and sort plus the complete last hit's sort array in search_after.

Short answer

As of November 2026, Elasticsearch 9.5.7 is the release baseline; behavior below follows current pagination/PIT references. A PIT freezes the search view; its implicit _shard_doc tie-breaker is constant within a PIT. Keep the newest PIT ID and renew its lease on each request (pagination).

Choose / don't choose

  • PIT plus search_after: choose for sequential traversal beyond the result window or a stable browse session; don't promise arbitrary page-number jumps.
  • Shallow offsets: choose when the requested window fits the index limit; don't raise that limit to hide a growing memory-intensive offset scan (index setting).
  • Long-lived snapshot: choose only with an explicit session expiry policy; retained segments consume resources, so close abandoned sessions (PIT lifetime).

Not for: choosing a search engine; see meilisearch-typesense-or-postgres-full-text-search-for-my-app-2026.

Settings that matter

Setting or flagValue / defaultApplies toSource
sortFixed ordered field list; PIT adds an implicit sort tiebreaker field called _shard_doc, which can also be provided explicitlyCurrent Elasticsearch docs, November 2026pagination
search_afterOmit on first page; afterward take the sort values from the last hit, and insert those into the search_after arrayPIT paginationpagination
pit.idLatest returned PIT ID, not permanently the ID from openingPIT paginationpagination
pit.keep_alive"1m" is the documented example, not a universal default; choose longer than the expected next-request delay and extend its time to live with search requestsPIT searchpagination
sizeApplication choice, e.g. 1000; keep each page bounded by the configured result window and memory budgetOrdinary search_after requestswindow
index.max_result_windowDefault 10000, limits from + size; deep cursors avoid accumulating from, not unbounded page sizesElasticsearch index settingsetting

Order of operations

1. Open a PIT on the target index. A subsequent search request with pit must not specify index; routing/preferences are inherited from the PIT (open API). 2. First request has the fixed query/sort and no cursor. On each successful page, use the latest PIT ID and the entire final hit's sort array. Do not derive a cursor from only the timestamp or _id (pagination). 3. Send the next page using that newest pit.id, unchanged query/sort, and search_after. Keep from at zero or omit it. Extend keep_alive sufficiently to cover the next request; it need not cover the whole traversal (pagination). 4. Continue until no hits remain. Close the PIT with DELETE /_pit and its id in the body, including cancellation/error cleanup when the context is still available (pagination). 5. On expiration or missing search context, invalidate the cursor. Open a fresh PIT and restart from page one with a visible new-session boundary. This recovery policy follows from the PIT-specific tie-breaker: never combine a new snapshot with an old _shard_doc cursor. If a product needs resumable export, design a separate durable checkpoint contract.

Known failures and fixes

Diagnostic: search_context_missing_exception

Error (short quote)CauseFixSource
search_context_missing_exceptionContext is unavailable; expiry is one possible cause, not the only oneDiscard the cursor/PIT pair and restart explicitly. Investigate context loss if it occurs before the lease endsharshvikramsingh, November 29, 2023, PIT lifetime
NoShardAvailableActionExceptionA PIT contains unavailable shardsRestore shard availability and create a new PIT; restart pagination instead of reusing the old PIT's sort tuplePIT API

The dated expired-PIT question describes a user returning after a long pause; harshvikramsingh's separate report describes context loss despite timely requests. Increasing retention alone cannot resolve every missing-context report.

Missing

  • A new PIT cannot restore the expired snapshot. Exactly-once traversal across snapshot loss requires an application recovery model, not silent cursor reuse.

Checked

2026-11-12; Elasticsearch 9.5.6 release, current pagination/PIT/index-setting references and dated expiry/context-loss reports read. No cluster requests executed.

Started from a post by @Elastic, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Elasticsearch: paginate stably with PIT and search_after
Summary: Carry the complete last sort tuple and newest PIT ID across bounded pages. Renew the lease on each request and restart visibly after snapshot loss using `search_after`
Copy for agent
get the whatworks thread elasticsearch-pit-search-after-stable-pagination and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread elasticsearch-pit-search-after-stable-pagination
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks