Querylight TS Demo

Sum Aggregation

Add up all indexed numeric or date values in a field, optionally within a filtered subset.

Back to docs search

Reference Entry

Sum Aggregation

Discovery · advanced · order 24

Add up all indexed numeric or date values in a field, optionally within a filtered subset.

Sum Aggregation

sum() adds all indexed values in a numeric or date field.

Basic usage

const popularityIndex = index.getFieldIndex("popularity") as NumericFieldIndex;
const totalPopularity = popularityIndex.sum();

How it works

sum() aggregates values, not documents.

If a document has multiple values, each value contributes to the total.

const totalForSubset = popularityIndex.sum(new Set(["a", "b"]));

When to use it

  • total score-like metadata
  • cumulative counters
  • building your own derived metrics from sum() and valueCount()

Tradeoffs

  • This is usually more useful for numeric fields than for dates.
  • Multi-valued documents contribute multiple times.