elasticsearch中數據聚合問題

因爲項目中最近用到了elasticsearch,而且用到elasticsearch的聚合(Aggregation)功能,就深刻研究了一下,elasticsearch中的聚合主要有四種:Bucketing Aggregation、Metric Aggregation、Matrix Aggregation和Pipeline Aggregation。mysql

聚合的基本結構

"aggregations" : {    
    "<aggregation_name>" : {    --用戶本身起的名字
        "<aggregation_type>" : {      --聚合類型,如avg, sum
            <aggregation_body>      -- 針對的字段
        }
        [,"meta" : {  [<meta_data_body>] } ]?
        [,"aggregations" : { [<sub_aggregation>]+ } ]?    --聚合裏面能夠嵌套聚合
    }
    [,"<aggregation_name_2>" : { ... } ]*
}

Metric Aggregation

Avg Aggregation--計算平均值

請求示例:sql

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "avg_value": {
      "avg": {"field": "value"}
    }
  }
}

返回結果:elasticsearch

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 315,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg_value" : {
      "value" : 342.84761904761905
    }
  }
}

以前看到其餘博客上有說search_type=count能夠只返回aggregation部分的結果,但我在7.x版本中試了下,好像不行,這邊只能經過將size設爲0來隱藏掉除了統計數據之外的數據。函數

Cardinality Aggregation--去重(至關於mysql中的distinct)

請求示例:code

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "avg_value": {
      "cardinality": {"field": "service_id"}
    }
  }
}

返回結果:ip

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 317,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg_value" : {
      "value" : 2
    }
  }
}

Extended Status Aggragation--獲取某個字段的全部統計信息(包括平均值,最大/小值....)

請求示例:博客

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "avg_status": {
      "extended_stats": {
        "field": "value"
      }
    }
  }
}

返回結果:it

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 326,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg_status" : {
      "count" : 326,       // 數量
      "min" : 2.0,         // 最小值  
      "max" : 2481.0,      // 最大值 
      "avg" : 347.63803680981596,    // 均值
      "sum" : 113330.0,              // 和
      "sum_of_squares" : 1.02303634E8,
      "variance" : 192962.62358387595,
      "std_deviation" : 439.275111500613,
      "std_deviation_bounds" : {
        "upper" : 1226.188259811042,
        "lower" : -530.91218619141
      }
    }
  }
}

Max Aggregation--求最大值

請求示例:io

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "max_value": {
      "max": {
        "field": "value"
      }
    }
  }
}

返回結果:ast

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 352,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "max_value" : {
      "value" : 2481.0
    }
  }
}

Min Aggreegation--計算最小值

請求示例:

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "min_value": {
      "min": {
        "field": "value"
      }
    }
  }
}

返回結果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 352,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "min_value" : {
      "value" : 2.0
    }
  }
}

Percentiles Aggregation -- 百分比統計,按照[ 1, 5, 25, 50, 75, 95, 99 ]來統計

請求示例:

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "value_outlier": {
      "percentiles": {
        "field": "value"
      }
    }
  }
}

返回結果:

{
  "took" : 44,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 334,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "value_outlier" : {
      "values" : {
        "1.0" : 4.0,
        "5.0" : 67.2,
        "25.0" : 91.33333333333333,
        "50.0" : 151.0,
        "75.0" : 420.0,
        "95.0" : 1412.4000000000005,
        "99.0" : 1906.32
      }
    }
  }
}

從返回結果能夠看出來,75%的數據在420ms加載完畢。

固然咱們也能夠指定本身須要統計的百分比:

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "value_outlier": {
      "percentiles": {
        "field": "value",
        "percents": [95, 96, 99, 99.5]
      }
    }
  }
}

返回結果:

{
  "took" : 20,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 330,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "value_outlier" : {
      "values" : {
        "95.0" : 1366.0,
        "96.0" : 1449.8000000000002,
        "99.0" : 1906.3999999999999,
        "99.5" : 2064.400000000004
      }
    }
  }
}

Percentile Ranks Aggregation -- 統計返回內數據的百分比

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "value_range": {
      "percentile_ranks": {
        "field": "value",
        "values": [100, 200]
      }
    }
  }
}

返回結果:

{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 346,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "value_range" : {
      "values" : {
        "100.0" : 32.51445086705203,
        "200.0" : 65.19405450041288
      }
    }
  }
}

從返回結果能夠看出,在100ms左右加載完畢的佔了32%, 200ms左右加載完畢的佔了65%

Status Aggregation -- 狀態統計

請求示例:

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "value_status": {
      "stats": {
        "field": "value"
      }
    }
  }
}

返回結果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 355,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "value_status" : {
      "count" : 355,
      "min" : 2.0,
      "max" : 2753.0,
      "avg" : 339.8112676056338,
      "sum" : 120633.0
    }
  }
}

能夠發現跟以前的extended stats aggregation返回數據相似,只是少了一些較複雜的標準差之類的數據。

Sum Aggregation -- 求和函數

請求示例:

