Elasticsearch Min Bucket 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 Bucket Aggregation is a sibling pipeline aggregation that identifies the bucket(s) with the minimum value of a specified metric in a sibling aggregation and outputs both the value and the key(s) of the bucket(s).

Syntax

The basic syntax for a Min Bucket Aggregation is:

{
  "min_bucket": {
    "buckets_path": "path_to_metric"
  }
}

For more detailed information, refer to the official Elasticsearch documentation on Min Bucket Aggregation.

Example Usage

Here's an example of using Min Bucket Aggregation to find the month with the lowest total sales:

{
  "aggs": {
    "sales_per_month": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "month"
      },
      "aggs": {
        "total_sales": {
          "sum": {
            "field": "price"
          }
        }
      }
    },
    "min_monthly_sales": {
      "min_bucket": {
        "buckets_path": "sales_per_month>total_sales"
      }
    }
  }
}

Common Issues

  1. Incorrect buckets_path: Ensure the buckets_path is correctly specified and points to an existing metric in a sibling aggregation.
  2. Empty buckets: Min Bucket Aggregation ignores empty buckets. If all buckets are empty, the result will be null.
  3. Non-numeric values: The aggregation works only with numeric values. Ensure the metric being referenced returns numeric data.

Best Practices

  1. Use Min Bucket Aggregation in combination with other aggregations to gain insights into your data, such as identifying periods of lowest activity or performance.
  2. Consider using gap_policy parameter to handle missing data points if necessary.
  3. When working with date histograms, be mindful of time zones and date formats to ensure accurate results.

Frequently Asked Questions

Q: Can Min Bucket Aggregation work with nested aggregations?
A: Yes, Min Bucket Aggregation can work with nested aggregations. Use the > character in the buckets_path to navigate through nested aggregations.

Q: How does Min Bucket Aggregation handle ties?
A: If multiple buckets have the same minimum value, Min Bucket Aggregation will return all of them.

Q: Can I use Min Bucket Aggregation with non-numeric fields?
A: No, Min Bucket Aggregation works only with numeric values. Ensure the metric you're referencing returns numeric data.

Q: Is it possible to exclude certain buckets from Min Bucket Aggregation?
A: There's no direct way to exclude specific buckets. However, you can use a bucket selector aggregation before the min bucket to filter out unwanted buckets.

Q: How does Min Bucket Aggregation perform with large datasets?
A: Min Bucket Aggregation is generally efficient as it operates on already aggregated data. However, performance may be affected if there are a large number of buckets to compare.

Subscribe to the Pulse Newsletter

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