📖 Scenario: You have a collection of products in an online store. Each product has a category and a price. You want to find out how many products are in the electronics category and cost less than 500.
🎯 Goal: Create an Elasticsearch query using filter aggregation to count products that belong to the electronics category and have a price less than 500.
📋 What You'll Learn
Create a filter aggregation named electronics_under_500
Use a bool query with must conditions for category equals electronics and price less than 500
Include a match_all query at the top level
Print the full JSON query as the final output
💡 Why This Matters
🌍 Real World
Filter aggregations help online stores analyze product counts based on multiple conditions, like category and price, to make business decisions.
💼 Career
Understanding filter aggregations is important for roles involving Elasticsearch, data analysis, and search engine optimization.
Progress0 / 4 steps
1
Create the base query
Create a JSON object called query with a match_all query inside it.
Elasticsearch
Hint
The match_all query returns all documents.
2
Add filter aggregation
Add an aggs field to the JSON with a filter aggregation named electronics_under_500.
Elasticsearch
Hint
The filter aggregation lets you count documents matching a filter.
3
Define the filter conditions
Inside the filter aggregation, add a bool query with must conditions: one for termcategory equals electronics and one for rangeprice less than 500.
Elasticsearch
Hint
Use bool with must array to combine filters.
4
Print the final query
Print the full JSON query object exactly as defined in the previous steps.
Elasticsearch
Hint
Use print() to display the JSON object.
Practice
(1/5)
1. What is the main purpose of a filter aggregation in Elasticsearch?
easy
A. To delete documents matching a query
B. To sort documents by a field value
C. To update documents in the index
D. To select documents that match a specific condition
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 D
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?