世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问:avg_bucket有什么特点?
答:
问:avg_bucket如何使用?
答:
DELETE /avg_bucket_test
PUT /avg_bucket_test
{
"mappings": {
"properties": {
"type": {"type": "integer"},
"num": {"type": "integer"}
}
}
}
POST /avg_bucket_test/_bulk
{"index": {"_id": 1}}
{"type": 1, "num": 3}
{"index": {"_id": 2}}
{"type": 2, "num": 7}
{"index": {"_id": 3}}
{"type": 1, "num": 5}
{"index": {"_id": 4}}
{"type": 2, "num": 9}
{"index": {"_id": 5}}
{"type": 1, "num": 6}
GET /avg_bucket_test/_search
{
"size": 0,
"aggs": {
"avg_num_aggs": {
"terms": {
"field": "type",
"size": 2
},
"aggs": {
"sum_num_aggs": {
"sum": {
"field": "num"
}
}
}
},
"avg_num_bucket_aggs": {
"avg_bucket": {
"buckets_path": "avg_num_aggs>sum_num_aggs"
}
}
}
}
# 结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"avg_num_aggs" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1,
"doc_count" : 3,
"sum_num_aggs" : {
"value" : 14.0
}
},
{
"key" : 2,
"doc_count" : 2,
"sum_num_aggs" : {
"value" : 16.0
}
}
]
},
"avg_num_bucket_aggs" : {
"value" : 15.0
}
}
}



