0
0
Elasticsearchquery~30 mins

Discover for data exploration in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Discover for data exploration
📖 Scenario: You are working with a collection of sales data stored in Elasticsearch. You want to explore this data to find useful insights like total sales per product category.
🎯 Goal: Build a simple Elasticsearch query to discover and aggregate sales data by product category.
📋 What You'll Learn
Create an Elasticsearch index with sample sales data
Define a filter to select sales from the year 2023
Write an aggregation query to sum sales amounts by product category
Print the aggregation results
💡 Why This Matters
🌍 Real World
Exploring sales data helps businesses understand which product categories perform best and make informed decisions.
💼 Career
Data analysts and developers use Elasticsearch queries to explore and summarize large datasets quickly.
Progress0 / 4 steps
1
Create sample sales data index
Create an Elasticsearch index called sales with documents containing fields product_category (string), sale_amount (number), and sale_year (number). Add exactly these three documents: {"product_category": "Books", "sale_amount": 100, "sale_year": 2023}, {"product_category": "Electronics", "sale_amount": 250, "sale_year": 2023}, {"product_category": "Books", "sale_amount": 150, "sale_year": 2022}.
Elasticsearch
Need a hint?

Use the PUT method to add documents to the sales index with the exact fields and values.

2
Define filter for sales in 2023
Create a variable called filter_2023 that contains a filter to select documents where sale_year is exactly 2023.
Elasticsearch
Need a hint?

Use a term filter to match sale_year exactly 2023.

3
Write aggregation query for total sales by category
Create a variable called agg_query that contains an Elasticsearch query to filter by filter_2023 and aggregate total sale_amount by product_category using a terms aggregation and a sum sub-aggregation.
Elasticsearch
Need a hint?

Use a bool query with filter and a terms aggregation on product_category with a sum sub-aggregation on sale_amount.

4
Print aggregation results
Print the agg_query variable to display the full Elasticsearch query.
Elasticsearch
Need a hint?

Use print(agg_query) to display the query.