histogram:类似于terms,也是进行bucket分组操作,接收一个field,按照这个field的值的各个范围区间,进行bucket分组操作
"histogram":{ "field": "price","interval": 2000
}
interval:2000,划分范围,02000,20004000,40006000,60008000,8000~10000,
buckets去根据price的值,比如2500落在哪个区间内,落到20004000,此时就会将这条数据放入20004000对应的那个bucket中
bucket划分的方法,terms,将field值相同的数据划分到一个bucket中
histogram,将field处于相同区间的数据划分到一个bucket中
bucket有了之后,一样的,去对每个bucket执行avg,count,sum,max,min,等各种metric操作,聚合分析
GET /tvs/sales/_search
{"size" : 0,"aggs":{"price":{"histogram":{ "field": "price","interval": 2000},"aggs":{"revenue": {"sum": { "field" : "price"}}}}}
}
响应结果
{"took": 14,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 8,"max_score": 0,"hits": []},"aggregations": {"price": {"buckets": [{"key": 0,"doc_count": 3,"revenue": {"value": 3700}},{"key": 2000,"doc_count": 4,"revenue": {"value": 9500}},{"key": 4000,"doc_count": 0,"revenue": {"value": 0}},{"key": 6000,"doc_count": 0,"revenue": {"value": 0}},{"key": 8000,"doc_count": 1,"revenue": {"value": 8000}}]}}
}