如何对聚合得到的结果再次统计

Elasticsearch | 作者 Sunsy | 发布于2018年07月13日 | 阅读数:716

GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "terms" : {
              "field" : "color",
              "order": {
                "avg_price" : "desc"
              }
            },
            "aggs": {
                "avg_price": {
                    "avg": {"field": "price"}
                }
            }
        }
     }
}
这是官网的一个例子,按照颜色分组,统计不同颜色汽车销售额的平均值
结果如下:
  "aggregations": {
    "colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4,
          "avg_price": {
            "value": 32500
          }
        },
        {
          "key": "green",
          "doc_count": 2,
          "avg_price": {
            "value": 21000
          }
        },
        {
          "key": "blue",
          "doc_count": 2,
          "avg_price": {
            "value": 20000
          }
        }
      ]
    }
  }
我现在想在这个基础上对每种颜色汽车销售额均值再做均值,最大值,最小值统计,请问有何好的办法
已邀请:

rochy - rochy_he@tw

赞同来自: Sunsy

可以参考:Pipeline aggregations
PS:以下代码未经测试
POST /cars/transactions/_search
{
"size" : 0,
"aggs" : {
"colors" : {
"terms" : {
"field" : "color",
"order": {
"avg_price" : "desc"
}
},
"aggs": {
"avg_price": {
"avg": {"field": "price"}
}
}
},
"avg_price_avg": {
"avg_bucket": {
"buckets_path": "colors>avg_price"
}
},
"avg_price_max": {
"max_bucket": {
"buckets_path": "colors>avg_price"
}
},
"avg_price_min": {
"min_bucket": {
"buckets_path": "colors>avg_price"
}
}
}
}

要回复问题请先登录注册