Elasticsearch Min Aggregation - Syntax, Example, and Tips

Pulse - Elasticsearch Operations Done Right

On this page

Syntax Example Usage Common Issues Best Practices Frequently Asked Questions

The Min Aggregation (min) is a single-value metrics aggregation that computes the minimum value of numeric values extracted from the aggregated documents.

Syntax

{
  "aggs": {
    "min_price": {
      "min": {
        "field": "price"
      }
    }
  }
}

For more details, refer to the official Elasticsearch documentation on Min Aggregation.

Example Usage

Here's an example of using the Min Aggregation to find the lowest price in a product catalog:

GET /products/_search
{
  "size": 0,
  "aggs": {
    "lowest_price": {
      "min": {
        "field": "price"
      }
    }
  }
}

This query will return the minimum price across all documents in the "products" index.

Common Issues

  1. Missing field: If the specified field doesn't exist in any document, the aggregation will return null.
  2. Non-numeric data: The Min Aggregation only works on numeric fields. Using it on non-numeric fields will result in an error.
  3. Scaled float: When using scaled_float data type, be aware that the min value is calculated on the scaled value, not the original one.

Best Practices

  1. Use the missing parameter to specify a default value for documents where the field is missing.
  2. Consider using script when you need to perform calculations before finding the minimum.
  3. For better performance on large datasets, use the filter aggregation to reduce the number of documents before applying the Min Aggregation.

Frequently Asked Questions

Q: Can I use Min Aggregation on date fields?
A: Yes, Min Aggregation can be used on date fields. It will return the earliest date in the specified field.

Q: How does Min Aggregation handle null values?
A: By default, Min Aggregation ignores null values. You can use the missing parameter to specify a value to use for documents that are missing the field.

Q: Can I use Min Aggregation with nested objects?
A: Yes, you can use Min Aggregation with nested objects by combining it with a Nested Aggregation.

Q: Is it possible to get the document that contains the minimum value?
A: The Min Aggregation itself doesn't return the document. However, you can use Top Hits Aggregation in combination with Min Aggregation to achieve this.

Q: How does Min Aggregation perform on large datasets?
A: Min Aggregation is generally fast as it only needs to keep track of a single value. However, for very large datasets, consider using sampling or filtering to improve performance.

Subscribe to the Pulse Newsletter

Get early access to new Pulse features, insightful blogs & exclusive events , webinars, and workshops.