0Pricing
Elasticsearch & Full Text Search Systems · 课时

管道聚合

将多个聚合连接起来,根据其他聚合的结果执行计算,构建复杂的分析管道。

管道聚合 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Intro to Pipeline Aggregations

Welcome to Pipeline Aggregations! So far, we've learned about metric and bucket aggregations.

Pipeline aggregations take things a step further. They operate on the results of other aggregations, allowing you to chain them together for more sophisticated analytics.

Beyond Basic Aggregations

Think of basic aggregations as giving you statistics within each bucket (e.g., total sales per product category).

Pipeline aggregations, on the other hand, allow you to calculate statistics across those buckets or over time. For example, finding the overall total of 'total sales per category'.

Chaining Aggregations

The core idea is chaining. A pipeline aggregation receives its input from another aggregation's output.

  • It processes the results of 'sibling' or 'parent' aggregations.
  • This enables multi-stage analysis, where one calculation feeds into the next.

sum_bucket Aggregation

One common pipeline aggregation is sum_bucket. It calculates the sum of a specified metric across all sibling buckets.

Use case: You've grouped sales by product category and calculated the total sales for each category. Now you want to find the grand total sales across all categories.

sum_bucket in Action

This example first gets total sales per category, then uses sum_bucket to sum those category totals into an overall total. You can send this JSON to Elasticsearch using curl -XGET 'localhost:9200/your_index/_search?pretty' -H 'Content-Type: application/json' -d'...'

{ "size": 0,
  "aggs": {
    "sales_by_category": {
      "terms": { "field": "product_category.keyword" },
      "aggs": {
        "total_sales_per_category": { "sum": { "field": "sales_amount" } }
      }
    },
    "overall_total_sales": {
      "sum_bucket": {
        "buckets_path": "sales_by_category>total_sales_per_category"
      }
    }
  }
}

avg_bucket Aggregation

Similar to sum_bucket, the avg_bucket aggregation calculates the average of a specific metric across all sibling buckets.

Use case: After getting the total sales for each product category, you might want to know the average total sales amount *per category*.

avg_bucket in Action

Here, we use avg_bucket to calculate the average of the 'total sales per category' values. Notice the buckets_path remains the same, pointing to the aggregation whose results we want to average.

{ "size": 0,
  "aggs": {
    "sales_by_category": {
      "terms": { "field": "product_category.keyword" },
      "aggs": {
        "total_sales_per_category": { "sum": { "field": "sales_amount" } }
      }
    },
    "average_sales_per_category": {
      "avg_bucket": {
        "buckets_path": "sales_by_category>total_sales_per_category"
      }
    }
  }
}

min_bucket & max_bucket

Elasticsearch also provides min_bucket and max_bucket pipeline aggregations.

  • min_bucket: Finds the minimum value among the specified metric from all sibling buckets.
  • max_bucket: Finds the maximum value among the specified metric from all sibling buckets.

These work just like sum_bucket and avg_bucket, simply changing the operation.

Understanding buckets_path

The buckets_path parameter is crucial for pipeline aggregations. It tells Elasticsearch which aggregation's output to use as input.

  • It uses a > separator for nested aggregations (e.g., parent_agg>child_agg).
  • You can also reference the document count of a bucket using _count (e.g., sales_by_category>_count).

Pipeline Aggregation Check

Let's check your understanding of pipeline aggregations!

Pipeline Aggregations Recap

Great job! In this lesson, you learned about:

  • What pipeline aggregations are and why they're powerful.
  • How they allow you to perform calculations across the results of other aggregations.
  • Key types like sum_bucket, avg_bucket, min_bucket, and max_bucket.
  • The importance of the buckets_path parameter for referencing aggregation outputs.

Pipeline aggregations enable a deeper level of data analysis in Elasticsearch!

常见问题解答

「管道聚合」课时是免费的吗?

是的 — 「管道聚合」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「管道聚合」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?

能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 指标聚合
  2. 桶聚合
  3. 管道聚合
  4. 嵌套聚合与子聚合
← 返回 Elasticsearch & Full Text Search Systems