桶聚合
根据词项、范围或日期等字段将文档分组到“桶”中,从而实现分面搜索和数据分类。
桶聚合 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Grouping Your Data with Buckets
Welcome to Bucket Aggregations! In Elasticsearch, aggregations allow you to analyze your data.
Bucket aggregations specifically group documents into sets, or 'buckets', based on field values. Think of it like the GROUP BY clause in SQL.
- Categorize data: Group products by brand.
- Faceted search: Show counts for different filters.
- Analyze trends: See activity per day or month.
Finding Unique Values with Terms
The terms aggregation is one of the most common bucket aggregations. It finds the top unique values for a specific field and then counts how many documents fall into each unique value's bucket.
It's incredibly useful for seeing the distribution of categorical data, like product categories, user roles, or country names.
Example: Products by Category
Let's use a terms aggregation to find the top 5 product categories and the count of products in each. We use .keyword for exact matches on text fields.
GET /products/_search
{
"size": 0,
"aggs": {
"top_categories": {
"terms": {
"field": "category.keyword",
"size": 5
}
}
}
}Bucketing by Numeric Ranges
The range aggregation allows you to define custom ranges for numeric or date fields. Documents whose field values fall within a defined range are grouped into that bucket.
This is perfect for creating price tiers (e.g., $0-10, $10-50, $50+) or age groups (e.g., 0-18, 19-65, 65+).
Example: Products by Price Range
Here, we define three price ranges: products under $10, between $10 and $50, and over $50. The size: 0 means we only want aggregation results, not actual search hits.
GET /products/_search
{
"size": 0,
"aggs": {
"price_tiers": {
"range": {
"field": "price",
"ranges": [
{ "to": 10.00 },
{ "from": 10.00, "to": 50.00 },
{ "from": 50.00 }
]
}
}
}
}Grouping Data Over Time
When working with time-series data, the date_histogram aggregation is your best friend. It buckets documents into fixed time intervals like minutes, hours, days, or months.
You specify an interval (e.g., 'day', 'month') and Elasticsearch automatically creates buckets for each period, even if no documents exist for a particular period.
Example: Sales Trends by Month
This aggregation groups sales orders by month, allowing you to easily track monthly sales performance. We assume an order_date field of type date.
GET /sales/_search
{
"size": 0,
"aggs": {
"monthly_sales": {
"date_histogram": {
"field": "order_date",
"calendar_interval": "month"
}
}
}
}Multi-Level Grouping: Nesting Buckets
The true power of bucket aggregations comes from nesting them. You can place one bucket aggregation inside another to create hierarchical groupings.
For example, you might want to see product categories, and then within each category, the price ranges of products. This enables deep, multi-dimensional analysis.
Where Buckets Shine
Bucket aggregations are fundamental for many real-world applications:
- Faceted Search: Allowing users to filter search results by category, brand, price range, etc.
- Data Exploration: Discovering patterns and distributions in your data.
- Dashboards: Building visualizations like bar charts (e.g., sales per month, products per category).
- Reporting: Generating summary reports based on grouped data.
Bucket Aggregation Quiz
You are analyzing user feedback and want to count how many reviews were submitted each day over the past week. Which aggregation type is most suitable?
Bucket Aggregations Recap
Great job! You've learned about the power of Bucket Aggregations in Elasticsearch.
- They group documents into logical 'buckets'.
terms: Groups by unique field values.range: Groups by custom numeric or date ranges.date_histogram: Groups by fixed time intervals.- You can nest them for multi-level analysis.
Next, we'll explore Metric Aggregations, which perform calculations (like sum, avg, min, max) on the data within these buckets!
常见问题解答
「桶聚合」课时是免费的吗?
是的 — 「桶聚合」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「桶聚合」这节课中我会学到什么?
根据词项、范围或日期等字段将文档分组到“桶”中,从而实现分面搜索和数据分类。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「桶聚合」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。