最小值聚合编辑

一个 单值 指标聚合,它跟踪并返回从聚合文档中提取的数值中的最小值。

minmax 聚合在数据的 double 表示形式上运行。因此,当对绝对值大于 2^53 的长整数运行时,结果可能近似。

计算所有文档中的最小价格值

response = client.search(
  index: 'sales',
  size: 0,
  body: {
    aggregations: {
      min_price: {
        min: {
          field: 'price'
        }
      }
    }
  }
)
puts response
POST /sales/_search?size=0
{
  "aggs": {
    "min_price": { "min": { "field": "price" } }
  }
}

响应

{
  ...

  "aggregations": {
    "min_price": {
      "value": 10.0
    }
  }
}

如您所见,聚合的名称(上面的 min_price)也用作键,通过该键可以从返回的响应中检索聚合结果。

脚本编辑

如果您需要获取比单个字段更复杂的内容的 min,请在 运行时字段 上运行聚合。

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: {
      min_price: {
        min: {
          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": {
    "min_price": {
      "min": { "field": "price.adjusted" }
    }
  }
}

缺失值编辑

missing 参数定义了如何处理缺少值的文档。默认情况下,它们将被忽略,但也可以将它们视为具有值。

response = client.search(
  index: 'sales',
  body: {
    aggregations: {
      grade_min: {
        min: {
          field: 'grade',
          missing: 10
        }
      }
    }
  }
)
puts response
POST /sales/_search
{
  "aggs": {
    "grade_min": {
      "min": {
        "field": "grade",
        "missing": 10 
      }
    }
  }
}

grade 字段中没有值的文档将与具有值 10 的文档落入同一个桶中。

直方图字段编辑

当在 直方图字段 上计算 min 时,聚合的结果是 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: {
      min_latency: {
        min: {
          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" : {
    "min_latency" : { "min" : { "field" : "latency_histo" } }
  }
}

min 聚合将返回所有直方图字段的最小值

{
  "aggregations": {
    "min_latency": {
      "value": 0.1
    }
  }
}