A filter aggregation lets you count or analyze documents that match a specific filter condition, like counting only documents with a certain field value.
Click to reveal answer
beginner
How do you define a filter aggregation in an Elasticsearch query?
You define it by specifying a filter with a query inside the aggregation, like "filter": { "term": { "status": "active" } }.
Click to reveal answer
intermediate
Can filter aggregation be combined with other aggregations?
Yes! You can nest other aggregations inside a filter aggregation to analyze only the filtered documents further.
Click to reveal answer
intermediate
What is the difference between filter aggregation and filters aggregation?
Filter aggregation uses one filter to select documents. Filters aggregation lets you define multiple filters and get results for each filter separately.
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.
A. The filter aggregation must use "term" not "terms" for single value
B. The "terms" query requires an array of values, not a single string
C. The aggregation name "filtered_data" is invalid
D. The filter aggregation cannot be nested inside "aggs"
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 B
Quick Check:
"terms" query needs array, not string [OK]
Hint: "terms" query needs array of values, not single string [OK]
Common Mistakes:
Using string instead of array for "terms" query
Thinking aggregation names have restrictions
Believing filter aggregation can't be nested
5. You want to find the average price of products only in the category "electronics" using filter aggregation. Which of the following query snippets correctly combines filter aggregation with an average sub-aggregation?