ES aggregation详解
aggregation分類
aggregations —— 聚合,提供了一種基于查詢條件來對數(shù)據(jù)進(jìn)行分桶、計算的方法。有點類似于 SQL 中的 group by 再加一些函數(shù)方法的操作。
聚合可以嵌套,由此可以組成復(fù)雜的操作(Bucketing聚合可以包含sub-aggregation)。
聚合整體上可以分為 3 類:
1. Bucketing:桶分聚合:
此類聚合執(zhí)行的是對文檔分組的操作,把滿足相關(guān)特性的文檔分到一個桶里,即桶分。輸出結(jié)果往往是一個個包含多個文檔的桶。
此類聚合會有一個關(guān)鍵字(field、script),以及一些桶分(分組)的判斷條件。執(zhí)行聚合操作時候,文檔會判斷每一個分組條件,如果滿足某個,該文檔就會被分為該組(fall in)。
2. Metric:指標(biāo)聚合:
此類聚合是對文檔進(jìn)行一些權(quán)值計算(比如求所有文檔某個字段的max值)。輸出結(jié)果往往是文檔的權(quán)值,相當(dāng)于為文檔添加了一些統(tǒng)計信息。
此類聚合基于特定字段(field)或腳本值(generated using scripts),計算聚合中文檔的權(quán)值。
3. Pipeline:管道聚合:
對其它聚合操作的輸出及其關(guān)聯(lián)指標(biāo)進(jìn)行聚合。
此類聚合的作用對象往往是桶,而不是文檔,是一種后期對每個分桶的一些計算操作。
應(yīng)用場景
對于 3 中聚合,常見的應(yīng)用場景如下流程:
buckets 聚合對文檔進(jìn)行必要的歸類(桶分) ——> metric 聚合對每個桶進(jìn)行一些額外的信息計算(如:max) ——> pipeline 聚合針對所有桶做一些桶層面的統(tǒng)計或計算應(yīng)用示例:
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : { // bucket 聚合,按照月份進(jìn)行分桶,每個月的歸屬一個桶
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": { // metric 聚合,對每個桶類的 price 求和,即每月的銷售額
"field": "price"
}
}
}
},
"max_monthly_sales": {
"max_bucket": { // pipeline 聚合,求所有桶中銷售額 sales 最大的值
"buckets_path": "sales_per_month>sales"
}
}
}
}
aggregation結(jié)構(gòu)
聚合可以是父子(嵌套)關(guān)系聚合,buckets 聚合作為父,metric 聚合作為子。
聚合也可以是兄弟關(guān)系聚合,buckets 聚合在前,pipeline 聚合在后。
結(jié)構(gòu)如下:
"aggregations" : { //定義聚合對象,也可用 "aggs"
"<aggregation_name>" : { //聚合的名稱,用戶自定義
"<aggregation_type>" : { //聚合類型,比如 "histogram"
<aggregation_body> //每個聚合類型都有其自己的結(jié)構(gòu)定義
}
[,"meta" : { [<meta_data_body>] } ]?
[,"aggregations" : { [<sub_aggregation>]+ } ]? //可以定義多個 sub-aggregation
}
[,"<aggregation_name_2>" : { ... } ]* //定義額外的多個平級 aggregation,只有 Bucketing 類型才有意義
}
2 metrics aggregations
概述
權(quán)值聚合類型從需要聚合的文檔中取一個值(value)來計算文檔的相應(yīng)權(quán)值(比如該值在這些文檔中的max、sum等)。
用于計算的值(value)可以是文檔的字段(field),也可以是腳本(script)生成的值。
數(shù)值權(quán)值聚合是特殊的權(quán)值聚合類型,因為它的輸出權(quán)值也是數(shù)字。
數(shù)值權(quán)值聚合(注意分類只針對數(shù)值權(quán)值聚合,非數(shù)值的無此分類)輸出單個權(quán)值的,叫做 single-value numeric metrics,其它生成多個權(quán)值(比如:stats)的被叫做 multi-value numeric metrics。
單值和多值數(shù)字權(quán)值聚合,在它們作為一些 Bucket 聚合的直接子聚合的時候會有明顯區(qū)別。
Avg Aggregation(single-value numeric metrics)
均值聚合——基于文檔的某個值,計算該值在聚合文檔中的均值。
用于計算的值可以是特定的數(shù)值型字段,也可以通過腳本計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
missing:文檔缺省字段時的默認(rèn)值
{
"aggs" : {
"avg_grade" : { "avg" : { "field" : "grade" } } //計算字段 grade 在文檔中的平均值
}
}
//輸出
{
...
"aggregations": {
"avg_grade": {
"value": 75
}
}
}
Cardinality Aggregation(single-value)
基數(shù)聚合——基于文檔的某個值,計算文檔非重復(fù)的個數(shù)(去重計數(shù))。
用于計算的值可以是特定的字段,也可以通過腳本計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
precision_threshold:
missing:文檔缺省字段時的默認(rèn)值
{
"aggs" : {
"author_count" : {
"cardinality" : {
"field" : "author" //count the unique authors that match a query
}
}
}
}
stats aggregation(multi-value)
統(tǒng)計聚合——基于文檔的某個值,計算出一些統(tǒng)計信息(min、max、sum、count、avg)。
用于計算的值可以是特定的數(shù)值型字段,也可以通過腳本計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
missing:文檔缺省字段時的默認(rèn)值
{
"aggs" : {
"grades_stats" : { "stats" : { "field" : "grade" } }
}
}
//輸出
{
...
"aggregations": {
"grades_stats": {
"count": 6,
"min": 60,
"max": 98,
"avg": 78.5,
"sum": 471
}
}
}
Extended Stats Aggregation(multi-value)
擴(kuò)展統(tǒng)計聚合——基于文檔的某個值,計算出一些統(tǒng)計信息(比普通的stats聚合多了sum_of_squares、variance、std_deviation、std_deviation_bounds)。
用于計算的值可以是特定的數(shù)值型字段,也可以通過腳本計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
missing:文檔缺省字段時的默認(rèn)值
sigma:標(biāo)準(zhǔn)差界限
{
...
"aggregations": {
"grade_stats": {
"count": 9,
"min": 72,
"max": 99,
"avg": 86,
"sum": 774,
//輸出比 stats 聚合多了一些值
"sum_of_squares": 67028,
"variance": 51.55555555555556,
"std_deviation": 7.180219742846005,
"std_deviation_bounds": {
"upper": 100.36043948569201,
"lower": 71.63956051430799
}
}
}
}
Geo Bounds Aggregation
地理邊界聚合——基于文檔的某個字段(geo-point類型字段),計算出該字段所有地理坐標(biāo)點的邊界(左上角/右下角坐標(biāo)點)。
配置參數(shù)
field:用于計算的字段
wrap_longitude:是否允許地理邊界與國際日界線存在重疊
{
"query" : {
"match" : { "business_type" : "shop" }
},
"aggs" : {
"viewport" : {
"geo_bounds" : {
"field" : "location",
"wrap_longitude" : true
}
}
}
}
//輸出
{
...
"aggregations": {
"viewport": {
"bounds": {
"top_left": { //左上角經(jīng)緯度
"lat": 80.45,
"lon": -160.22
},
"bottom_right": { //右下角經(jīng)緯度
"lat": 40.65,
"lon": 42.57
}
}
}
}
}
Geo Centroid Aggregation
地理重心聚合——基于文檔的某個字段(geo-point類型字段),計算所有坐標(biāo)的加權(quán)重心。
配置參數(shù)
field:用于計算的字段(geo-point類型)
{
"query" : {
"match" : { "crime" : "burglary" }
},
"aggs" : {
"centroid" : {
"geo_centroid" : {
"field" : "location"
}
}
}
}
//輸出
{
...
"aggregations": {
"centroid": {
"location": { //重心經(jīng)緯度
"lat": 80.45,
"lon": -160.22
}
}
}
}
Max Aggregation(single)
最大值聚合——基于文檔的某個值,求該值在聚合文檔中的最大值。
用于計算的值可以是特定的數(shù)值型字段,也可以通過腳本計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
missing:文檔缺省字段時的默認(rèn)值
Min Aggregation(single)
最小值聚合——基于文檔的某個值,求該值在聚合文檔中的最小值。
用于計算的值可以是特定的數(shù)值型字段,也可以通過腳本計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
missing:文檔缺省字段時的默認(rèn)值
Sum Aggregation(single-value)
求和聚合——基于文檔的某個值,求該值在聚合文檔中的統(tǒng)計和。
用于計算的值可以是特定的數(shù)值型字段,也可以通過腳本計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
missing:文檔缺省字段時的默認(rèn)值
//最大值,field
{
"aggs" : {
"max_price" : { "max" : { "field" : "price" } } // field
}
}
//最小值,script
{
"aggs" : {
"min_price" : {
"min" : {
"script" : { //script 計算 value
"file": "my_script",
"params": {
"field": "price"
}
}
}
}
}
}
//總和,value script
{
"aggs" : {
...
"aggs" : {
"daytime_return" : {
"sum" : {
"field" : "change", // field
"script" : "_value * _value" // 基于 field 用 script 計算 value
}
}
}
}
}
Percentiles Aggregation(multi-value)
百分百聚合——基于聚合文檔中某個數(shù)值類型的值,求這些值中
用于計算的值可以是特定的數(shù)值型字段,也可以通過腳本計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
missing:文檔缺省字段時的默認(rèn)值
Script Metric Aggregation
基于腳本的權(quán)值聚合——用腳本來計算出一個權(quán)值
配置參數(shù)
init_script:用于計算的字段
map_script:由腳本生成用來計算的 value
combine_script:文檔缺省字段時的默認(rèn)值
reduce_script:
{
"query" : {
"match_all" : {}
},
"aggs": {
"profit": {
"scripted_metric": {
"init_script" : "_agg['transactions'] = []",
"map_script" : "if (doc['type'].value == "sale") { _agg.transactions.add(doc['amount'].value) } else { _agg.transactions.add(-1 * doc['amount'].value) }",
"combine_script" : "profit = 0; for (t in _agg.transactions) { profit += t }; return profit",
"reduce_script" : "profit = 0; for (a in _aggs) { profit += a }; return profit"
}
}
}
}
Top hits Aggregation
最高匹配權(quán)值聚合——跟蹤聚合中相關(guān)性最高的文檔。
該聚合一般用做 sub-aggregation,以此來聚合每個桶中的最高匹配的文檔。
配置參數(shù)
from:最匹配的結(jié)果中的文檔個數(shù)偏移
size:top matching hits 返回的最大文檔個數(shù)(default 3)
sort:最匹配的文檔的排序方式
{
"aggs": {
"top-tags": {
"terms": {
"field": "tags",
"size": 3
},
"aggs": {
"top_tag_hits": {
"top_hits": { //用 tags 字段分組,每個 tag(即一個分組)只顯示最后一個問題,并且只在 _source 中保留 title 字段
"sort": [
{
"last_activity_date": {
"order": "desc"
}
}
],
"_source": {
"include": [
"title"
]
},
"size" : 1
}
}
}
}
}
}
//輸出
"top_tags_hits": {
"hits": {
"total": 25365,
"max_score": 1,
"hits": [
{
"_index": "stack",
"_type": "question",
"_id": "602679",
"_score": 1,
"_source": {
"title": "Windows port opening"
},
"sort": [
1370143231177
]
}
]
}
}
Value Count Aggregation(single-value)
值計數(shù)聚合——計算聚合文檔中某個值的個數(shù)。
用于計數(shù)的值可以是特定的數(shù)值型字段,也可以通過腳本計算而來。
該聚合一般域其它 single-value 聚合聯(lián)合使用,比如在計算一個字段的平均值的時候,可能還會關(guān)注這個平均值是由多少個值計算而來。
配置參數(shù)
field:用于計算的字段
script:由腳本生成用來計算的 value
{
"aggs" : {
"grades_count" : { "value_count" : { "field" : "grade" } } //計算 grade 字段共有多少個值,和 cardinality 聚合不同的
}
}
3 bucket aggregation
概述
桶分聚合不進(jìn)行權(quán)值的計算,他們對文檔根據(jù)聚合請求中提供的判斷條件(比如:{"from":0, "to":100})來進(jìn)行分組(桶分)。
桶分聚合還會額外返回每一個桶內(nèi)文檔的個數(shù)。
桶分聚合可以包含子聚合——sub-aggregations(權(quán)值聚合不能包含子聚合,可以作為子聚合),子聚合操作將會應(yīng)用到由父(parent)聚合產(chǎn)生的每一個桶上。
桶分聚合根據(jù)聚合條件,可以只定義輸出一個桶;也可以輸出多個;還可以在根據(jù)聚合條件動態(tài)確定桶個數(shù)(比如:terms aggregation)。
Histogram Aggregation(multi-bucket)
直方圖聚合——基于文檔中的某個【數(shù)值類型】字段,通過計算來動態(tài)的分桶。
一個文檔屬于某個桶,計算過程大致如下:
rem = value % interval
if (rem < 0) {
rem += interval
}
bucket_key = value - rem
配置參數(shù)
field:字段,必須為數(shù)值類型
interval:分桶間距
min_doc_count:最少文檔數(shù)桶過濾,只有不少于這么多文檔的桶才會返回
extended_bounds:范圍擴(kuò)展
order:對桶排序,如果 histogram 聚合有一個權(quán)值聚合類型的"直接"子聚合,那么排序可以使用子聚合中的結(jié)果
offset:桶邊界位移,默認(rèn)從0開始
keyed:hash結(jié)構(gòu)返回,默認(rèn)以數(shù)組形式返回每一個桶
missing:配置缺省默認(rèn)值
{
"aggs" : {
"prices" : {
"histogram" : {
"field" : "price",
"interval" : 50,
"min_doc_count" : 1,
"extended_bounds" : {
"min" : 0,
"max" : 500
},
"order" : { "_count" : "desc" },
"keyed":true,
"missing":0
}
}
}
}
Data Histogram Aggregation(multi-bucket)
日期直方圖聚合——基于日期類型,以【日期間隔】來桶分聚合。
可用的時間間隔類型為:year、quarter、month、week、day、hour、minute、second,其中,除了year、quarter 和 month,其余可用小數(shù)形式。
配置參數(shù)
field:
interval:
format:定義日期的格式,配置后會返回一個 key_as_string 的字符串類型日期(默認(rèn)只有key)
time_zone:定義時區(qū),用作時間值的調(diào)整
offset:
missing:
{
"aggs" : {
"articles_over_time" : {
"date_histogram" : {
"field" : "date",
"interval" : "month",
"format" : "yyyy-MM-dd",
"time_zone": "+08:00"
}
}
}
}
Range Aggregation(multi-bucket)
范圍聚合——基于某個值(可以是 field 或 script),以【字段范圍】來桶分聚合。
范圍聚合包括 from 值,不包括 to 值(區(qū)間前閉后開)。
配置參數(shù)
ranges:配置區(qū)間,數(shù)組,每一個元素是一個區(qū)間。例如:[{from:0}, {from:50, to:100}, {to:200}]
keyed:以一個關(guān)聯(lián)的唯一字符串作為鍵,以 HASH 形式返回,而不是默認(rèn)的數(shù)組
script:利用 script 執(zhí)行結(jié)果替代普通的 field 值進(jìn)行聚合。script可以用file給出,還可以對其它 field 進(jìn)行求值計算。
{
"aggs" : {
"price_ranges" : {
"range" : {
"field" : "price",
"ranges" : [ //包含 3 個桶
{ "to" : 50 },
{ "from" : 50, "to" : 100 },
{ "from" : 100 }
],
"keyed" : true
}
}
}
}
Date Range Aggregation(multi-bucket)
日期范圍聚合——基于日期類型的值,以【日期范圍】來桶分聚合。
日期范圍可以用各種Date Math表達(dá)式。
同樣的,包括 from 的值,不包括 to 的值。
配置參數(shù)
format:定義日期格式,配置后會返回一個 [to/from]_as_string 的字符串類型日期,默認(rèn)是 to/from 的數(shù)值表示
{
"aggs": {
"range": {
"date_range": {
"field": "date",
"format": "MM-yyy",
"ranges": [ //包含 3 個桶
{ "to": "now-10M/M" },
{ "from": "now-10M/M" },
{"from":"1970-1-1", "to":"2000-1-1"}
]
}
}
}
}
Terms Aggregation(multi-bucket)
詞元聚合——基于某個field,該 field 內(nèi)的每一個【唯一詞元】為一個桶,并計算每個桶內(nèi)文檔個數(shù)。
默認(rèn)返回順序是按照文檔個數(shù)多少排序。
當(dāng)不返回所有 buckets 的情況,文檔個數(shù)可能不準(zhǔn)確。
配置參數(shù)
size:size用來定義需要返回多個 buckets(防止太多),默認(rèn)會全部返回。(注意,如果只返回部分buckets,統(tǒng)計的文檔個數(shù)不一定準(zhǔn)確(每個分片各自的top size個)。size 越大,count 會越精確。)
order:排序方式
min_doc_count:只返回文檔個數(shù)不小于該值的 buckets
script:用基本來生成詞元
include:包含過濾
exclude:排除過濾
execution_hint:
collect_mode:
missing:
{
"aggs" : {
"genders" : {
"terms" : {
"field" : "gender",
"size" : 5,
"order" : { "_count" : "asc" },
"min_doc_count": 10,
"include" : ".*sport.*",
"exclude" : "water_.*",
"missing": "N/A"
}
}
}
}
Filters Aggregation(multi-bucket)
多過濾聚合——基于多個過濾條件,來對當(dāng)前文檔進(jìn)行【過濾】的聚合,每個過濾都包含所有滿足它的文檔(多個bucket中可能重復(fù))。
配置參數(shù)
filters: 配置過濾條件,支持 HASH 或 數(shù)組格式
other_bucket: 是否計算不滿足任何匹配條件的文檔
other_bucket_key: 作為不匹配所有過濾條件的文檔的 bucket 名稱
{
"aggs" : {
"messages" : {
"filters" : {
"other_bucket_key": "other_messages", //不在過濾條件范圍內(nèi)的文檔都?xì)w屬于 other_messages 桶
"filters" : { //過濾條件
"errors" : { "term" : { "body" : "error" }},
"warnings" : { "term" : { "body" : "warning" }}
}
},
"aggs" : {
"monthly" : {
"histogram" : {
"field" : "timestamp",
"interval" : "1M"
}
}
}
}
}
}
Filter Aggregation(single-bucket)
過濾聚合——基于一個條件,來對當(dāng)前的文檔進(jìn)行過濾的聚合。
{
"aggs" : {
"red_products" : {
"filter" : { "term": { "color": "red" } },
"aggs" : {
"avg_price" : { "avg" : { "field" : "price" } }
}
}
}
}
IPv4 Range Aggregation(multi-bucket)
IP4聚合——基于一個 IPv4 字段,對文檔進(jìn)行【IPv4范圍】的桶分聚合。
和 Range Aggregation 類似,只是應(yīng)用字段必須是 IPv4 數(shù)據(jù)類型。
{
"aggs" : {
"ip_ranges" : {
"ip_range" : {
"field" : "ip",
"ranges" : [ //包含 3 個桶,各個桶之間可能有文檔重復(fù)
{ "to" : "10.0.0.5" },
{ "from" : "10.0.0.5" },
{ "from":"1.1.1.1", "to" : "10.0.0.5" },
]
}
}
}
}
Nested Aggregation(single-bucket)
嵌套類型聚合——基于嵌套(nested)數(shù)據(jù)類型,把該【嵌套類型的信息】聚合到單個桶里,然后就可以對嵌套類型做進(jìn)一步的聚合操作。
// resellers 是一個嵌套類型
{
...
"product" : {
"properties" : {
"resellers" : {
"type" : "nested",
"properties" : {
"name" : { "type" : "string" },
"price" : { "type" : "double" }
}
}
}
}
}
// 對 nested 對象里面的信息做其它聚合操作
{
"query" : {
"match" : { "name" : "led tv" }
},
"aggs" : {
"resellers" : {
"nested" : { //"嵌套類型聚合"把所有嵌套信息都包含在單一的桶里,以供進(jìn)一步處理
"path" : "resellers"
},
"aggs" : {
"min_price" : { "min" : { "field" : "resellers.price" } } //對嵌套類型聚合輸出的桶做進(jìn)一步處理,這里是計算其 price 的 average
}
}
}
}
4 pipeline aggregations
概述
管道聚合處理的對象是其它聚合的輸出(桶或者桶的某些權(quán)值),而不是直接針對文檔。
管道聚合的作用是為輸出增加一些有用信息。
管道聚合大致分為兩類:
parent
此類聚合的"輸入"是其【父聚合】的輸出,并對其進(jìn)行進(jìn)一步處理。一般不生成新的桶,而是對父聚合桶信息的增強。
sibling
此類聚合的輸入是其【兄弟聚合】的輸出。并能在同級上計算新的聚合。
管道聚合通過 buckets_path 參數(shù)指定他們要進(jìn)行聚合計算的權(quán)值對象,buckets_path 參數(shù)有其自己的使用語法。
管道聚合不能包含子聚合,但是某些類型的管道聚合可以鏈?zhǔn)绞褂茫ū热缬嬎銓?dǎo)數(shù)的導(dǎo)數(shù))。
bucket_path語法
1. 聚合分隔符 ==>">",指定父子聚合關(guān)系,如:"my_bucket>my_stats.avg"
2. 權(quán)值分隔符 ==>".",指定聚合的特定權(quán)值
3. 聚合名稱 ==> <name of the aggregation> ,直接指定聚合的名稱
4. 權(quán)值 ==> <name of the metric> ,直接指定權(quán)值
5. 完整路徑 ==>agg_name[> agg_name]*[. metrics] ,綜合利用上面的方式指定完整路徑
6. 特殊值 ==>"_count",輸入的文檔個數(shù)
特殊情況
1. 要進(jìn)行 pipeline aggregation 聚合的對象名稱或權(quán)值名稱包含小數(shù)點
"buckets_path": "my_percentile[99.9]"
2. 處理對象中包含空桶(無文檔的桶分)
參數(shù) gap_policy,可選值有 skip、insert_zeros
Avg Bucket Aggregation(sibliing)
桶均值聚合——基于兄弟聚合的某個權(quán)值,求所有桶的權(quán)值均值。
用于計算的兄弟聚合必須是多桶聚合。
用于計算的權(quán)值必須是數(shù)值類型。
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
}
}
},
"avg_monthly_sales": {
"avg_bucket": { //對所有月份的銷售總 sales 求平均值
"buckets_path": "sales_per_month>sales"
}
}
}
}
Derivative Aggregation(parent)
求導(dǎo)聚合——基于父聚合(只能是histogram或date_histogram類型)的某個權(quán)值,對權(quán)值求導(dǎo)。
用于求導(dǎo)的權(quán)值必須是數(shù)值類型。
封閉直方圖(histogram)聚合的 min_doc_count 必須是 0。
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
},
"sales_deriv": { //對每個月銷售總和 sales 求導(dǎo)
"derivative": {
"buckets_path": "sales" //同級,直接用 metric 值
}
}
}
}
}
}
Max Bucket Aggregation(sibling)
桶最大值聚合——基于兄弟聚合的某個權(quán)值,輸出權(quán)值最大的那一個桶。
用于計算的權(quán)值必須是數(shù)值類型。
用于計算的兄弟聚合必須是多桶聚合類型。
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
Min Bucket Aggregation(sibling)
桶最小值聚合——基于兄弟聚合的某個權(quán)值,輸出權(quán)值最小的一個桶。
用于計算的權(quán)值必須是數(shù)值類型。
用于計算的兄弟聚合必須是多桶聚合類型。
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
Sum Buchet Aggregation(sibling)
桶求和聚合——基于兄弟聚合的權(quán)值,對所有桶的權(quán)值求和。
用于計算的權(quán)值必須是數(shù)值類型。
用于計算的兄弟聚合必須是多桶聚合類型。
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
}
}
},
"max_monthly_sales": { //輸出兄弟聚合 sales_per_month 的每月銷售總和 sales 的最大一個桶
"max_bucket": {
"buckets_path": "sales_per_month>sales"
}
},
"min_monthly_sales": { //輸出兄弟聚合 sales_per_month 的每月銷售總和 sales 的最小一個桶
"min_bucket": {
"buckets_path": "sales_per_month>sales"
}
},
"sum_monthly_sales": { //輸出兄弟聚合 sales_per_month 的每月銷售總和 sales 的最小一個桶
"sum_bucket": {
"buckets_path": "sales_per_month>sales"
}
}
}
}
Stats Bucket Aggregation(sibling)
桶統(tǒng)計信息聚合——基于兄弟聚合的某個權(quán)值,對【桶的信息】進(jìn)行一些統(tǒng)計學(xué)運算(總計多少個桶、所有桶中該權(quán)值的最大值、最小等)。
用于計算的權(quán)值必須是數(shù)值類型。
用于計算的兄弟聚合必須是多桶聚合類型。
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
}
}
},
"stats_monthly_sales": { // 對父聚合的每個桶(每月銷售總和)的一些基本信息進(jìn)行聚合
"stats_bucket": {
"buckets_paths": "sales_per_month>sales"
}
}
}
}
//輸出結(jié)果
{
"aggregations": {
"sales_per_month": {
"buckets": [
{
"key_as_string": "2015/01/01 00:00:00",
"key": 1420070400000,
"doc_count": 3,
"sales": {
"value": 550
}
},
{
"key_as_string": "2015/02/01 00:00:00",
"key": 1422748800000,
"doc_count": 2,
"sales": {
"value": 60
}
},
{
"key_as_string": "2015/03/01 00:00:00",
"key": 1425168000000,
"doc_count": 2,
"sales": {
"value": 375
}
}
]
},
"stats_monthly_sales": { //注意,統(tǒng)計的是桶的信息
"count": 3,
"min": 60,
"max": 550,
"avg": 328.333333333,
"sum": 985
}
}
}
Extended Stats Bucket Aggregation(sibling)
擴(kuò)展桶統(tǒng)計聚合——基于兄弟聚合的某個權(quán)值,對【桶信息】進(jìn)行一系列統(tǒng)計學(xué)計算(比普通的統(tǒng)計聚合多了一些統(tǒng)計值)。
用于計算的權(quán)值必須是數(shù)值類型。
用于計算的兄弟聚合必須是多桶聚合類型。
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
sigma:偏差顯示位置(above/below)
Percentiles Bucket Aggregation(sibling)
桶百分比聚合——基于兄弟聚合的某個權(quán)值,計算權(quán)值的百分百。
用于計算的權(quán)值必須是數(shù)值類型。
用于計算的兄弟聚合必須是多桶聚合類型。
對百分百的計算是精確的(不像Percentiles Metric聚合是近似值),所以可能會消耗大量內(nèi)存
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
percents:需要計算的百分百列表(數(shù)組形式)
Moving Average Aggregation(parent)
窗口平均值聚合——基于已經(jīng)排序過的數(shù)據(jù),計算出處在當(dāng)前出口中數(shù)據(jù)的平均值。
比如窗口大小為 5 ,對數(shù)據(jù) 1—10 的部分窗口平均值如下:
(1 + 2 + 3 + 4 + 5) / 5 = 3
(2 + 3 + 4 + 5 + 6) / 5 = 4
(3 + 4 + 5 + 6 + 7) / 5 = 5
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
window:窗口大小
model:移動模型
minimize:
settings:
{
"the_movavg":{
"moving_avg":{
"buckets_path": "the_sum",
"window" : 30,
"model" : "simple"
}
}
}
Cumulative Sum Aggregation(parent)
累計和聚合——基于父聚合(只能是histogram或date_histogram類型)的某個權(quán)值,對權(quán)值在每一個桶中求所有之前的桶的該值累計的和。
用于計算的權(quán)值必須是數(shù)值類型。
封閉直方圖(histogram)聚合的 min_doc_count 必須是 0。
配置參數(shù)
buckets_path:用于計算均值的權(quán)值路徑
format:該聚合的輸出格式定義
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
},
"cumulative_sales": {
"cumulative_sum": {
"buckets_path": "sales"
}
}
}
}
}
}
//輸出
{
"aggregations": {
"sales_per_month": {
"buckets": [
{
"key_as_string": "2015/01/01 00:00:00",
"key": 1420070400000,
"doc_count": 3,
"sales": {
"value": 550
},
"cumulative_sales": {
"value": 550 //總計 sales = 550
}
},
{
"key_as_string": "2015/02/01 00:00:00",
"key": 1422748800000,
"doc_count": 2,
"sales": {
"value": 60
},
"cumulative_sales": {
"value": 610 //總計 sales = 550 + 60
}
},
Bucket Script Aggregation(parent)
桶腳本聚合——基于父聚合的【一個或多個權(quán)值】,對這些權(quán)值通過腳本進(jìn)行運算。
用于計算的父聚合必須是多桶聚合。
用于計算的權(quán)值必須是數(shù)值類型。
執(zhí)行腳本必須要返回數(shù)值型結(jié)果。
配置參數(shù)
script:用于計算的腳本,腳本可以是 inline,也可以是 file,還可以是Scripting指定的
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"total_sales": {
"sum": {
"field": "price"
}
},
"t-shirts": {
"filter": {
"term": {
"type": "t-shirt"
}
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
}
}
},
"t-shirt-percentage": {
"bucket_script": {
"buckets_path": { //對兩個權(quán)值進(jìn)行計算
"tShirtSales": "t-shirts>sales",
"totalSales": "total_sales"
},
"script": "tShirtSales / totalSales * 100"
}
}
}
}
}
}
Bucket Selector Aggregation(parent)
桶選擇器聚合——基于父聚合的【一個或多個權(quán)值】,通過腳本對權(quán)值進(jìn)行計算,并決定父聚合的哪些桶需要保留,其余的將被丟棄。
用于計算的父聚合必須是多桶聚合。
用于計算的權(quán)值必須是數(shù)值類型。
運算的腳本必須是返回 boolean 類型,如果腳本是腳本表達(dá)式形式給出,那么允許返回數(shù)值類型。
配置參數(shù)
script:用于計算的腳本,腳本可以是 inline,也可以是 file,還可以是Scripting指定的
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
{
"bucket_selector": {
"buckets_path": {
"my_var1": "the_sum",
"my_var2": "the_value_count"
},
"script": "my_var1 > my_var2" // true 則保留該桶;false 則丟棄
}
}
Serial Differencing Aggregation(parent)
串行差分聚合——基于父聚合(只能是histogram或date_histogram類型)的某個權(quán)值,對權(quán)值值進(jìn)行差分運算,(取時間間隔,后一刻的值減去前一刻的值:f(X) = f(Xt) – f(Xt-n))。
用于計算的父聚合必須是多桶聚合。
配置參數(shù)
lag:滯后間隔(比如lag=7,表示每次從當(dāng)前桶的值中減去其前面第7個桶的值)
buckets_path:用于計算均值的權(quán)值路徑
gap_policy:空桶處理策略(skip/insert_zeros)
format:該聚合的輸出格式定義
{
"aggs": {
"my_date_histo": {
"date_histogram": {
"field": "timestamp",
"interval": "day"
},
"aggs": {
"the_sum": {
"sum": {
"field": "lemmings"
}
},
"thirtieth_difference": {
"serial_diff": {
"buckets_path": "the_sum",
"lag" : 30 //差分間隔為 30 day
}
}
}
}
}
}
正因為當(dāng)初對未來做了太多的憧憬,所以對現(xiàn)在的自己尤其失望。生命中曾經(jīng)有過的所有燦爛,終究都需要用寂寞來償還。
總結(jié)
以上是生活随笔為你收集整理的ES aggregation详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个高质量的程序应具备哪些条件?_如何开
- 下一篇: android 一个字符串分两行显示_重