Course outline

Elasticsearch / Advanced: resilient and recoverable retrieval / Lesson 21

Manage the lifecycle of an async search

Submit, inspect, and clean up a long-running search without treating partial results as final.

About 4 min readContent reviewed
On this page

Lesson goals

  • Explain async search state and partial results.
  • Use an identifier to retrieve and clean up an isolated search.
  • Define client polling, authorization, and resource boundaries.

Define the result contract

An asynchronous search can return progress and partial results while the search continues. The application must distinguish a running response from a completed result and decide whether partial information is suitable for display. Record the intended index scope, user authorization, query, timeout, and retention needs. An asynchronous interface does not make an expensive query free or guarantee that a partial aggregation is complete.

Submit a lab search

The request below is illustrative for an existing synthetic lab-artifacts index on an isolated Elasticsearch 8.19 cluster. It requests a short initial wait and asks to retain a completed response. The result can complete immediately or continue running depending on the environment. Use only authorized lab data; the public course does not execute this request or claim an actual search identifier.

http Submit an async search
POST /lab-artifacts/_async_search?wait_for_completion_timeout=1s&keep_on_completion=true
{ "query": { "match_all": {} }, "size": 10 }

The request searches the synthetic index and asks for up to ten hits. Inspect the actual response state and identifier when provided; no timing, output, or resource usage is asserted.

Retrieve and delete by identifier

When the response provides a retained search identifier, substitute it for the angle-bracketed path component in the following illustrative requests. Retrieve progress with the appropriate authorized identity and inspect whether the search is still running or its result is partial. Delete the retained search when it is no longer needed according to the application lifecycle. The identifier is sensitive operational context and should not be exposed unnecessarily.

http Inspect and clean up the retained search
GET /_async_search/<async-search-id>

DELETE /_async_search/<async-search-id>

The path component is a value returned by the lab submission, not a literal identifier. GET retrieves the search state; DELETE requests cleanup or cancellation according to the documented API behavior. Responses depend on current state and permissions.

Handle partial and expired results

A client can disconnect, lose the identifier, receive a partial response, or return after retention has expired. Define bounded polling, an overall deadline, user-visible state, and how the application handles expired or failed searches. Check shard failures and completeness rather than equating an HTTP success with a complete result. Protect the search identifier and ensure the same authorization boundary is applied to retrieval and cleanup.

Measure the whole workflow

In an isolated environment, test a search that completes quickly and one that remains running long enough to poll. Verify the transition to a final result, cleanup, and handling of an invalid or expired identifier. Compare final results with an equivalent ordinary search on a stable fixture when appropriate. Monitor query cost and retained-result resources; increasing polling frequency does not make the underlying search cheaper.

Common errors and fixes

A partial aggregation is displayed as final.

Why it happens
The client ignores the running or partial response state.
How to fix it
Inspect the documented state and label or wait for completion.
Prevent it next time
Test both partial and complete responses.

The application cannot retrieve a retained search.

Why it happens
It lost the identifier, the result expired, or the caller lacks access.
How to fix it
Classify the actual response and follow the application recovery policy.
Prevent it next time
Store the identifier securely and define its lifetime.

Retained searches accumulate unnecessarily.

Why it happens
The client has no cleanup lifecycle.
How to fix it
Delete no-longer-needed searches through the documented API.
Prevent it next time
Test cancellation, completion, and expiry paths.

2 practice exercises

1. Does an HTTP success prove the async result is complete?

Show worked solution

No. Inspect the documented running, partial, and shard-result information. The application must decide whether an incomplete result may be shown.

2. What should replace <async-search-id> in the lab requests?

Show worked solution

Use the identifier returned by the authorized lab submission when one is available. Protect it and handle expiration or missing access according to the application contract.

Key takeaways

  • Async search exposes progress and may return partial results.
  • Use and protect the returned identifier through retrieval and cleanup.
  • Bound polling and verify final correctness and resource use.

Official references