Skip to main content
Aggregate stage showing statistical computations on search results
The Aggregate stage computes statistical aggregations across your search results, including counts, sums, averages, min/max values, and custom metrics. This is useful for analytics, faceted search, and understanding result distributions.
Stage Category: REDUCE (Aggregates results)Transformation: N documents → aggregation results + optional documents
Aggregating a whole collection? Use POST /v1/collections/{collection_identifier}/documents/aggregate. It runs the same functions over every matching document, not just the current pipeline’s results. It returns exact counts at any collection size, and count, count_distinct, sum, avg, min and max stream without a row cap. Pair it with the is_null filter operator for field-presence checks, such as counting documents missing a from_collection ancestor across 180k+ assets.The two request bodies share group_by (an array of objects), function and alias. The endpoint adds filters, having, unwind and range_buckets, and calls its sort direction sort_direction where this stage uses sort_order.aggregations is required, with at least one entry. Every entry needs function and alias. field is required for every function except count.
Read the results from the stage statistics. This stage consumes the documents and, unless include_documents is true, emits none. The groups and metrics are in stage_statistics.stages.<your stage_name>.metadata.aggregations. A response with an empty documents array and a null top-level facets is what a working aggregate stage returns. Top-level facets is filled only by the feature_search stage’s own facets option. See Output.

When to Use

When NOT to Use

Parameters

Each entry in aggregations: A field path resolves against the document the way the Group By stage does. score and document_id are read directly. A bare name such as client_id is looked up on the document, then in metadata. A dotted path such as metadata.price walks the document.

Aggregation Functions

Configuration Examples

Output

The groups and metrics come back in the stage’s statistics:
stages is keyed by the stage_name you gave the stage. output_count is 0 when include_documents is false, because the stage consumes the documents. With include_documents: true it equals input_count, and the documents continue to the next stage unchanged. Without group_by there is one entry and group is null:

Performance

Common Pipeline Patterns

Search with Facets

Analytics Pipeline

E-Commerce Product Analytics

Aggregation Details

Count

Counts documents in the group. No field required.

Sum / Avg / Min / Max

Reads numeric values only. A document whose value is missing or not a number is skipped. With no numeric values the metric is null.

Count Distinct

Counts unique values exactly. A list-valued field contributes each element.

Percentile

Returns one percentile per entry, interpolated between neighbouring values. Add one entry per percentile you want.

Statistical Aggregation Examples

Statistical Output Examples

Frequency returns value counts with percentages:
Co-occurrence returns field pair counts:
Correlation returns the Pearson coefficient (-1 to 1):

Error Handling