Elasticsearch & Full Text Search Systems · 课时

指标聚合

使用各种指标聚合类型,对数据执行求和、平均值、最小值、最大值和计数等计算。

第 1 / 4 课11 个步骤

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

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

Discover Data Insights

What if you could ask "how much total sales did we have last month?" or "what's the average price of our products?" Aggregations in Elasticsearch let you do exactly that!

They provide powerful analytical capabilities beyond simple search. Think of them as SQL's GROUP BY and aggregate functions, but for your search data.

Unpacking Metric Aggregations

Metric aggregations are the simplest type of aggregation. They calculate a single metric (like a sum, average, min, or max) over a set of documents.

They give you numerical summaries of your data, helping you quickly understand key statistics and trends without retrieving all individual documents.

Indexing Sample Sales Data

To demonstrate metric aggregations, let's index some sample sales data. We'll use documents with a price field.

Run this curl command to add our first product:

curl -X PUT "localhost:9200/sales_data/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "product": "Laptop",
  "price": 1200,
  "quantity": 1
}'

Adding More Sample Data

Great! To have more data for our aggregations, let's add two more products. Imagine you've run similar curl commands for these:

  • sales_data/_doc/2: {"product": "Mouse", "price": 25, "quantity": 2}
  • sales_data/_doc/3: {"product": "Keyboard", "price": 75, "quantity": 1}

Now we have a small dataset to work with!

Calculating the Average (`avg`)

The avg aggregation calculates the arithmetic mean of a numeric field. It's perfect for finding the average price of products, average age, or average score.

Let's find the average price of our indexed products:

curl -X GET "localhost:9200/sales_data/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "average_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}'

Summing Up Values (`sum`)

The sum aggregation calculates the total sum of a numeric field's values. This is ideal for finding total sales, total quantity, or total revenue.

Let's calculate the total price of all indexed products:

curl -X GET "localhost:9200/sales_data/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "total_price": {
      "sum": {
        "field": "price"
      }
    }
  }
}'

Finding Min and Max (`min`, `max`)

The min and max aggregations return the smallest and largest values of a numeric field, respectively. They help identify outliers or boundary values in your data.

Let's find the cheapest and most expensive product prices:

curl -X GET "localhost:9200/sales_data/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "min_price": { "min": { "field": "price" } },
    "max_price": { "max": { "field": "price" } }
  }
}'

Counting Values (`value_count`)

The value_count aggregation counts the number of documents that have a value for a specific field. It's different from a document count because it only considers documents where the field exists and is not null.

Let's count how many products have a 'price' field:

curl -X GET "localhost:9200/sales_data/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "price_count": {
      "value_count": {
        "field": "price"
      }
    }
  }
}'

All Stats in One Go (`stats`)

The stats aggregation is a convenience aggregation that computes min, max, sum, avg, and count (of values) all at once for a numeric field.

It saves you from writing five separate aggregations and is great for a quick overview!

curl -X GET "localhost:9200/sales_data/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "price_stats": {
      "stats": {
        "field": "price"
      }
    }
  }
}'

Metric Aggregation Check

Consider a scenario where you have a list of product prices: 100, 50, 200, 50, null.

What would be the result of a value_count aggregation on the 'price' field?

Metric Aggregations Recap

In this lesson, you've learned about Elasticsearch metric aggregations, powerful tools for summarizing your data:

  • avg: Calculates the arithmetic mean.
  • sum: Computes the total sum.
  • min & max: Finds the smallest and largest values.
  • value_count: Counts non-null field values.
  • stats: A convenient way to get all basic stats at once.

These aggregations are fundamental for understanding the numerical characteristics of your data and are often combined for more complex analysis.

免费开始

用 AI 导师学习 Elasticsearch & Full Text Search Systems — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「指标聚合」课时是免费的吗?

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

「指标聚合」课时需要多长时间?

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

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

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

此课程中的所有课时

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