elasticsearch聚合之后获取桶的平均值avg_bucket或者stats_bucket

database

例子

GET /collect_data_store_goods_fact*/_search

{

"query": {

"match_all": {}

},

"size": 0,

"aggs": {

"brand":{

"terms": {

"field": "orgId",

"size": 2,

"order": {

"or_totalSaleGoodsAmountSum": "desc"

}

},

"aggs": {

"or_totalSaleGoodsAmountSum": {

"sum": {

"field": "store_goods_totalSaleGoodsAmount"

}

}

}

},

"stats_monthly_sales": {

"stats_bucket": {

"buckets_path": "brand>or_totalSaleGoodsAmountSum"

}

},

"avg_stately_age" : {

"avg_bucket": {

"buckets_path": "brand>or_totalSaleGoodsAmountSum"

}

},

"store_goods_totalSaleGoodsAmountALL": {

"sum": {

"field": "store_goods_totalSaleGoodsAmount"

}

}

}

}

执行结果

{

"took" : 6,

"timed_out" : false,

"_shards" : {

"total" : 10,

"successful" : 10,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 30,

"relation" : "eq"

},

"max_score" : null,

"hits" : [ ]

},

"aggregations" : {

"store_goods_totalSaleGoodsAmountALL" : {

"value" : 1.6650087E7

},

"brand" : {

"doc_count_error_upper_bound" : 0,

"sum_other_doc_count" : 12,

"buckets" : [

{

"key" : 13274,

"doc_count" : 17,

"or_totalSaleGoodsAmountSum" : {

"value" : 1.6112652E7

}

},

{

"key" : 13278,

"doc_count" : 1,

"or_totalSaleGoodsAmountSum" : {

"value" : 299700.0

}

}

]

},

"stats_monthly_sales" : {

"count" : 2,

"min" : 299700.0,

"max" : 1.6112652E7,

"avg" : 8206176.0,

"sum" : 1.6412352E7

},

"avg_stately_age" : {

"value" : 8206176.0

}

}

}

因为 bucket 似乎默认是 返回10个 桶的,除非指定 size 将所有桶都 返回出来否则计算 桶的平均值 是 用 显示出来的 桶的数据/size 的。

也就是 说 avg_bucket 并不能 获取到 分组之后 所有桶的  平均值了。

 

 

 

 

以上是 elasticsearch聚合之后获取桶的平均值avg_bucket或者stats_bucket 的全部内容, 来源链接: utcz.com/z/531379.html

回到顶部