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

ElasticSearch學習筆記之二十七 Pipeline Aggregations

ElasticSearch學習筆記之二十七 Pipeline Aggregations

Pipeline Aggregations

Pipeline aggregations 作用於其他聚合的結果而不是文件集,用於向響應資訊新增追加資訊。Pipeline aggregations 有很多種,每種都可以從其他聚合得到不同的資訊,我們可以近似把它們分為2大類:

型別 說明
Parent 在父聚合的結果上進行聚合計算並且可以計算出新的分組或是新的聚合結果加入到現有分組中。
Sibling 在兄弟聚合(同級聚合)的結果上進行聚合計算。計算出一個新的聚合結果,結果與兄弟聚合的結果同級。

buckets_path引數的路徑值可以用來指出指標計算基於的聚合,定義這些路徑的語法可以在下面的buckets_path語法部分中找到。

Pipeline aggregations不可以有子聚合但是可以用buckets_path

引入其他的Pipeline aggregations從而將Pipeline aggregations連結起來,例如:你可以把兩個導數連線起來計算二階導數(即導數的導數)。

注意:
由於pipeline aggregations只會追加輸出結果, 當連結pipeline aggregations的時候,每個pipeline aggregations的結果都會包含在最終的響應結果裡面。

buckets_path 語法

大多數的 pipeline aggregations需要其他的聚合作為它們的輸入源,作輸入源的聚合可以通過buckets_path引數指明,要求格式如下:

AGG_SEPARATOR
= '>' ; METRIC_SEPARATOR = '.' ; AGG_NAME = <the name of the aggregation> ; METRIC = <the name of the metric (in case of multi-value metrics aggregation)> ; PATH = <AGG_NAME> [ <AGG_SEPARATOR>, <AGG_NAME> ]* [ <METRIC_SEPARATOR>, <METRIC> ] ;

例如,路徑 my_bucket>my_stats.avg表明需要對my_bucket分組聚合的my_stats 平均值指標作為pipeline aggregations的輸入源。

pipeline aggregation和路徑是相關聯的; 並不是絕對路徑,路徑也不可以順著聚合路徑向上,例如,在日期直方圖聚合中嵌入移動平均值聚合, 同級聚合the_sum指標:

POST /_search
{
    "aggs": {
        "my_date_histo":{
            "date_histogram":{
                "field":"timestamp",
                "interval":"day"
            },
            "aggs":{
                "the_sum":{    #指標
                    "sum":{ "field": "lemmings" } 
                },
                "the_movavg":{
                    "moving_avg":{ "buckets_path": "the_sum" }  #指明相對路徑指標
                }
            }
        }
    }
}

buckets_path 也可以在同級 pipeline aggregations中使用, pipeline aggregations的結果會和分組聚合的分組同級而不是嵌入它們,例如 max_bucket 聚合使用buckets_path 指定一個嵌入同級聚合的指標。

POST /_search
{
    "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"  #我們希望得到同級日期直方圖分組聚合內部的sales總數指標聚合值的最大聚合指標
            }
        }
    }
}

Special Paths(特殊路徑)

除了指明指標路徑,buckets_path也可以使用特殊的 _count" 引數路徑.讓 pipeline aggregation用文件的計數作為輸入源. 例如, 分組聚合每個分組的移動平均值可以不用給出具體路徑

POST /_search
{
    "aggs": {
        "my_date_histo": {
            "date_histogram": {
                "field":"timestamp",
                "interval":"day"
            },
            "aggs": {
                "the_movavg": {
                    "moving_avg": { "buckets_path": "_count" }  # 使用 _count 而不是指標名稱,我們就可以計算日期直方圖聚合分組文件的移動平均值
                }
            }
        }
    }
}

buckets_path也可以使用_bucket_count來表明使用多分組聚合的分組數而不是具體指標。 例如 bucket_selector可以用來指出內部索引詞聚合沒有分組的分組 。

POST /sales/_search
{
  "size": 0,
  "aggs": {
    "histo": {
      "date_histogram": {
        "field": "date",
        "interval": "day"
      },
      "aggs": {
        "categories": {
          "terms": {
            "field": "category"
          }
        },
        "min_bucket_selector": {
          "bucket_selector": {
            "buckets_path": {
              "count": "categories._bucket_count" 
            },
            "script": {
              "source": "params.count != 0"
            }
          }
        }
      }
    }
  }
}

使用_bucket_count 而不是指標名稱我們可以指出內部categories聚合沒有分組的分組 。

Dealing with dots in agg names(處理聚合名稱中有.)

處理聚集在名稱中有點的聚合或指標支援另一種語法,例如:99.9%指標可以表示為:

"buckets_path": "my_percentile[99.9]"

Dealing with gaps in the data(處理空資料)

現實中的資料不總是我們希望的,具有空值——指定域不存在的資料是時常存在的,最常見的原因如下:

  • 文件分到一個不包含需要的欄位的分組。
  • 一個或者多個分組沒有匹配文件
  • 很可能因為一個依賴的分組缺少值導致無法計算指標值。一些pipeline aggregations具有必須滿足的特定要求。(例如,由於沒有先前的值,導數不能計算第一個值的。

Gap policies 是用來通知pipeline aggregation當遇到有缺失資料時的處理策略。 所有的 pipeline aggregations 都可以配置 gap_policy 引數,它們有2種可供選擇:

方案 說明
skip 如果計算時遇到具有空值的資料則跳過,繼續執行下一個資料。
insert_zeros 當遇到空值,則將空值替換成0,然後繼續執行