一、桶聚合
### --- Bucket Aggregations,桶聚合。
~~~ 它执行的是对文档分组的操作(与sql中的group by类似),
~~~ 把满足相关特性的文档分到一个桶里,即桶分,
~~~ 输出结果往往是一个个包含多个文档的桶(一个桶就是一个group)
~~~ bucket:一个数据分组
~~~ metric:对一个数据分组执行的统计
### --- 桶聚合
POST /book/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 200
},
{
"from": 200,
"to": 400
},
{
"from": 400,
"to": 1000
}
]
},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
### --- 值的个数统计
"count_price": {
"value_count": {
"field": "price"
}
}
### --- 实现having 效果
POST /book/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 200
},
{
"from": 200,
"to": 400
},
{
"from": 400,
"to": 1000
}
]
},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
},
"having": {
"bucket_selector": {
"buckets_path": {
"avg_price": "average_price"
},
"script": {
"source": "params.avg_price >= 200 "
}
}
}
}
}
}
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor