主要内容:多种聚合分析与排序的实现
1、下钻分析之统计每季度每个品牌的销售额
GET /tvs/_search
{
"size": 0,
"aggs": {
"group_by_sold_date": {
"date_histogram": {
"field": "sold_date",
"calendar_interval": "quarter", ## quarter:季度,month:月度
"format": "yyyy-MM-dd",
"min_doc_count": 1, ##只返回有结果的聚合
"extended_bounds": {
"min": "2016-01-01",
"max": "2017-12-31"
}
},
"aggs": {
"group_by_brand": { ##对每个季度的品牌进行统计
"terms": {
"field": "brand"
},
"aggs": {
"sum_price": { ## 每个品牌的总销售额
"sum": {
"field": "price"
}
}
}
},
"total_sum_price": { ##当季的总销售额
"sum": {
"field": "price"
}
}
}
}
}
}
2、统计指定品牌下每个颜色的销量
搜索和聚合组合起来使用
任何的聚合,都必须在搜索出来的结果数据中之行,搜索结果,就是聚合分析操作的scope
GET /tvs/_search
{
"size": 0,
"query": {
"term": {
"brand": {
"value": "小米"
}
}
},
"aggs": {
"group_by_color": {
"terms": {
"field": "color"
}
}
}
}
3、单个品牌与所有品牌销量对比
出来两个结果,一个结果,是基于query搜索结果来聚合的; 另一个结果,是对所有数据执行聚合的
GET /tvs/_search
{
"size": 0,
"query": {
"term": {
"brand": {
"value": "长虹"
}
}
},
"aggs": {
"single_brand_avg_price": {
"avg": {
"field": "price"
}
},
"all": {
"global": {}, ##global:就是global bucket,就是将所有数据纳入聚合的scope
"aggs": {
"all_brand_avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
single_brand_avg_price:就是针对query搜索结果,执行的,拿到的,就是长虹品牌的平均价格
all.all_brand_avg_price:拿到所有品牌的平均价格
4、统计价格大于1200的电视平均价格
过滤+聚合
GET /tvs/_search
{
"size": 0,
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gte": 1200
}
}
}
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
5、_bucket filter:统计品牌最近一个月的平均价格
统计长虹电视不同时间范围内的平均价格:
GET /tvs/_search
{
"size": 0,
"query": {
"term": {
"brand": {
"value": "长虹" ## 如果放query里面的filter,是全局的,会对所有的数据都有影响
}
}
},
"aggs": {
"recent_150d": { ##150天内的平均价格
"filter": {
"range": {
"sold_date": {
"gte": "now-150d"
}
}
},
"aggs": {
"recent_150d_avg_price": {
"avg": {
"field": "price"
}
}
}
},
"recent_1M": {
"filter": {
"range": {
"sold_date": {
"gte": "now-1M" ##最近一个月内的平均价格
}
}
},
"aggs": {
"recent_1M_avg_price": {
"avg": {
"field": "price"
}
}
}
},
"recent_2M": {
"filter": {
"range": {
"sold_date": {
"gte": "now-2M" ##最近两个月内的平均价格
}
}
},
"aggs": {
"recent_2M_avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
aggs.filter,针对的是聚合去做的
bucket filter:对不同的bucket下的aggs,进行filter
不同时间的表达:
year(1y)年
quarter(1q)季度
month(1M)月份
week(1w)星期
day(1d)天
hour(1h)小时
minute(1m)分钟
second(1s)秒
6、按每种颜色的平均销售额降序排序
之前的排序是按照每个bucket的doc_count降序来排的,我们现在统计出来每个颜色的电视的销售额,需要按照销售额降序排序
## 未进行排序
GET /tvs/_search
{
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
## 增加排序
GET /tvs/_search
{
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color",
"order": {
"avg_price": "asc" ## 按照销售额进行升序排序
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
7、下钻分析时按最深层metric进行排序
GET /tvs/_search
{
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color"
},
"aggs": {
"group_by_brand": {
"terms": {
"field": "brand",
"order": {
"avg_price": "desc" ## 按颜色分类之后,对品牌的平均价格进行降序排序
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}