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

When to Use

Use CaseDescription
Faceted searchCount documents by category
AnalyticsCompute metrics across results
Price rangesMin/max/avg calculations
DistributionsUnderstand result characteristics

When NOT to Use

ScenarioRecommended Alternative
Grouping with full docsgroup_by
Simple countingUse search facets
Per-document statscode_execution
LLM-based analysissummarize

Parameters

ParameterTypeDefaultDescription
aggregationsarrayRequiredList of aggregation operations
group_bystringnoneField to group aggregations by
include_documentsbooleanfalseInclude original documents in output

Aggregation Types

TypeDescriptionExample
countNumber of documentsTotal results
sumSum of field valuesTotal revenue
avgAverage valueAverage price
minMinimum valueLowest price
maxMaximum valueHighest price
cardinalityUnique values countUnique authors
percentilePercentile valuesP50, P95
histogramValue distributionPrice buckets

Configuration Examples

{
  "stage_type": "reduce",
  "stage_id": "aggregate",
  "parameters": {
    "aggregations": [
      {"type": "count", "name": "total"},
      {"type": "avg", "field": "metadata.price", "name": "avg_price"},
      {"type": "min", "field": "metadata.price", "name": "min_price"},
      {"type": "max", "field": "metadata.price", "name": "max_price"}
    ]
  }
}

Output Schema

Without Group By

{
  "aggregations": {
    "total": 150,
    "avg_price": 49.99,
    "min_price": 9.99,
    "max_price": 199.99
  },
  "documents": []  // if include_documents: false
}

With Group By

{
  "aggregations": {
    "electronics": {
      "count": 45,
      "avg_rating": 4.2
    },
    "clothing": {
      "count": 62,
      "avg_rating": 4.5
    },
    "books": {
      "count": 43,
      "avg_rating": 4.7
    }
  }
}

Histogram Output

{
  "aggregations": {
    "price_ranges": {
      "buckets": [
        {"key": "0-50", "count": 45},
        {"key": "50-100", "count": 62},
        {"key": "100-150", "count": 28},
        {"key": "150-200", "count": 15}
      ]
    }
  }
}

Performance

MetricValue
Latency5-50ms
MemoryO(groups × aggregations)
CostFree
ScalabilityEfficient for large result sets

Common Pipeline Patterns

Search with Facets

[
  {
    "stage_type": "filter",
    "stage_id": "semantic_search",
    "parameters": {
      "query": "{{INPUT.query}}",
      "vector_index": "text_extractor_v1_embedding",
      "top_k": 1000
    }
  },
  {
    "stage_type": "reduce",
    "stage_id": "aggregate",
    "parameters": {
      "aggregations": [
        {"type": "count", "name": "total"},
        {"type": "histogram", "field": "metadata.category", "name": "categories"},
        {"type": "histogram", "field": "metadata.price", "interval": 25, "name": "price_ranges"}
      ],
      "include_documents": true
    }
  }
]

Analytics Pipeline

[
  {
    "stage_type": "filter",
    "stage_id": "structured_filter",
    "parameters": {
      "conditions": {
        "AND": [
          {"field": "metadata.date", "operator": "gte", "value": "2024-01-01"},
          {"field": "metadata.status", "operator": "eq", "value": "published"}
        ]
      }
    }
  },
  {
    "stage_type": "reduce",
    "stage_id": "aggregate",
    "parameters": {
      "aggregations": [
        {"type": "count", "name": "total_published"},
        {"type": "sum", "field": "metadata.views", "name": "total_views"},
        {"type": "avg", "field": "metadata.engagement", "name": "avg_engagement"},
        {"type": "percentile", "field": "metadata.views", "percentiles": [50, 90, 99], "name": "view_distribution"}
      ],
      "group_by": "metadata.author"
    }
  }
]

E-Commerce Product Analytics

[
  {
    "stage_type": "filter",
    "stage_id": "hybrid_search",
    "parameters": {
      "query": "{{INPUT.query}}",
      "vector_index": "product_embedding",
      "top_k": 500
    }
  },
  {
    "stage_type": "filter",
    "stage_id": "structured_filter",
    "parameters": {
      "conditions": {
        "field": "metadata.in_stock",
        "operator": "eq",
        "value": true
      }
    }
  },
  {
    "stage_type": "reduce",
    "stage_id": "aggregate",
    "parameters": {
      "aggregations": [
        {"type": "count", "name": "available_products"},
        {"type": "min", "field": "metadata.price", "name": "lowest_price"},
        {"type": "max", "field": "metadata.price", "name": "highest_price"},
        {"type": "avg", "field": "metadata.rating", "name": "avg_rating"},
        {"type": "cardinality", "field": "metadata.brand", "name": "brand_count"}
      ],
      "group_by": "metadata.category",
      "include_documents": true
    }
  }
]

Aggregation Details

Count

{"type": "count", "name": "total"}
Counts documents. No field required.

Sum / Avg / Min / Max

{"type": "avg", "field": "metadata.price", "name": "average_price"}
Requires numeric field.

Cardinality

{"type": "cardinality", "field": "metadata.author", "name": "unique_authors"}
Counts unique values (approximate for large sets).

Percentile

{"type": "percentile", "field": "score", "percentiles": [25, 50, 75, 95], "name": "score_percentiles"}
Returns specified percentile values.

Histogram

{"type": "histogram", "field": "metadata.price", "interval": 50, "name": "price_buckets"}
Groups values into buckets.

Error Handling

ErrorBehavior
Missing fieldSkip document for that aggregation
Non-numeric fieldError for numeric aggregations
Empty resultsReturn zero/empty aggregations
Invalid typeStage fails