Reference Entry
Histogram Aggregation
Discovery · advanced · order 28
Bucket numeric fields into regular fixed-width intervals for charts and sidebar facets.
Relevant APIs
Histogram Aggregation
histogram(interval) groups numeric values into regular fixed-width buckets.
Basic usage
const priceIndex = index.getFieldIndex("price") as NumericFieldIndex;
const buckets = priceIndex.histogram(10);
Expected shape:
[
{ key: 0, docCount: 4 },
{ key: 10, docCount: 9 },
{ key: 20, docCount: 3 }
]
Each key is the start of the bucket.
How it works
With an interval of 10:
- values from
0through9.999...land in bucket0 - values from
10through19.999...land in bucket10
Buckets use document-count semantics, so a document contributes once per bucket even if multiple values fall inside the same bucket.
const subsetBuckets = priceIndex.histogram(10, new Set(["a", "b"]));
When to use it
- price charts
- popularity distributions
- article length histograms
Tradeoffs
- Only fixed-width intervals are supported.
- Interval must be a finite number greater than zero.