liveBook · Manning

Metric aggregation

As explained in Elasticsearch in Action, Second Edition

A single-value metric aggregation computes exactly one numeric value from a set of input documents.

A metric aggregation calculates a numerical summary across a set of Elasticsearch documents. Use it when individual search hits are less important than a question about the whole result set: total sales, highest deaths, lowest error count, or average rating. Common metric functions include sum, min, max, and avg. section 13.1 section 13.2

A single-value metric aggregation produces exactly one value from its input documents. For example, min, max, and avg each return one result. This differs from stats, which returns several values—such as count, min, max, average, and sum—for the same field. section 13.2 section 2.6.1

Total critical patients

Use the _search endpoint and put aggs at the request root. Give the aggregation a report name, choose sum, and name the numeric field. The response places the computed value under aggregations. Set size to 0 when the report does not need the matching source documents returned alongside it. section 2.6.1

Listing 2.26 Fetching the total number of critical patients
GET covid/_search
{
  "aggs": {                   #1
    "critical_patients": {    #2
      "sum": {                #3
        "field": "critical"   #4
      }
    }
  }
}

This request sums the critical field across COVID documents. The critical_patients name becomes the response key, while sum selects the calculation and field selects its input. section 2.6.1

The same pattern supports other one-number reports. Replace sum with max to find the largest field value, or use min or avg as needed. When several core statistics are needed together, prefer stats rather than issuing separate basic metric aggregations. section 2.6.1 section 13.2

Listing 2.28 Returning all statistics with the stats metric
GET covid/_search
{
  "size": 0, 
  "aggs": {
    "all_stats": {
      "stats": {            #1
        "field": "deaths"
      }
    }
  }
}
#1 The stats query returns all five core metrics at once.

The stats request shows the multi-value alternative: one field yields count, minimum, maximum, average, and sum rather than a single result. section 2.6.1

Metric aggregations fit into broader analytics work:

  • Combine search and aggregations in one request to calculate a metric for documents selected by search criteria. section 2.6
  • Use bucket aggregations when values must first be grouped into intervals, ranges, or categories. section 2.6
  • Use pipeline aggregations when a calculation must consume the output of metric or bucket aggregations rather than document fields directly. section 13.5

Start with a field whose values answer the operational question, such as critical patients, deaths, sales, or ratings. A metric summarizes the selected document set; it does not replace search when the application needs the individual matching documents. For grouped or chained analysis, build on metrics with buckets or pipelines. section 2.6.1 section 13.6

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines single-value output, demonstrates sum and max requests, and contrasts stats; defers broader aggregation coverage to later detail.
Examples
Uses COVID-country documents to sum critical patients, find maximum deaths, and return death statistics.
What it assumes
Introduces aggregation syntax through Query DSL requests and assumes indexed documents with numeric fields.
What you can do after
Apply it via the Elasticsearch _search API to return one numeric summary for a selected document set.
How it connects
Connects metric aggregations to search, bucket aggregations, stats aggregations, and pipeline aggregations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.