1. 程式人生 > >ElasticSearch學習筆記之二十八 細說Pipeline Aggregations

ElasticSearch學習筆記之二十八 細說Pipeline Aggregations

ElasticSearch學習筆記之二十八 細說Pipeline Aggregations

Avg Bucket Aggregation(平均值分組聚合)

Avg Bucket Aggregation是一個會計算同級聚合指定指標的平均值的同級pipeline aggregation 。指定的指標必須是數字型,同級聚合必須是多分組聚合。

Syntax(語法)

avg_bucket aggregation結構如下:

{
    "avg_bucket"
: { "buckets_path": "the_sum" } }

avg_bucket 引數

引數名 說明 是否必須 預設值
buckets_path 計算平均值的分組聚合路徑 (更多參見 buckets_path Syntax) Required
gap_policy 資料出現控制的處理策略 (更多參見Dealing with gaps in the data) Optional skip
format 聚合輸出的格式化 Optional null

下面的案例展示所有月份銷售總額的平均值:

POST /_search
{
  "size": 0,
  "aggs": {
    "sales_per_month": {
      "date_histogram": {
        "field": "date",
        "interval": "month"
      },
      "aggs": {
        "sales": {
          "sum": {
            "field": "price"
          }
        }
      }
    },
    "avg_monthly_sales": {
      "avg_bucket": {
        "buckets_path": "sales_per_month>sales" 
      }
    }
  }
}

buckets_path 表明 avg_bucket 聚合希望計算 sales_per_month日期直方圖聚合內部的sales 總數指標聚合的平均值.

響應如下:

{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "sales": {
                  "value": 550.0
               }
            },
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2,
               "sales": {
                  "value": 60.0
               }
            },
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "sales": {
                  "value": 375.0
               }
            }
         ]
      },
      "avg_monthly_sales": {
          "value": 328.33333333333333
      }
   }
}

Max Bucket Aggregation(最大分組聚合)

Max Bucket Aggregation是一個會指出同級聚合指定指標最大值的分組的同級pipeline aggregation ,並且會同時返回分組key(s)和最大值,指定的指標必須是數字型,同級聚合必須是多分組聚合。

Syntax(語法)

max_bucket aggregation結構如下:

{
    "max_bucket": {
        "buckets_path": "the_sum"
    }
}

max_bucket 引數

引數名 說明 是否必須 預設值
buckets_path 計算指標最大值的分組聚合路徑 (更多參見 buckets_path Syntax) Required
gap_policy 資料出現控制的處理策略 (更多參見Dealing with gaps in the data) Optional skip
format 聚合輸出的格式化 Optional null

下面的案例展示所有月份銷售總額的最大值:

POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "month"
            },
            "aggs": {
                "sales": {
                    "sum": {
                        "field": "price"
                    }
                }
            }
        },
        "max_monthly_sales": {
            "max_bucket": {
                "buckets_path": "sales_per_month>sales" 
            }
        }
    }
}

buckets_path 表明 max_bucket 聚合希望計算 sales_per_month日期直方圖聚合內部的sales 總數指標聚合的最大值.

響應如下:

{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "sales": {
                  "value": 550.0
               }
            },
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2,
               "sales": {
                  "value": 60.0
               }
            },
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "sales": {
                  "value": 375.0
               }
            }
         ]
      },
      "max_monthly_sales": {
          "keys": ["2015/01/01 00:00:00"], #最大值可能出現在多個分組,所以keys是陣列
          "value": 550.0
      }
   }
}

Min Bucket Aggregation(最小分組聚合)

Min Bucket Aggregation是一個會指出同級聚合指定指標最小值的分組的同級pipeline aggregation ,並且會同時返回分組key(s)和最大值,指定的指標必須是數字型,同級聚合必須是多分組聚合。

Syntax(語法)

min_bucket aggregation結構如下:

{
    "min_bucket": {
        "buckets_path": "the_sum"
    }
}

min_bucket 引數

引數名 說明 是否必須 預設值
buckets_path 計算指標最小值的分組聚合路徑 (更多參見 buckets_path Syntax) Required
gap_policy 資料出現控制的處理策略 (更多參見Dealing with gaps in the data) Optional skip
format 聚合輸出的格式化 Optional null

