实际上来说,我们之前学习的搜索相关的知识,完全可以和聚合组合起来使用

    1. select count(*)
    2. from tvs.sales
    3. where brand like "%长%"
    4. group by color

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

    GET /tvs/_search 
    {
      "size": 0,
      "query": {
        "term": {
          "brand": {
            "value": "小米"
          }
        }
      },
      "aggs": {
        "group_by_color": {
          "terms": {
            "field": "color"
          }
        }
      }
    }
    
    {
      "took": 5,
      "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
            }
          ]
        }
      }
    }