Weighted_avg

Weighted average aggregation

/ Weighted_avg, / Aggregation

Weighted average aggregation #

A single-value metrics aggregation that computes the weighted average of numeric values. Each document contributes value × weight, divided by the total weight — e.g. an average price weighted by quantity sold. When documents are not all equally important, avg is the wrong tool and this is the right one.

How it works #

The engine walks the matching documents once, accumulating Σ(value × weight) in the numerator and Σ weight in the denominator; the result is their quotient. Both sides are computed in the same pass over two columns, so the pairing of a document’s value with its own weight is never lost.

...