elasticsearch 之 聚合分析 aggregate analysis

本文介绍如何使用Elasticsearch对商品数据进行聚合分析,包括按标签分组计数、计算特定名称商品的标签分布、求各标签下商品平均价格、按平均价格排序以及按价格区间分组后再按标签聚合分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

  • 0、构造数据
  • 1、第一个聚合分析的需求:计算每个tag下的商品数量
  • 2、第二个聚合分析的需求:对名称中包含yagao的商品,计算每个tag下的商品数量
  • 3、第三个聚合分析的需求:先分组,再算每组的平均值,计算每个tag下的商品的平均价格
  • 4、第四个聚合分析的需求:计算每个tag下的商品的平均价格,并且按照平均价格降序排序
  • 5、第五个聚合分析的需求:按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格,按照平均价格升序

0、构造数据

PUT /ecommerce/product/1
{
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 30,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
}

1、第一个聚合分析的需求:计算每个tag下的商品数量

GET /ecommerce/product/_search
{
  "size": 0,
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }
}

反馈信息

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_tags": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "fangzhu",
          "doc_count": 3
        },
        {
          "key": "qingxin",
          "doc_count": 3
        }
      ]
    }
  }
}

2、第二个聚合分析的需求:对名称中包含yagao的商品,计算每个tag下的商品数量

GET /ecommerce/product/_search
{
  "size": 0, 
  "query": {
    "match": {
      "name": "yagao"
    }
  },
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }
}

反馈信息:

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_tags": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "fangzhu",
          "doc_count": 3
        },
        {
          "key": "qingxin",
          "doc_count": 3
        }
      ]
    }
  }
}

3、第三个聚合分析的需求:先分组,再算每组的平均值,计算每个tag下的商品的平均价格

GET ecommerce/product/_search
{
  "size": 0, 
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      },
      "aggs": {
        "avg_salary": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

反馈信息

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_tags": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "fangzhu",
          "doc_count": 3,
          "avg_salary": {
            "value": 35
          }
        },
        {
          "key": "qingxin",
          "doc_count": 3,
          "avg_salary": {
            "value": 34.333333333333336
          }
        }
      ]
    }
  }
}

 

4、第四个聚合分析的需求:计算每个tag下的商品的平均价格,并且按照平均价格降序排序

GET ecommerce/product/_search
{
  "size": 0, 
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags",
        "order": {
          "avg_salary": "desc"
        }
      },
      "aggs": {
        "avg_salary": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

5、第五个聚合分析的需求:按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格,按照平均价格升序

GET ecommerce/product/_search
{
  "size": 0, 
  "aggs": {
    "group_by_price": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 20,
            "to": 40
          },
          {
            "from": 40,
            "to": 60
          }
        ]
      },
      "aggs": {
        "group_by_tags": {
          "terms": {
            "field": "tags",
            "order": {
              "avg_price": "asc"
            }
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}

反馈信息

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_price": {
      "buckets": [
        {
          "key": "20.0-40.0",
          "from": 20,
          "to": 40,
          "doc_count": 4,
          "group_by_tags": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "fangzhu",
                "doc_count": 2,
                "avg_price": {
                  "value": 30
                }
              },
              {
                "key": "qingxin",
                "doc_count": 2,
                "avg_price": {
                  "value": 31.5
                }
              }
            ]
          }
        },
        {
          "key": "40.0-60.0",
          "from": 40,
          "to": 60,
          "doc_count": 2,
          "group_by_tags": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "qingxin",
                "doc_count": 1,
                "avg_price": {
                  "value": 40
                }
              },
              {
                "key": "fangzhu",
                "doc_count": 1,
                "avg_price": {
                  "value": 45
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Elasticsearch 中,聚合操作是一种非常强大的数据分析工具,可以从数据中提取有意义的信息,帮助我们更好地了解数据。下面是一个简单的例子,演示如何使用聚合操作并显示其他字段。 假设你有一个存储了用户行为数据的索引,其中包含了用户的 ID、行为类型、行为时间等字段。现在,你想要对这些数据进行聚合分析,以了解不同用户的行为模式和时间分布情况,并在结果中显示用户的 ID。 以下是一个示例查询: ``` GET user_behavior/_search { "size": 0, "aggs": { "user_count": { "cardinality": { "field": "user_id" } }, "behavior_type": { "terms": { "field": "behavior_type" }, "aggs": { "time_distribution": { "date_histogram": { "field": "behavior_time", "interval": "day" } } } } } } ``` 这个查询使用了两个聚合操作: 1. `cardinality` 聚合操作统计了不同用户的数量,即用户总数。 2. `terms` 聚合操作按照行为类型进行分组,并在每个分组中使用 `date_histogram` 对行为时间进行分组,并且设置了按天进行时间间隔分组。 这个查询将返回一个包含聚合结果的响应体,其中包括了用户总数和每个行为类型的时间分布情况。此外,还可以看到每个行为类型的结果中包含了用户的 ID 字段。 需要注意的是,如果想要在聚合结果中显示其他字段,需要在聚合操作中添加该字段。例如,在上面的查询中,如果你还想要显示用户的姓名字段,需要将其添加到 `terms` 聚合操作中。 希望这个例子能够帮助你更好地理解 Elasticsearch 中的聚合操作,并且能够借此了解如何在聚合结果中显示其他字段。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值