【ES】学习8-聚合1
2024-10-13 23:04:05
参考资料:
https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_combining_the_two.html
特定概念:
桶(Buckets)满足特定条件的文档的集合
指标(Metrics)对桶内的文档进行统计计算
SELECT COUNT(color)
FROM table
GROUP BY color
COUNT(color)
相当于指标。
GROUP BY color
相当于桶。
聚合例子:
curl -XGET 'localhost:9200/text/_search?pretty' -d '
{"size":0,
"aggs":
{"popular_ports":
{"terms":
{"field":"port"}
}
}
}'
对port字段做聚合,结果名称为popular_ports。size设为0表示不关心搜索结果,结果的hits部分会是空值。
计算每种颜色车辆价格的平均值
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
嵌套桶
每个颜色的汽车制造商的分布
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"make": {
"terms": {
"field": "make"
}
}
}
}
}
}
每个颜色的汽车制造商的分布,以及各个制造商生产汽车的最高和最低价格。
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": { "avg": { "field": "price" }
},
"make" : {
"terms" : {
"field" : "make"
},
"aggs" : {
"min_price" : { "min": { "field": "price"} },
"max_price" : { "max": { "field": "price"} }
}
}
}
}
}
}
条形图
histogram
GET /cars/transactions/_search
{
"size" : 0,
"aggs":{
"price":{
"histogram":{
"field": "price",
"interval": 20000
},
"aggs":{
"revenue": {
"sum": {
"field" : "price"
}
}
}
}
}
}
terms桶 extended_stats度量
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"makes": {
"terms": {
"field": "make",
"size": 10
},
"aggs": {
"stats": {
"extended_stats": {
"field": "price"
}
}
}
}
}
}
最新文章
- Sql Server随机取数据
- 最长递增子序列LIS再谈
- js数组的迭代
- Android获取ip地址
- Javascript:重用之道
- java程序使用memcached
- iOS 之 设置控件在视图中心位置
- 用一个jsp实现对数据库发访问
- 记录一次因为硬盘写满造成的redis无法连接
- JAVA进阶15
- Ansible 脚本运行一次后,再次运行时出现报错情况,原因:ansible script 的格式不对,应改成Unix编码
- 毕业不到一年,绩效打了个D!
- JavaScript之函数(上)
- Python高级网络编程系列之终极篇---自己实现一个Web框架
- BZOJ 3503 [CQOI2014]和谐矩阵
- 金融量化分析【day113】:PGEC策略
- day 20 collection模块 time 模块 os 模块
- 代码直连指定ip的dubbo服务
- C#读取配置文件app.config
- Spring之事务操作(注解)