官方文檔鏈接
Elastic search 可以使用 aggregations 功能對查詢的數(shù)據(jù)進行聚合怒详,支持多重聚合炉媒。
下面舉一個計數(shù)聚合的例子:
{
"query":{
"bool":{
"must":[
{
"range":{
"@timestamp":{
"gte":1536805052000,
"lte":1536905052000,
"format":"epoch_millis"
}
}
}
]
}
},
"size":0,
"stored_fields":[],
"script_fields":{},
"docvalue_fields":[],
"aggs":{
"results_by_component":{
"terms":{
"field":"component_name",
"size":5
}
}
}
}
??其中, query字段描述查詢條件昆烁,這個例子中吊骤,我們只對時間做了限制,查詢指定時間段內(nèi)的所有l(wèi)og善玫。
??"size"為 0 表示顯示 0 條查詢結(jié)果水援, 在這里我們對每一條查詢結(jié)果不感興趣密强,只對聚合結(jié)果感興趣茅郎,所以將size設(shè)置為 0。
??接下來 "aggs"字段是對聚合的設(shè)置或渤。這個例子比較簡單系冗,只對查詢結(jié)果按 "component_name"進行聚合( “component_name” 是定義在每條log中的一個字段,表示這條log是由哪個component產(chǎn)生)薪鹦。
??通過上面的query掌敬,我們得到了如下結(jié)果(結(jié)果只截取 aggregations 部分):
{
"aggregations": {
"results_by_component": {
"doc_count_error_upper_bound": 120532,
"sum_other_doc_count": 4539368,
"buckets": [
{
"key": "component_test01",
"doc_count": 1762831
},
{
"key": "component_test02",
"doc_count": 1680588
},
{
"key": "component_test03",
"doc_count": 1304537
},
{
"key": "component_test04",
"doc_count": 970381
},
{
"key": "component_test05",
"doc_count": 835906
}
]
}
}
下面舉一個百分位數(shù)聚合的例子惯豆。
query內(nèi)容不變,我們將 "aggs"的內(nèi)容做如下修改:
{
"aggs": {
"percentile_time":{
"percentiles":{
"field":"response_time_ms",
"percents":[
50,
90,
95,
99,
99.9
]
}
}
}
}
統(tǒng)計"response_time_ms"的百分位數(shù)奔害,將得到如下結(jié)果:
{
"aggregations": {
"percentile_time": {
"values": {
"50.0": 18.151901586137335,
"90.0": 97.01449517757428,
"95.0": 297.5137181184368,
"99.0": 1079.8195648476355,
"99.9": 5414.805508330881
}
}
}
}
如果想得到每一個 component_name 下對"response_status"計數(shù)聚合和 "response_time_ms"的百分位數(shù)聚合楷兽,則可嵌套多個聚合條件
例子
{
"aggregations": {
"results_by_component":{
"terms":{
"field":"component_name",
"size":5
},
"aggs":{
"status_code_count":{
"terms":{
"field":"response_status",
"size":5
}
},
"percentile_time":{
"percentiles":{
"field":"response_time_ms",
"percents":[
50,
90,
95,
99,
99.9
]
}
}
}
}
}
}
聚合結(jié)果如下:
{
"aggregations": {
"results_by_component": {
"doc_count_error_upper_bound": 137336,
"sum_other_doc_count": 7650192,
"buckets": [
{
"key": "component_test01",
"doc_count": 1762831,
"percentile_time": {
"values": {
"50.0": 79.41854086887764,
"90.0": 393.42530776763,
"95.0": 423.8467834610685,
"99.0": 1100.6490415600176,
"99.9": 8980.100018441459
}
},
"status_code_count": {
"doc_count_error_upper_bound": 20,
"sum_other_doc_count": 88619,
"buckets": [
{
"key": 200,
"doc_count": 1343600
},
{
"key": 201,
"doc_count": 282345
},
{
"key": 400,
"doc_count": 48267
}
]
}
},
{
"key": "component_test02",
"doc_count": 1680588,
"percentile_time": {
"values": {
"50.0": 47.20412905186976,
"90.0": 98.34655431504568,
"95.0": 379.85813836814305,
"99.0": 1049.5351664550062,
"99.9": 8602.25736085292
}
},
"status_code_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 74103,
"buckets": [
{
"key": 200,
"doc_count": 1279955
},
{
"key": 201,
"doc_count": 282159
},
{
"key": 400,
"doc_count": 44371
}
]
}
}
]
}
}
}