Async search for expensive queries in Elasticsearch - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When running expensive searches in Elasticsearch, it is important to understand how the time to get results grows as the data or query size increases.
We want to know how the cost of running an asynchronous search changes with bigger or more complex queries.
Analyze the time complexity of the following async search request.
POST /_async_search
{
"query": {
"bool": {
"must": [
{ "match": { "field1": "value1" } },
{ "range": { "field2": { "gte": 10, "lte": 100 } } }
]
}
}
}
This code starts an asynchronous search that runs a complex query combining text match and numeric range filters.
Look for parts that repeat work as input grows.
- Primary operation: Elasticsearch scans matching documents to check query conditions.
- How many times: It processes each document that matches filters, which depends on data size and query selectivity.
As the number of documents or query complexity grows, the search takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 document checks |
| 100 | About 100 document checks |
| 1000 | About 1000 document checks |
Pattern observation: The work grows roughly in direct proportion to the number of documents checked.
Time Complexity: O(n)
This means the time to complete the async search grows linearly with the number of documents it needs to examine.
[X] Wrong: "Async search makes the query run instantly regardless of data size."
[OK] Correct: Async search runs the query in the background but still processes documents one by one, so bigger data means longer time.
Understanding how async search scales helps you explain how to handle large data queries efficiently in real projects.
"What if we added more filters to the query? How would the time complexity change?"
Practice
async search in Elasticsearch for expensive queries?Solution
Step 1: Understand async search purpose
Async search lets you run slow or heavy queries without making your app wait or freeze.Step 2: Identify the main benefit
This means your app can continue working while the query runs in the background.Final Answer:
It allows running slow queries without blocking the application. -> Option DQuick Check:
Async search = non-blocking query execution [OK]
- Thinking async search speeds up queries automatically
- Assuming async search caches results permanently
- Believing async search disables logging
Solution
Step 1: Recall async search API endpoint
The correct endpoint to start an async search is POST /_async_search with the query in the body.Step 2: Check HTTP method and path
GET is not used to start async search, and /_search/async or PUT are incorrect paths or methods.Final Answer:
POST /_async_search with query body -> Option AQuick Check:
Start async search = POST /_async_search [OK]
- Using GET instead of POST to start async search
- Using wrong endpoint like /_search/async
- Using PUT method which is invalid here
id field represent?
{
"id": "r1A2B3C4D5E6F7G8H9I",
"is_running": true,
"response": null
}Solution
Step 1: Understand the async search response fields
Theidis a unique string to identify this async search request.Step 2: Purpose of the
You use thisididto check if the search is done or to get the results later.Final Answer:
The unique identifier to check status or fetch results later. -> Option CQuick Check:
Async search id = unique query handle [OK]
id tracks query status and results [OK]- Confusing
idwith document count - Thinking
idis timeout or node info - Assuming
idchanges during query
POST /_async_search
{
"query": {
"match": {
"title": "Elasticsearch"
}
},
"wait_for_completion_timeout": "1s"
}
What is the error in this request?Solution
Step 1: Check JSON syntax
The JSON body is missing a comma after the closing brace of the "query" object.Step 2: Validate method and fields
POST is correct method, wait_for_completion_timeout is valid in body, and "title" is a valid field name.Final Answer:
Missing comma between query and wait_for_completion_timeout fields. -> Option AQuick Check:
JSON syntax error = missing comma [OK]
- Forgetting commas between JSON objects
- Confusing HTTP methods for async search
- Misplacing wait_for_completion_timeout outside body
Solution
Step 1: Understand async search timeout and polling
Setting a reasonablewait_for_completion_timeoutlets the server try to finish quickly but returns control if it takes longer.Step 2: Use the returned
You can check the status later using theidto poll for completioniduntil the results are ready, avoiding timeouts and blocking.Final Answer:
Start async search with a long wait_for_completion_timeout and poll using the returned id until results are ready. -> Option BQuick Check:
Async search + polling = efficient for expensive queries [OK]
- Using normal search with high timeout risking app freeze
- Requesting results immediately before completion
- Manually merging partial results instead of async search
