Filter aggregation in Elasticsearch - Time & Space Complexity
When using filter aggregation in Elasticsearch, we want to know how the time to get results changes as the data grows.
We ask: How does the number of documents affect the work Elasticsearch does to apply the filter?
Analyze the time complexity of the following code snippet.
{
"aggs": {
"filtered_data": {
"filter": {
"term": { "status": "active" }
}
}
}
}
This snippet filters documents where the field status equals active and aggregates the count.
Look for repeated work Elasticsearch does when applying the filter.
- Primary operation: Checking each document's
statusfield against the filter value. - How many times: Once for every document in the index.
As the number of documents grows, Elasticsearch checks more documents to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of documents.
Time Complexity: O(n)
This means the time to apply the filter grows in a straight line as the number of documents increases.
[X] Wrong: "Filter aggregation runs instantly no matter how many documents there are."
[OK] Correct: Elasticsearch must check each document to see if it matches the filter, so more documents mean more work.
Understanding how filters scale helps you explain how search engines handle large data efficiently and why indexing matters.
What if we added a second filter inside a bool must clause? How would the time complexity change?
Practice
filter aggregation in Elasticsearch?Solution
Step 1: Understand filter aggregation role
A filter aggregation is used to select documents that meet a certain condition or criteria.Step 2: Compare with other options
Sorting, updating, or deleting documents are not functions of filter aggregation.Final Answer:
To select documents that match a specific condition -> Option DQuick Check:
Filter aggregation = select matching documents [OK]
- Confusing filter aggregation with sorting
- Thinking filter aggregation modifies documents
- Mixing filter aggregation with delete operations
Solution
Step 1: Identify filter aggregation syntax
The filter aggregation uses the key "filter" with a query inside, like "term" for exact match.Step 2: Check each option
"aggs": { "my_filter": { "filter": { "term": { "status": "active" } } } } correctly uses "filter" with a "term" query. Options A, C, and D use other aggregation types or query types incorrectly.Final Answer:
"aggs": { "my_filter": { "filter": { "term": { "status": "active" } } } } -> Option AQuick Check:
Filter aggregation syntax = option with "filter" key [OK]
- Using "terms" instead of "filter" for filter aggregation
- Confusing query types inside aggregation
- Missing the "filter" key in aggregation
active_users?
{
"aggs": {
"active_users": {
"filter": { "term": { "status": "active" } }
}
}
}
Assuming the index has 1000 documents, 300 of which have status set to active.Solution
Step 1: Understand filter aggregation result
The filter aggregation counts documents matching the filter condition, here "status" = "active".Step 2: Apply given data
Out of 1000 documents, 300 have status "active", so the aggregation count is 300.Final Answer:
300 -> Option AQuick Check:
Filter count = matching docs = 300 [OK]
- Choosing total documents instead of filtered count
- Confusing filter aggregation with terms aggregation count
- Assuming zero if filter is not exact
{
"aggs": {
"filtered_data": {
"filter": {
"terms": {
"status": "active"
}
}
}
}
}Solution
Step 1: Check the "terms" query usage
The "terms" query expects an array of values, e.g., ["active"], not a single string.Step 2: Validate other parts
The aggregation name is valid, and filter aggregation can be inside "aggs". Using "term" is an alternative but not an error here.Final Answer:
The "terms" query requires an array of values, not a single string -> Option BQuick Check:
"terms" query needs array, not string [OK]
- Using string instead of array for "terms" query
- Thinking aggregation names have restrictions
- Believing filter aggregation can't be nested
Solution
Step 1: Understand filter aggregation with sub-aggregation
Filter aggregation uses "filter" key and can contain sub-aggregations inside "aggs".Step 2: Check each option
{ "aggs": { "electronics_filter": { "filter": { "term": { "category": "electronics" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } correctly nests an average aggregation inside a filter aggregation for category "electronics". { "aggs": { "avg_price": { "avg": { "field": "price" } }, "filter": { "term": { "category": "electronics" } } } } misplaces filter outside aggregation. { "query": { "term": { "category": "electronics" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } uses query filtering, not aggregation filtering. { "aggs": { "electronics_filter": { "terms": { "field": "category" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } uses "terms" aggregation, not filter.Final Answer:
{ "aggs": { "electronics_filter": { "filter": { "term": { "category": "electronics" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } -> Option CQuick Check:
Filter aggregation with sub-aggregation = { "aggs": { "electronics_filter": { "filter": { "term": { "category": "electronics" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } [OK]
- Placing filter outside aggregation block
- Using query filter instead of filter aggregation
- Confusing terms aggregation with filter aggregation
Ready to prove it?
Pass the test and this pattern moves unknown
