最小值聚合

编辑

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

minmax 聚合操作的是数据的 double 表示形式。因此,当在绝对值大于 2^53 的 long 类型数据上运行时,结果可能只是近似值。

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

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

响应

{
  ...

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

可以看出,聚合的名称(上面的 min_price)也充当从返回的响应中检索聚合结果的键。

脚本

编辑

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

resp = client.search(
    index="sales",
    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      "
        }
    },
    aggs={
        "min_price": {
            "min": {
                "field": "price.adjusted"
            }
        }
    },
)
print(resp)
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
const response = await client.search({
  index: "sales",
  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      ",
    },
  },
  aggs: {
    min_price: {
      min: {
        field: "price.adjusted",
      },
    },
  },
});
console.log(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 参数定义了应如何处理缺少值的文档。默认情况下,它们将被忽略,但也可以将它们视为具有某个值。

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

grade 字段中没有值的文档将与值等于 10 的文档归入同一个存储桶。

直方图字段

编辑

当在直方图字段上计算 min 时,聚合的结果是 values 数组中所有元素的最小值。请注意,直方图的 counts 数组将被忽略。

例如,对于以下索引,它存储了不同网络的延迟指标的预聚合直方图

resp = client.indices.create(
    index="metrics_index",
    mappings={
        "properties": {
            "latency_histo": {
                "type": "histogram"
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="metrics_index",
    id="1",
    refresh=True,
    document={
        "network.name": "net-1",
        "latency_histo": {
            "values": [
                0.1,
                0.2,
                0.3,
                0.4,
                0.5
            ],
            "counts": [
                3,
                7,
                23,
                12,
                6
            ]
        }
    },
)
print(resp1)

resp2 = client.index(
    index="metrics_index",
    id="2",
    refresh=True,
    document={
        "network.name": "net-2",
        "latency_histo": {
            "values": [
                0.1,
                0.2,
                0.3,
                0.4,
                0.5
            ],
            "counts": [
                8,
                17,
                8,
                7,
                6
            ]
        }
    },
)
print(resp2)

resp3 = client.search(
    index="metrics_index",
    size="0",
    filter_path="aggregations",
    aggs={
        "min_latency": {
            "min": {
                "field": "latency_histo"
            }
        }
    },
)
print(resp3)
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
const response = await client.indices.create({
  index: "metrics_index",
  mappings: {
    properties: {
      latency_histo: {
        type: "histogram",
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "metrics_index",
  id: 1,
  refresh: "true",
  document: {
    "network.name": "net-1",
    latency_histo: {
      values: [0.1, 0.2, 0.3, 0.4, 0.5],
      counts: [3, 7, 23, 12, 6],
    },
  },
});
console.log(response1);

const response2 = await client.index({
  index: "metrics_index",
  id: 2,
  refresh: "true",
  document: {
    "network.name": "net-2",
    latency_histo: {
      values: [0.1, 0.2, 0.3, 0.4, 0.5],
      counts: [8, 17, 8, 7, 6],
    },
  },
});
console.log(response2);

const response3 = await client.search({
  index: "metrics_index",
  size: 0,
  filter_path: "aggregations",
  aggs: {
    min_latency: {
      min: {
        field: "latency_histo",
      },
    },
  },
});
console.log(response3);
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
    }
  }
}