文章目录


白话Elasticsearch39-深入聚合数据分析之案例实战_搜索+聚合: 统计指定品牌下每个颜色的销量_Elasticsearch教程


概述

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

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


案例

需求: 统计指定品牌下每个颜色的销量

原始数据:
白话Elasticsearch39-深入聚合数据分析之案例实战_搜索+聚合: 统计指定品牌下每个颜色的销量_数据库_02


示例

类比下SQL:

select count(*) from tvs.sales where brand = '小米' group by price

es aggregation的范围 scope ,任何的聚合,都必须在搜索出来的结果数据中之行,搜索结果,就是聚合分析操作的scope .

统计“小米”品牌下每个颜色的销量


GET /tvs/sales/_search
{
  "query": {
    "term": {
      "brand": "小米"
    }
  },
  "aggs": {
    "group_by_color": {
      "terms": {
        "field": "color"
      }
    }
  },
  "size": 0
}

返回

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_color": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "绿色",
          "doc_count": 1
        },
        {
          "key": "蓝色",
          "doc_count": 1
        }
      ]
    }
  }
}