PrepZone Logo
PrepZone

Elasticsearch Aggregations

Bucket and metric aggregations for faceted search and sales dashboards.

Read these first

Why this matters

  • VaultCommerce faceted browse ("outdoor, $50–$100, 4+ stars") needs category and price histograms without loading every product into the app server.
  • Aggregations on analyzed text fields fail — facet on keyword fields only.
  • Heavy aggregations on high-cardinality fields (SKU) stress data nodes — design dashboards with rollups or separate indices.
Cluster
Node 1Shard 0 primary
Node 2Shard 1 primary
Node 3Shard 0 replica
An index is split into shards across nodes. Replicas provide failover and read scaling.

Metric aggregations

Java
GET /vaultcommerce-products/_search
{
  "size": 0,
  "aggs": {
    "avg_price": { "avg": { "field": "price" } },
    "price_stats": { "stats": { "field": "price" } },
    "categories": { "cardinality": { "field": "category" } }
  }
}

size: 0 returns only aggregation results — standard for dashboard APIs that do not need individual hits.

Bucket aggregations for facets

Java
GET /vaultcommerce-products/_search
{
  "size": 10,
  "query": { "match": { "title": "pack" } },
  "aggs": {
    "by_category": {
      "terms": { "field": "category", "size": 20 }
    },
    "price_ranges": {
      "range": {
        "field": "price",
        "ranges": [
          { "to": 50 },
          { "from": 50, "to": 100 },
          { "from": 100 }
        ]
      }
    }
  }
}

VaultCommerce renders facet counts from by_category and price-band filters from price_ranges on the search results page.

Aggregation families

  • Bucket — group documents (terms, range, date_histogram).
  • Metric — compute values (avg, sum, stats, cardinality).
  • Pipeline — aggregate on other aggregation outputs (avg_bucket).
  • Nested — aggregate inside nested object arrays (product variants).

Sub-aggregations

Java
GET /vaultcommerce-orders/_search
{
  "size": 0,
  "aggs": {
    "sales_by_category": {
      "terms": { "field": "category" },
      "aggs": {
        "total_revenue": { "sum": { "field": "line_total" } },
        "avg_order_value": { "avg": { "field": "line_total" } }
      }
    }
  }
}

Parent bucket groups by category; child metrics sum revenue per group — merchandising's weekly report in one query.

Composite pagination for high cardinality

Java
GET /vaultcommerce-products/_search
{
  "size": 0,
  "aggs": {
    "sku_pages": {
      "composite": {
        "size": 100,
        "sources": [{ "sku": { "terms": { "field": "sku" } } }]
      }
    }
  }
}

Use composite with after_key to paginate through millions of unique SKUs without the memory blow-up of a single giant terms agg.

Quick recall

Everything you need if you only revisit this box.

  • Aggregations = buckets (group) + metrics (compute) + pipelines (meta).
  • Set "size": 0 when you only need analytics, not hits.
  • Facet on keyword fields — never on analyzed text.
  • Sub-aggregations nest metrics inside bucket groups.
  • composite paginates high-cardinality terms safely.
  • VaultCommerce facets category and price range on search result pages.

Test yourself

Answer these before moving on — recall is what makes it stick.