1,terms 分组,利用field分组
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"popular_colors": {
"terms": {
"field": "color"
}
}
}
}
2,avg 求平均值 ,下面的这个是在分组的情况下进行的求平均数
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
3,下钻
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"group_by_brand" : {
"terms": {
"field": "brand"
},
"aggs": {
"avg_brand_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
------------------------
aggs 是不能并列的,aggs是可以下钻(嵌套的)
aggs下面的name可以是多个
4,min max就最大值最小值
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"min_price" : {
"min": {
"field": "price"
}
},
"max_price" : {
"max": {
"field": "price"
}
},
"sum_price" : {
"sum": {
"field": "price"
}
}
}
}
}
}
5,数字区间分组 histogram
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"histogram": {
"field": "price",
"interval": 50
},
"aggs": {
"sum_price": {
"sum": {
"field": "price"
}
}
}
}
}
}
6,日期区间分组 date_histogram
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_date": {
"date_histogram": {
"field": "sold_date",
"interval": "month",
"format": "yyyy-MM-dd",
"min_doc_count": 0,
"extended_bounds" : {
"min" : "2016-01-01",
"max" : "2017-01-01"
}
}
}
}
}
7,查询到基础上分组
GET /tvs/sales/_search
{
"query": {
"match": {
"brand": "小米"
}
},
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color"
}
}
}
}
局部分组中,取全部数据进行分析
GET /tvs/sales/_search
{
"query": {
"match": {
"brand": "小米"
}
},
"size": 0,
"aggs": {
"sing_brand_avg_price": {
"avg": {
"field": "price"
}
},
"all" : {
"global": {},
"aggs": { //这里要记住,不能直接跟aggs的name,必须要继续进行下钻
"all_avg_price" : {
"avg" : {
"field" : "price"
}
}
}
}
}
}
范围搜索之后进行分组
GET /tvs/sales/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gte": 1200
}
}
}
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
对各个分组中的数据进行过滤
GET /tvs/sales/_search
{
"query": {
"match": {
"brand": "小米"
}
},
"aggs": {
"avg_brand": {
"filter" : { //aggs_name下面接filter,aggs(filter 和 aggs)
"range": {
"price": {
"gte": 1200
}
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
按每种颜色的平均销售额降序排序
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color",
"order": {
"avg_price": "asc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
深层排序、
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color"
},
"aggs": {
"group_by_brand": {
"terms": {
"field": "brand",
"order": {
"avg_price": "asc" //在这一层进行排序
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
cardinality去重算法,每月销售的品牌数量
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_sold_data": {
"date_histogram": { //先用月份进行分组
"field": "sold_date",
"interval": "month"
},
"aggs": {
"distinct_brand_cnt": {
"cardinality": { //cardinality相当于count(distinct)
"field": "brand"
}
}
}
}
}
}
网站tp访问量
GET /website/logs/_search
{
"size": 0,
"aggs": {
"latency_percentiles": {
"percentiles": { //这个对于网站的平均访问时间统计很有用
"field": "timestamp",
"percents": [
50,
95,
99
]
}
},
"latency_avg" : {
"avg": {
"field": "latency"
}
}
}
}
先用省份进行分组,再分析每个省份内的tp量
GET /website/logs/_search
{
"size": 0,
"aggs": {
"group_by_province": {
"terms": {
"field": "province"
},
"aggs": {
"latency_percentiles": {
"percentiles": {
"field": "timestamp",
"percents": [
50,
95,
99
]
}
},
"latency_avg" : {
"avg": {
"field": "latency"
}
}
}
}
}
}
percentiles rank以及网站访问时延SLA统计
GET /website/logs/_search
{
"size": 0,
"aggs": {
"group_by_province": {
"terms": {
"field": "province"
},
"aggs": {
"load_time": {
"percentile_ranks": {
"field": "latency",
"values": [
200,
1000
]
}
}
}
}
}
}