ElasticSearch学习笔记之二十八 细说Pipeline Aggregations

ElasticSearch学习笔记之二十八 细说Pipeline Aggregations

  • Avg Bucket Aggregation(平均值分组聚合)
    • Syntax(语法)
    • avg_bucket 参数
  • Max Bucket Aggregation(最大分组聚合)
    • Syntax(语法)
    • max_bucket 参数
  • Min Bucket Aggregation(最小分组聚合)
    • Syntax(语法)
    • min_bucket 参数
  • Sum Bucket Aggregation(总数分组聚合)
  • Syntax(语法)
    • sum_bucket 参数
  • Stats Bucket Aggregation(统计分组聚合)
    • Syntax(语法)
    • stats_bucket参数

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
      }
   }
}

你可能感兴趣的:(ElasticSearch学习笔记之二十八 细说Pipeline Aggregations)