最大值聚合
编辑最大值聚合编辑
一种 单值
指标聚合,用于跟踪并返回从聚合文档中提取的数值中的最大值。
min
和 max
聚合对数据的 double
表示形式进行操作。因此,当对绝对值大于 2^53
的长整型进行运算时,结果可能近似。
计算所有文档中的最高价格值
response = client.search( index: 'sales', size: 0, body: { aggregations: { max_price: { max: { field: 'price' } } } } ) puts response
POST /sales/_search?size=0 { "aggs": { "max_price": { "max": { "field": "price" } } } }
响应
{ ... "aggregations": { "max_price": { "value": 200.0 } } }
可以看出,聚合的名称(上面的 max_price
)也用作从返回的响应中检索聚合结果的键。
脚本编辑
如果需要获取比单个字段更复杂的 max
,请对 运行时字段 运行聚合。
response = client.search( index: 'sales', body: { size: 0, runtime_mappings: { 'price.adjusted' => { type: 'double', script: "\n double price = doc['price'].value;\n if (doc['promoted'].value) {\n price *= 0.8;\n }\n emit(price);\n " } }, aggregations: { max_price: { max: { field: 'price.adjusted' } } } } ) puts response
POST /sales/_search { "size": 0, "runtime_mappings": { "price.adjusted": { "type": "double", "script": """ double price = doc['price'].value; if (doc['promoted'].value) { price *= 0.8; } emit(price); """ } }, "aggs": { "max_price": { "max": { "field": "price.adjusted" } } } }
缺失值编辑
missing
参数定义了如何处理缺少值的文档。默认情况下,它们将被忽略,但也可以将它们视为具有值。
response = client.search( index: 'sales', body: { aggregations: { grade_max: { max: { field: 'grade', missing: 10 } } } } ) puts response
直方图字段编辑
当对 直方图字段 计算 max
时,聚合的结果是 values
数组中所有元素的最大值。请注意,直方图的 counts
数组将被忽略。
例如,对于以下存储了不同网络的延迟指标的预聚合直方图的索引
response = client.indices.create( index: 'metrics_index', body: { mappings: { properties: { latency_histo: { type: 'histogram' } } } } ) puts response response = client.index( index: 'metrics_index', id: 1, refresh: true, body: { 'network.name' => 'net-1', latency_histo: { values: [ 0.1, 0.2, 0.3, 0.4, 0.5 ], counts: [ 3, 7, 23, 12, 6 ] } } ) puts response response = client.index( index: 'metrics_index', id: 2, refresh: true, body: { 'network.name' => 'net-2', latency_histo: { values: [ 0.1, 0.2, 0.3, 0.4, 0.5 ], counts: [ 8, 17, 8, 7, 6 ] } } ) puts response response = client.search( index: 'metrics_index', size: 0, filter_path: 'aggregations', body: { aggregations: { max_latency: { max: { field: 'latency_histo' } } } } ) puts response
PUT metrics_index { "mappings": { "properties": { "latency_histo": { "type": "histogram" } } } } PUT metrics_index/_doc/1?refresh { "network.name" : "net-1", "latency_histo" : { "values" : [0.1, 0.2, 0.3, 0.4, 0.5], "counts" : [3, 7, 23, 12, 6] } } PUT metrics_index/_doc/2?refresh { "network.name" : "net-2", "latency_histo" : { "values" : [0.1, 0.2, 0.3, 0.4, 0.5], "counts" : [8, 17, 8, 7, 6] } } POST /metrics_index/_search?size=0&filter_path=aggregations { "aggs" : { "max_latency" : { "max" : { "field" : "latency_histo" } } } }
max
聚合将返回所有直方图字段的最大值
{ "aggregations": { "max_latency": { "value": 0.5 } } }