> For the complete documentation index, see [llms.txt](https://docs.hyper-space.io/hyperspace-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hyper-space.io/hyperspace-docs/api-documentation/dsl-query-framework/aggregations/metric-aggregations.md).

# Metric Aggregations

Metric Aggregations allow you to compute and analyze numeric measures on sets of documents. Metric aggregations operate on a numeric field in documents and produce a single numeric result for the specified metric.&#x20;

To create a metric aggregation, follow the following template -&#x20;

{% code lineNumbers="true" %}

```json
{
  "aggs": {
    "agg_name": {
      "metric_type": {
        "fieldname": "numeric_field"
      }
    }
  }
}
```

{% endcode %}

Where -

* <mark style="color:purple;">agg\_name</mark> **-** aggregation results will be saved under this key
* <mark style="color:purple;">metric\_type</mark> **-** the type of aggregation. Possible values are:
  * **sum** - Returns the sum of the field over the relevant candidates
  * **min** - Returns the min of the field over the relevant candidates
  * **max**  - Returns the max of the field over the relevant candidates
  * **avg**- Returns the average of the field over the relevant candidates
  * **count** - Returns the total number of valid field entries in the relevant candidates
  * **cardinality** - Returns the total number of valid field values in the relevant candidates
  * **percentiles** - Returns the percentiles of the field over the relevant candidates.
* <mark style="color:purple;">fieldname</mark> - the name of the field to be used in the aggregation

**Example 1 -**&#x20;

{% code lineNumbers="true" %}

```json
{
  "aggs": {
    "total_sales": {
      "sum": {
        "field": "sales"
      }
    }
  }
}
```

{% endcode %}

In the above example, the sum of the field "sales" will be calculated and stored under a key named "total\_sales"

**Example 2 -**&#x20;

{% code lineNumbers="true" %}

```json
{
  "aggs": {
    "unique_users": {
      "cardinality": {
        "field": "user_id"
      }
    }
  }
}
```

{% endcode %}

In the above example, the number of unique values of the field "user\_id" will be calculated and stored under a key named "unique\_users".
