美文网首页java从0到架构师
45_深入聚合数据分析_排序:按每种颜色的平均销售额降序排序

45_深入聚合数据分析_排序:按每种颜色的平均销售额降序排序

作者: 小山居 | 来源:发表于2020-02-28 19:08 被阅读0次

    45_深入聚合数据分析_排序:按每种颜色的平均销售额降序排序

    之前的话,排序,是按照每个bucket的doc_count降序来排的

    但是假如说,我们现在统计出来每个颜色的电视的销售额,需要按照销售额降序排序????

    GET /tvs/sales/_search 
    {
      "size": 0,
      "aggs": {
        "group_by_color": {
          "terms": {
            "field": "color"
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
    

    分析结果:

    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 8,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "group_by_color": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "红色",
              "doc_count": 4,
              "avg_price": {
                "value": 3250
              }
            },
            {
              "key": "绿色",
              "doc_count": 2,
              "avg_price": {
                "value": 2100
              }
            },
            {
              "key": "蓝色",
              "doc_count": 2,
              "avg_price": {
                "value": 2000
              }
            }
          ]
        }
      }
    }
              }
    

    按照每种颜色的平均销售额---降序排列

    GET /tvs/sales/_search 
    {
      "size": 0,
      "aggs": {
        "group_by_color": {
          "terms": {
            "field": "color",
            "order": {
              "avg_price": "asc"
            }
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
    

    相关文章

      网友评论

        本文标题:45_深入聚合数据分析_排序:按每种颜色的平均销售额降序排序

        本文链接:https://www.haomeiwen.com/subject/jkkrhhtx.html