Variable_width_histogram

Variable width histogram aggregation

/ Variable_width_histogram, / Aggregation

Variable width histogram aggregation #

A multi-bucket aggregation like histogram, but bucket edges adapt to the data so each bucket holds roughly the same number of documents. Fixed grids go blind where data is sparse and waste resolution where it is dense; the variable-width form spends its buckets where the documents actually are.

How it works #

The engine collects every numeric value, sorts it, and partitions the sorted run into buckets contiguous segments such that the sum of squared distances to each segment’s mean is minimal — the classic one-dimensional k-segmentation problem, solved exactly with dynamic programming and prefix sums (an O(1) cost function per candidate cut). When n × k exceeds 1,048,576 it falls back to equal-count chunking, a linear-time approximation. buckets is capped at the number of distinct values, so asking for more buckets than there are values simply yields one bucket per value.

...