指标聚合 #
指标聚合让您可以执行简单的计算,例如查找字段的最小值、最大值和平均值。
指标聚合的类型 #
指标聚合有两种类型:单值指标聚合和多值指标聚合。
单值指标聚合 #
单值 指标聚合返回单个指标。 例如,sum
、 min
、 max
、 avg
、 cardinality
和 value_count
。
多值指标聚合 #
多值 指标聚合返回多个指标。 例如, stats
、 extended_stats
、 matrix_stats
、 percentile
、 percentile_ranks
、 geo_bound
、 top_hits
和 scripted_metric
。
sum, min, max, avg #
sum
、 min
、 max
和 avg
metric 是单值 指标聚合,分别返回字段的总和、最小值、最大值和平均值。
以下示例计算 taxful_total_price
字段的总和:
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"sum_taxful_total_price": {
"sum": {
"field": "taxful_total_price"
}
}
}
}
返回示例 #
...
"aggregations" : {
"sum_taxful_total_price" : {
"value" : 350884.12890625
}
}
...
以类似的方式,您可以找到字段的最小值、最大值和平均值。
cardinality(基数) #
cardinality
metric 是一种单值 指标聚合,用于计算字段的唯一或不同值的数量。
以下示例查找电子商务商店中唯一产品的数量:
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"unique_products": {
"cardinality": {
"field": "products.product_id"
}
}
}
}
返回示例 #
...
"aggregations" : {
"unique_products" : {
"value" : 7033
}
}
...
cardinality 计数统计是近似值。 如果您的商店中有数万种产品,则准确的基数计算需要将所有值加载到哈希集中并返回其大小。 这种方法不能很好地扩展,因为它需要更多内存并导致高延迟。
您可以使用 precision_threshold
设置来控制内存和准确性之间的权衡。 此设置定义了阈值,低于该阈值的计数预计接近准确。 高于此值,计数可能会变得不太准确。 precision_threshold
的默认值为 3,000。支持的最大值为 40,000。
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"unique_products": {
"cardinality": {
"field": "products.product_id",
"precision_threshold": 10000
}
}
}
}
value_count #
value_count
metric 是一种单值 指标聚合,用于计算聚合所基于的值的数量。
例如,您可以将 value_count
指标与 avg
指标结合使用,以确定聚合使用多少数字来计算平均值。
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"number_of_values": {
"value_count": {
"field": "taxful_total_price"
}
}
}
}
返回示例 #
...
"aggregations" : {
"number_of_values" : {
"value" : 4675
}
}
...
stats, extended_stats, matrix_stats #
stats
metric 是一种多值 指标聚合,可在一个聚合查询中返回所有基本指标,例如 min
、 max
、 sum
、 avg
和 value_count
。
以下示例返回 taxful_total_price
字段的基本统计数据:
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"stats_taxful_total_price": {
"stats": {
"field": "taxful_total_price"
}
}
}
}
返回示例 #
...
"aggregations" : {
"stats_taxful_total_price" : {
"count" : 4675,
"min" : 6.98828125,
"max" : 2250.0,
"avg" : 75.05542864304813,
"sum" : 350884.12890625
}
}
...
extended_stats
聚合是 stats
聚合的扩展版本。 除了包括基本统计数据外,extended_stats
还返回统计数据,例如 sum_of_squares
、 variance
和 std_deviation
。
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"extended_stats_taxful_total_price": {
"extended_stats": {
"field": "taxful_total_price"
}
}
}
}
返回示例 #
...
"aggregations" : {
"extended_stats_taxful_total_price" : {
"count" : 4675,
"min" : 6.98828125,
"max" : 2250.0,
"avg" : 75.05542864304813,
"sum" : 350884.12890625,
"sum_of_squares" : 3.9367749294174194E7,
"variance" : 2787.59157113862,
"variance_population" : 2787.59157113862,
"variance_sampling" : 2788.187974983536,
"std_deviation" : 52.79764740155209,
"std_deviation_population" : 52.79764740155209,
"std_deviation_sampling" : 52.80329511482722,
"std_deviation_bounds" : {
"upper" : 180.6507234461523,
"lower" : -30.53986616005605,
"upper_population" : 180.6507234461523,
"lower_population" : -30.53986616005605,
"upper_sampling" : 180.66201887270256,
"lower_sampling" : -30.551161586606312
}
}
}
...
std_deviation_bounds
对象提供数据的视觉方差,间隔为正/负两个标准差的平均值。
要将标准偏差设置为不同的值,比如 3,请将 sigma
设置为 3:
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"extended_stats_taxful_total_price": {
"extended_stats": {
"field": "taxful_total_price",
"sigma": 3
}
}
}
}
matrix_stats
聚合以矩阵形式为多个字段生成高级统计信息。
以下示例以矩阵形式返回 taxful_total_price
和 products.base_price
字段的高级统计信息:
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"matrix_stats_taxful_total_price": {
"matrix_stats": {
"fields": ["taxful_total_price", "products.base_price"]
}
}
}
}
返回示例 #
...
"aggregations" : {
"matrix_stats_taxful_total_price" : {
"doc_count" : 4675,
"fields" : [
{
"name" : "products.base_price",
"count" : 4675,
"mean" : 34.994239430147196,
"variance" : 360.5035285833703,
"skewness" : 5.530161335032702,
"kurtosis" : 131.16306324042148,
"covariance" : {
"products.base_price" : 360.5035285833703,
"taxful_total_price" : 846.6489362233166
},
"correlation" : {
"products.base_price" : 1.0,
"taxful_total_price" : 0.8444765264325268
}
},
{
"name" : "taxful_total_price",
"count" : 4675,
"mean" : 75.05542864304839,
"variance" : 2788.1879749835402,
"skewness" : 15.812149139924037,
"kurtosis" : 619.1235507385902,
"covariance" : {
"products.base_price" : 846.6489362233166,
"taxful_total_price" : 2788.1879749835402
},
"correlation" : {
"products.base_price" : 0.8444765264325268,
"taxful_total_price" : 1.0
}
}
]
}
}
...
统计 | 描述 |
---|---|
count | 测量的样本数。 |
mean | 从样本中测得的字段的平均值。 |
variance | 测量的字段值与其平均值相差多远。 方差越大,它离均值的分布就越大。 |
skewness | 字段值围绕均值分布的不对称度量。 |
kurtosis | 分布尾重的度量。 随着尾部变轻,峰度降低。 随着尾巴变重,峰度增加。 要了解峰度,请参阅 维基百科。 |
covariance | 两个字段之间的联合可变性的度量。 正值表示它们的值朝相同的方向移动,反之亦然。 |
correlation | 衡量两个领域之间关系强度的指标。 有效值介于 [-1, 1] 之间。 值为 -1 表示该值负相关,值为 1 表示它正相关。 值为 0 表示它们之间没有可识别的关系。 |
percentile, percentile_ranks #
百分比是等于或低于特定阈值的数据的百分比。
percentile
指标是一种多值指标聚合,可让您找到数据中的异常值或计算出数据的分布。
与 cardinality
指标一样,percentile
指标也是近似值。
以下示例计算与 taxful_total_price
字段相关的百分位数:
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"percentile_taxful_total_price": {
"percentiles": {
"field": "taxful_total_price"
}
}
}
}
返回示例 #
...
"aggregations" : {
"percentile_taxful_total_price" : {
"values" : {
"1.0" : 21.984375,
"5.0" : 27.984375,
"25.0" : 44.96875,
"50.0" : 64.22061688311689,
"75.0" : 93.0,
"95.0" : 156.0,
"99.0" : 222.0
}
}
}
...
百分位数排名是等于或低于按指定值分组的阈值的值的百分位数。 例如,如果一个值大于或等于值的 80%,则它的百分位排名为 80。
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"percentile_rank_taxful_total_price": {
"percentile_ranks": {
"field": "taxful_total_price",
"values": [
10,
15
]
}
}
}
}
返回示例 #
...
"aggregations" : {
"percentile_rank_taxful_total_price" : {
"values" : {
"10.0" : 0.055096056411283456,
"15.0" : 0.0830092961834656
}
}
}
...
geo_bound #
geo_bound
指标是一个多值指标聚合,根据 geo_point
字段周围的纬度和经度计算边界框。
以下示例返回 geoip.location
字段的 geo_bound
指标:
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"geo": {
"geo_bounds": {
"field": "geoip.location"
}
}
}
}
返回示例 #
...
"aggregations" : {
"geo" : {
"bounds" : {
"top_left" : {
"lat" : 52.49999997206032,
"lon" : -118.20000001229346
},
"bottom_right" : {
"lat" : 4.599999985657632,
"lon" : 55.299999956041574
}
}
}
}
...
top_hits #
top_hits
metric 是一种多值 指标聚合,它根据正在聚合的字段的相关性得分对匹配文档进行排名。
您可以指定以下选项:
from
: 命中的起始位置。size
: 返回的最大命中大小。默认值为 3。sort
: 匹配命中的排序方式。默认情况下,命中按聚合查询的相关性分数排序
以下示例返回电子商务数据中的前 5 个产品:
GET kibana_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"top_hits_products": {
"top_hits": {
"size": 5
}
}
}
}
返回示例 #
...
"aggregations" : {
"top_hits_products" : {
"hits" : {
"total" : {
"value" : 4675,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "kibana_sample_data_ecommerce",
"_type" : "_doc",
"_id" : "glMlwXcBQVLeQPrkHPtI",
"_score" : 1.0,
"_source" : {
"category" : [
"Women's Accessories",
"Women's Clothing"
],
"currency" : "EUR",
"customer_first_name" : "rania",
"customer_full_name" : "rania Evans",
"customer_gender" : "FEMALE",
"customer_id" : 24,
"customer_last_name" : "Evans",
"customer_phone" : "",
"day_of_week" : "Sunday",
"day_of_week_i" : 6,
"email" : "rania@evans-family.zzz",
"manufacturer" : [
"Tigress Enterprises"
],
"order_date" : "2021-02-28T14:16:48+00:00",
"order_id" : 583581,
"products" : [
{
"base_price" : 10.99,
"discount_percentage" : 0,
"quantity" : 1,
"manufacturer" : "Tigress Enterprises",
"tax_amount" : 0,
"product_id" : 19024,
"category" : "Women's Accessories",
"sku" : "ZO0082400824",
"taxless_price" : 10.99,
"unit_discount_amount" : 0,
"min_price" : 5.17,
"_id" : "sold_product_583581_19024",
"discount_amount" : 0,
"created_on" : "2016-12-25T14:16:48+00:00",
"product_name" : "Snood - white/grey/peach",
"price" : 10.99,
"taxful_price" : 10.99,
"base_unit_price" : 10.99
},
{
"base_price" : 32.99,
"discount_percentage" : 0,
"quantity" : 1,
"manufacturer" : "Tigress Enterprises",
"tax_amount" : 0,
"product_id" : 19260,
"category" : "Women's Clothing",
"sku" : "ZO0071900719",
"taxless_price" : 32.99,
"unit_discount_amount" : 0,
"min_price" : 17.15,
"_id" : "sold_product_583581_19260",
"discount_amount" : 0,
"created_on" : "2016-12-25T14:16:48+00:00",
"product_name" : "Cardigan - grey",
"price" : 32.99,
"taxful_price" : 32.99,
"base_unit_price" : 32.99
}
],
"sku" : [
"ZO0082400824",
"ZO0071900719"
],
"taxful_total_price" : 43.98,
"taxless_total_price" : 43.98,
"total_quantity" : 2,
"total_unique_products" : 2,
"type" : "order",
"user" : "rani",
"geoip" : {
"country_iso_code" : "EG",
"location" : {
"lon" : 31.3,
"lat" : 30.1
},
"region_name" : "Cairo Governorate",
"continent_name" : "Africa",
"city_name" : "Cairo"
},
"event" : {
"dataset" : "sample_ecommerce"
}
}
...
}
]
}
}
}
...
scripted_metric #
The scripted_metric
metric 是一个多值 指标聚合,返回根据指定脚本计算的指标。
脚本有四个阶段:初始阶段、映射阶段、合并阶段和归约阶段。
init_script
: (可选)设置初始状态并在任何文档集合之前执行。map_script
: 检查type
字段的值,并对收集的文档执行聚合。combine_script
: 聚合从每个分片返回的状态。聚合值将返回到协调节点。reduce_script
: 提供对变量状态的访问;此变量将每个分片上的combine_script
的结果组合到一个数组中。
以下示例聚合了 Web 日志数据中的不同 HTTP 响应类型:
GET kibana_sample_data_logs/_search
{
"size": 0,
"aggregations": {
"responses.counts": {
"scripted_metric": {
"init_script": "state.responses = ['error':0L,'success':0L,'other':0L]",
"map_script": """
def code = doc['response.keyword'].value;
if (code.startsWith('5') || code.startsWith('4')) {
state.responses.error += 1 ;
} else if(code.startsWith('2')) {
state.responses.success += 1;
} else {
state.responses.other += 1;
}
""",
"combine_script": "state.responses",
"reduce_script": """
def counts = ['error': 0L, 'success': 0L, 'other': 0L];
for (responses in states) {
counts.error += responses['error'];
counts.success += responses['success'];
counts.other += responses['other'];
}
return counts;
"""
}
}
}
}
返回示例 #
...
"aggregations" : {
"responses.counts" : {
"value" : {
"other" : 0,
"success" : 12832,
"error" : 1242
}
}
}
...