下面的案例展示所有月份銷售總額的最小值:

POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "month"
            },
            "aggs": {
                "sales": {
                    "sum": {
                        "field": "price"
                    }
                }
            }
        },
        "min_monthly_sales": {
            "min_bucket": {
                "buckets_path": "sales_per_month>sales" 
            }
        }
    }
}

buckets_path 表明 min_bucket 聚合希望計算 sales_per_month日期直方圖聚合內部的sales 總數指標聚合的最小值.

響應如下:

{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "sales": {
                  "value": 550.0
               }
            },
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2,
               "sales": {
                  "value": 60.0
               }
            },
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "sales": {
                  "value": 375.0
               }
            }
         ]
      },
      "min_monthly_sales": {
          "keys": ["2015/02/01 00:00:00"],  #最小值可能出現在多個分組,所以keys是陣列
          "value": 60.0
      }
   }
}

Sum Bucket Aggregation(總數分組聚合)

Sum Bucket Aggregation是一個會指出同級聚合指定指標總計的分組的同級pipeline aggregation ,並且會同時返回分組key(s)和最大值,指定的指標必須是數字型,同級聚合必須是多分組聚合。

Syntax(語法)

sum_bucket aggregation結構如下:

{
    "sum_bucket": {
        "buckets_path": "the_sum"
    }
}

sum_bucket 引數

引數名 說明 是否必須 預設值
buckets_path 計算指標總計的分組聚合路徑 (更多參見 buckets_path Syntax) Required
gap_policy 資料出現控制的處理策略 (更多參見Dealing with gaps in the data) Optional skip
format 聚合輸出的格式化 Optional null

下面的案例展示所有月份銷售總額的總計:

POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "month"
            },
            "aggs": {
                "sales": {
                    "sum": {
                        "field": "price"
                    }
                }
            }
        },
        "sum_monthly_sales": {
            "sum_bucket": {
                "buckets_path": "sales_per_month>sales" 
            }
        }
    }
}

buckets_path 表明 sum_bucket 聚合希望計算 sales_per_month日期直方圖聚合內部的sales 總數指標聚合的總計.

響應如下:

{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "sales": {
                  "value": 550.0
               }
            },
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2,
               "sales": {
                  "value": 60.0
               }
            },
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "sales": {
                  "value": 375.0
               }
            }
         ]
      },
      "sum_monthly_sales": {
          "value": 985.0
      }
   }
}

Stats Bucket Aggregation(統計分組聚合)

Stats Bucket Aggregation是一個會指出同級聚合指定指標統計的分組的同級pipeline aggregation ,並且會同時返回分組key(s)和最大值,指定的指標必須是數字型,同級聚合必須是多分組聚合。

Syntax(語法)

stats_bucket aggregation結構如下:

{
    "stats_bucket": {
        "buckets_path": "the_sum"
    }
}

stats_bucket引數

引數名 說明 是否必須 預設值
buckets_path 計算指標統計的分組聚合路徑 (更多參見 buckets_path Syntax) Required
gap_policy 資料出現控制的處理策略 (更多參見Dealing with gaps in the data) Optional skip
format 聚合輸出的格式化 Optional null

下面的案例展示所有月份銷售總額的統計:

POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "month"
            },
            "aggs": {
                "sales": {
                    "sum": {
                        "field": "price"
                    }
                }
            }
        },
        "stats_monthly_sales": {
            "stats_bucket": {
                "buckets_path": "sales_per_month>sales" 
            }
        }
    }
}

buckets_path 表明 stats_bucket 聚合希望計算 sales_per_month日期直方圖聚合內部的sales 總數指標聚合的統計.

響應如下:

{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "sales": {
                  "value": 550.0
               }
            },
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2,
               "sales": {
                  "value": 60.0
               }
            },
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "sales": {
                  "value": 375.0
               }
            }
         ]
      },
      "stats_monthly_sales": {
         "count": 3,
         "min": 60.0,
         "max": 550.0,
         "avg": 328.3333333333333,
         "sum": 985.0
      }
   }
}