聚合
POST /es/:target/_search
搜索文档的同时进行聚合
聚合支持
Bucketing
- terms - 支持在关键字类型和数值类型的字段上使用。
- range - 支持在数值类型和日期类型的字段上使用。
- date_range - 支持在日期类型的字段上使用。
- histogram - 支持在数值类型和日期类型的字段上使用。
- date_histogram - 支持在日期类型的字段上使用。
- auto_date_histogram - 支持在日期类型的字段上使用。
指标
- min, max, count, sum - 支持在数值类型的字段上使用。
- avg, weighted_vg - 支持在数值类型的字段上使用。
- Cardinality - 支持在数值类型的字段上使用。
请求示例
POST http://localhost:4080/es/myindex/_search
请求体:
{
"query": {
"bool": {
"should": {
"match": {
"_all": "Ice Hockey"
}
}
}
},
"sort": ["-@timestamp"],
"from": 0,
"size": 20,
"aggs": {
"Medal": {
"terms": {
"field": "Medal",
"size": 10
}
},
"Year": {
"range": {
"field": "Year",
"ranges": [
{ "from": 1900, "to": 1920},
{ "from": 1921, "to": 1950},
{ "from": 1951, "to": 2000},
{ "from": 2000, "to": 2021}
]
}
},
"@timestamp": {
"date_range": {
"field": "@timestamp",
"ranges": [
{ "from": "2020-01-21T09:22:50.604Z", "to": "2021-01-21T09:22:50.604Z"},
{ "from": "2021-01-22T09:22:50.604Z", "to": "2023-01-21T09:22:50.604Z"}
]
}
},
"max_Year": {
"max": { "field": "Year" }
},
"min_Year": {
"min": { "field": "Year" }
},
"avg_Year": {
"avg": { "field": "Year" }
},
"weighted_avg_Year": {
"weighted_avg": {
"field": "Year",
"weight_field": "Year"
}
},
"sum_Year": {
"sum": { "field": "Year" }
},
"count_Sport": {
"count": { "field": "Sport" }
}
}
}
响应
{
"took": 250,
"timed_out": false,
"hits": {
"total": {
"value": 3051
},
"max_score": 17.82873837347811,
"hits": null
},
"aggregations": {
"@timestamp": {
"buckets": [
{
"key": "[2020-01-21T09:22:50Z,2021-01-21T09:22:50Z)",
"doc_count": 0
},
{
"key": "[2021-01-22T09:22:50Z,2023-01-21T09:22:50Z)",
"doc_count": 6102
}
]
},
"Medal": {
"buckets": [
{
"key": "gold",
"doc_count": 1026
},
{
"key": "silver",
"doc_count": 1024
},
{
"key": "bronze",
"doc_count": 1001
}
]
},
"Year": {
"buckets": [
{
"key": "[1900.000000,1920.000000)",
"doc_count": 132
},
{
"key": "[1921.000000,1950.000000)",
"doc_count": 2292
},
{
"key": "[1951.000000,2000.000000)",
"doc_count": 9822
},
{
"key": "[2000.000000,2021.000000)",
"doc_count": 5646
}
]
},
"avg_Year": {
"value": 1980.4818092428711
},
"count_Sport": {
"value": 3051
},
"max_Year": {
"value": 2014
},
"min_Year": {
"value": 1908
},
"sum_Year": {
"value": 36254700
},
"weighted_avg_Year": {
"value": 1980.4818092428711
}
},
"error": ""
}