白话Elasticsearch32-深入聚合数据分析之案例实战Terms Aggs 统计哪种颜色电视销量最高


在这里插入图片描述


概述

继续跟中华石杉老师学习ES,第32篇

课程地址: https://www.roncoo.com/view/55


Terms Aggregation官方文档

官方文档: 戳这里

详细说明,参考官网即可,下面我们用示例来演示下


案例一 : 统计哪种颜色电视销量最高

模拟数据

创建索引

PUT /tvs
{
  "mappings": {
    "sales": {
      "properties": {
        "price": {
          "type": "long"
        },
        "color": {
          "type": "keyword"
        },
        "brand": {
          "type": "keyword"
        },
        "sold_date": {
          "type": "date"
        }
      }
    }
  }
}

模拟一批数据

POST /tvs/sales/_bulk
{ "index": {}}
{ "price" : 1000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-10-28" }
{ "index": {}}
{ "price" : 2000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-11-05" }
{ "index": {}}
{ "price" : 3000, "color" : "绿色", "brand" : "小米", "sold_date" : "2016-05-18" }
{ "index": {}}
{ "price" : 1500, "color" : "蓝色", "brand" : "TCL", "sold_date" : "2016-07-02" }
{ "index": {}}
{ "price" : 1200, "color" : "绿色", "brand" : "TCL", "sold_date" : "2016-08-19" }
{ "index": {}}
{ "price" : 2000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-11-05" }
{ "index": {}}
{ "price" : 8000, "color" : "红色", "brand" : "三星", "sold_date" : "2017-01-01" }
{ "index": {}}
{ "price" : 2500, "color" : "蓝色", "brand" : "小米", "sold_date" : "2017-02-12" }

原始数据:
在这里插入图片描述


统计哪种颜色的电视销量最高

DSL


GET /tvs/sales/_search
{
  "size": 0,
  "aggs": {
    "popular_colors": {
      "terms": {
        "field": "color"
      }
    }
  }
}

解读:

  • size:只获取聚合结果,而不要执行聚合的原始数据
  • aggs:固定语法,要对一份数据执行分组聚合操作
  • popular_colors:就是对每个aggs,都要起一个名字,自定义,叫啥都行
  • terms:根据字段的值进行分组
  • field:根据指定的字段的值进行分组

类比官方介绍
在这里插入图片描述

返回结果:
在这里插入图片描述

  • hits.hits:我们指定了size是0,所以hits.hits就是空的,否则会把执行聚合的那些原始数据给你返回回来
  • aggregations:聚合结果
  • popular_color:我们指定的某个聚合的名称
  • buckets:根据我们指定的field划分出的buckets
  • key:每个bucket对应的那个值
  • doc_count:这个bucket分组内,有多少个数据

类比官网说明
在这里插入图片描述

每种颜色对应的bucket中的数据的数量,其实就是这种颜色的销量

默认的排序规则:按照doc_count降序排序


size 参数 示例

外层size

当我们 外层不加size的时候,会返回执行聚合的那些原始数据


GET /tvs/sales/_search
{
  "aggs": {
    "popular_color": {
      "terms": {
        "field": "color"
      }
    }
  }
}

返回

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 1,
    "hits": [
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "QzGrtGwBCp8vhw_gCmb9",
        "_score": 1,
        "_source": {
          "price": 2000,
          "color": "红色",
          "brand": "长虹",
          "sold_date": "2016-11-05"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "PzGrtGwBCp8vhw_gCmb9",
        "_score": 1,
        "_source": {
          "price": 2000,
          "color": "红色",
          "brand": "长虹",
          "sold_date": "2016-11-05"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "QDGrtGwBCp8vhw_gCmb9",
        "_score": 1,
        "_source": {
          "price": 3000,
          "color": "绿色",
          "brand": "小米",
          "sold_date": "2016-05-18"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "QjGrtGwBCp8vhw_gCmb9",
        "_score": 1,
        "_source": {
          "price": 1200,
          "color": "绿色",
          "brand": "TCL",
          "sold_date": "2016-08-19"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "RDGrtGwBCp8vhw_gCmb9",
        "_score": 1,
        "_source": {
          "price": 8000,
          "color": "红色",
          "brand": "三星",
          "sold_date": "2017-01-01"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "PjGrtGwBCp8vhw_gCmb9",
        "_score": 1,
        "_source": {
          "price": 1000,
          "color": "红色",
          "brand": "长虹",
          "sold_date": "2016-10-28"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "QTGrtGwBCp8vhw_gCmb9",
        "_score": 1,
        "_source": {
          "price": 1500,
          "color": "蓝色",
          "brand": "TCL",
          "sold_date": "2016-07-02"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "RTGrtGwBCp8vhw_gCmb9",
        "_score": 1,
        "_source": {
          "price": 2500,
          "color": "蓝色",
          "brand": "小米",
          "sold_date": "2017-02-12"
        }
      }
    ]
  },
  "aggregations": {
    "popular_color": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "红色",
          "doc_count": 4
        },
        {
          "key": "绿色",
          "doc_count": 2
        },
        {
          "key": "蓝色",
          "doc_count": 2
        }
      ]
    }
  }
}

当把外层的 size设置为1 ,返回1条执行聚合的那些原始数据

在这里插入图片描述

设置为0 ,不返回执行聚合的那些原始数据

在这里插入图片描述

terms节点下的size

在这里插入图片描述

返回了bucket 中1条数据。

在这里插入图片描述. 不设置时,返回全部的聚合结果 。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值