GET /endpoint_avg/_search
{
  "size": 0,
  "query": {"term": {
    "service_id": {
      "value": 5
    }
  }}, 
  "aggs": {
    "sum_value": {
      "sum": {
        "field": "value"
      }
    }
  }
}

返回結果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 194,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "sum_value" : {
      "value" : 91322.0
    }
  }
}

Top Hits Aggregation -- 獲取前n條數據, 能夠嵌套使用

請求示例:

GET /endpoint_avg/_search
{
  "size": 0,
  "aggs": {
    "top_tags": {
      "terms": {
        "field": "service_id",
        "size": 2
      },
      "aggs": {
        "top_value": {
          "top_hits": {
            "size": 3,
            "sort": [{
              "time_bucket": {"order": "desc"}
            }]
          }
        }
      }
    }
  }
}

返回結果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 372,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "top_tags" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 5,
          "doc_count" : 198,
          "top_value" : {
            "hits" : {
              "total" : 198,
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "endpoint_avg",
                  "_type" : "type",
                  "_id" : "201906191621_25",
                  "_score" : null,
                  "_source" : {
                    "service_id" : 5,
                    "count" : 2,
                    "time_bucket" : 201906191621,
                    "service_instance_id" : 250,
                    "entity_id" : "25",
                    "value" : 149,
                    "summation" : 299
                  },
                  "sort" : [
                    201906191621
                  ]
                },
                {
                  "_index" : "endpoint_avg",
                  "_type" : "type",
                  "_id" : "201906191620_24",
                  "_score" : null,
                  "_source" : {
                    "service_id" : 5,
                    "count" : 1,
                    "time_bucket" : 201906191620,
                    "service_instance_id" : 250,
                    "entity_id" : "24",
                    "value" : 93,
                    "summation" : 93
                  },
                  "sort" : [
                    201906191620
                  ]
                },
                {
                  "_index" : "endpoint_avg",
                  "_type" : "type",
                  "_id" : "201906191620_37",
                  "_score" : null,
                  "_source" : {
                    "service_id" : 5,
                    "count" : 1,
                    "time_bucket" : 201906191620,
                    "service_instance_id" : 250,
                    "entity_id" : "37",
                    "value" : 122,
                    "summation" : 122
                  },
                  "sort" : [
                    201906191620
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : 3,
          "doc_count" : 174,
          "top_value" : {
            "hits" : {
              "total" : 174,
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "endpoint_avg",
                  "_type" : "type",
                  "_id" : "201906191621_144",
                  "_score" : null,
                  "_source" : {
                    "service_id" : 3,
                    "count" : 1,
                    "time_bucket" : 201906191621,
                    "service_instance_id" : 238,
                    "entity_id" : "144",
                    "value" : 93,
                    "summation" : 93
                  },
                  "sort" : [
                    201906191621
                  ]
                },
                {
                  "_index" : "endpoint_avg",
                  "_type" : "type",
                  "_id" : "201906191620_70",
                  "_score" : null,
                  "_source" : {
                    "service_id" : 3,
                    "count" : 1,
                    "time_bucket" : 201906191620,
                    "service_instance_id" : 238,
                    "entity_id" : "70",
                    "value" : 192,
                    "summation" : 192
                  },
                  "sort" : [
                    201906191620
                  ]
                },
                {
                  "_index" : "endpoint_avg",
                  "_type" : "type",
                  "_id" : "201906191620_18",
                  "_score" : null,
                  "_source" : {
                    "service_id" : 3,
                    "count" : 2,
                    "time_bucket" : 201906191620,
                    "service_instance_id" : 238,
                    "entity_id" : "18",
                    "value" : 81,
                    "summation" : 162
                  },
                  "sort" : [
                    201906191620
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Value Count Aggregation--統計不一樣值的數量

請求示例:

GET /endpoint_avg/_search
{
  "size": 2, 
  "aggs": {
    "value_count": {
      "value_count": {
        "field": "value"
      }
    }
  }
}

返回結果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 357,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "endpoint_avg",
        "_type" : "type",
        "_id" : "201906191457_16",
        "_score" : 1.0,
        "_source" : {
          "service_id" : 3,
          "count" : 1,
          "time_bucket" : 201906191457,
          "service_instance_id" : 238,
          "entity_id" : "16",
          "value" : 129,
          "summation" : 129
        }
      },
      {
        "_index" : "endpoint_avg",
        "_type" : "type",
        "_id" : "201906191503_691",
        "_score" : 1.0,
        "_source" : {
          "service_id" : 5,
          "count" : 2,
          "time_bucket" : 201906191503,
          "service_instance_id" : 250,
          "entity_id" : "691",
          "value" : 178,
          "summation" : 357
        }
      }
    ]
  },
  "aggregations" : {
    "value_count" : {
      "value" : 357
    }
  }
}

基本metrics中經常使用的聚合函數就這幾種,今天太累了,其餘三類的聚合後續再作研究吧!

相關文章
相關標籤/搜索