0
0
Elasticsearchquery~10 mins

Async search for expensive queries in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Async search for expensive queries
Start Async Search Request
Elasticsearch Accepts Request
Query Runs in Background
Client Polls for Results
Results Ready?
NoWait and Poll Again
Yes
Client Retrieves Results
Process or Display Results
End
The client sends an async search request, Elasticsearch runs the query in the background, the client polls until results are ready, then retrieves and processes them.
Execution Sample
Elasticsearch
POST /_async_search
{
  "query": { "match_all": {} },
  "size": 1000
}
Starts an async search that matches all documents and returns up to 1000 results.
Execution Table
StepActionRequest/ResponseStatusNotes
1Send async search requestPOST /_async_search with queryAcceptedElasticsearch starts query in background
2Receive async search ID{ "id": "abc123", "is_running": true }RunningClient gets search ID to poll later
3Poll for resultsGET /_async_search/abc123RunningQuery still running, no results yet
4Poll again after waitGET /_async_search/abc123CompletedResults ready, returned in response
5Process resultsResponse contains hitsSuccessClient processes or displays results
6Delete async searchDELETE /_async_search/abc123DeletedClean up resources on server
💡 Execution stops after results are retrieved and optionally deleted.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
search_idnull"abc123""abc123""abc123"null
is_runningnulltruetruefalsefalse
resultsnullnullnullhits datahits data
Key Moments - 3 Insights
Why do we get a search ID instead of immediate results?
Because the query is expensive, Elasticsearch runs it in the background and returns a search ID to let the client check back later, as shown in execution_table step 2.
What happens if we poll too early for results?
The response will indicate the search is still running (step 3), so the client must wait and poll again later.
Why should we delete the async search after retrieving results?
Deleting frees server resources used to keep the search context, as shown in step 6 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the status after the first async search request is sent?
ACompleted
BAccepted
CDeleted
DFailed
💡 Hint
Check the Status column at Step 1 in the execution_table.
At which step does the client receive the actual search results?
AStep 4
BStep 3
CStep 2
DStep 6
💡 Hint
Look for 'Results ready' in the Notes column of the execution_table.
If the client never deletes the async search, what variable in variable_tracker remains non-null?
Aresults
Bis_running
Csearch_id
Dnull
💡 Hint
Check the 'search_id' row in variable_tracker and what happens after Step 6.
Concept Snapshot
Async search lets Elasticsearch run expensive queries in background.
Client sends request and gets a search ID.
Client polls with ID until results are ready.
Results returned when complete.
Delete async search to free resources.
Full Transcript
Async search in Elasticsearch helps run heavy queries without waiting for immediate results. The client sends a request and gets back a search ID. Elasticsearch runs the query in the background. The client polls using the ID to check if results are ready. Once ready, the client retrieves and processes the results. Finally, the client can delete the async search to clean up resources. This process avoids long waits and keeps the system responsive.