ElasticSearch玩转大数据elasticsearch

四十、Elasticsearch聚合分析--搜索+聚合

2017-07-19  本文已影响68人  编程界的小学生

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

2、实现方式

SQL:

select count(*) from tvs.sales where brand like "%长%" group by price
GET /tvs/sales/_search
{
  "size": 0,
  "query": {
    "match": {
      "brand": "小米"
    }
  },
  "aggs": {
    "group_by_color": {
      "terms": {
        "field": "color"
      }
    }
  }
}

结果:

{
  "took": 107,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "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
        }
      ]
    }
  }
}

先进行搜索,对搜索出来的结果再次进行聚合

若有兴趣,欢迎来加入群,【Java初学者学习交流群】:458430385,此群有Java开发人员、UI设计人员和前端工程师。有问必答,共同探讨学习,一起进步!
欢迎关注我的微信公众号【Java码农社区】,会定时推送各种干货:


qrcode_for_gh_577b64e73701_258.jpg
上一篇下一篇

猜你喜欢

热点阅读