- Terms Aggregation 根据字段项分组聚合
{
"query": {
"term": {
"teamCityEn": {
"value": "Memphis"
}
}
},
"aggs": {
"ageTerms": {
"terms": {
"field": "age",
"size": 10
}
}
},
"size": 0
}
- order 分组聚合排序
{
"query": {
"term": {
"teamCityEn": {
"value": "Memphis"
}
}
},
"aggs": {
"ageTerms": {
"terms": {
"field": "age",
"size": 10,
"order": {
"_key": "desc" (根据文档数排序则 _count)
}
}
}
},
"size": 0
}
- 分组平均值排序
{
"aggs": {
"ageTerms": {
"terms": {
"field": "teamCityEn",
"size": 10,
"order": {
"agsAge": "desc"
}
},
"aggs": {
"agsAge": {
"avg": {
"field": "age"
}
}
}
}
},
"size": 0
}
- 筛选分组聚合
{
"aggs": {
"ageTerms": {
"terms": {
"field": "teamCityEn",
"include": ["Milwaukee","Dallas","Los Angeles"],
"exclude": ["Miami"],
"size": 10,
"order": {
"agsAge": "desc"
}
},
"aggs": {
"agsAge": {
"avg": {
"field": "age"
}
}
}
}
},
"size": 0
}
- Range Aggregation 范围分组聚合
{
"aggs": {
"ageRage": {
"range": {
"field": "age",
"ranges": [
{
"to": 25,
"key": "黄金周期"
},
{
"from": 25,
"to": 35,
"key": "衰退期"
},{
"from": 35,
"key": "退役"
}
]
}
}
},
"size": 0
}
- Date Range Aggregation 时间范围分组聚合
{
"aggs": {
"birthDayRange":{
"date_range": {
"field": "birthDay",
"format": "yyyy-MM-dd",
"ranges": [
{
"to": "1980-01-01"
},
{
"from": "1980-01-01",
"to": "1990-01-01"
},
{
"from": "1990-01-01"
}
]
}
}
},
"size": 0
}
- Date Histogram Aggregation 时间柱状图聚合
按天、月、年等进行聚合统计。可按 year (1y), quarter (1q), month (1M), week (1w), day(1d), hour (1h), minute (1m), second (1s) 间隔聚合
{
"aggs": {
"birthDayRange":{
"date_histogram": {
"field": "birthDay",
"format": "yyyy-MM-dd",
"calendar_interval": "month"
}
}
},
"size": 0
}