> 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".


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.hyper-space.io/hyperspace-docs/api-documentation/dsl-query-framework/aggregations/metric-aggregations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
