﻿---
title: Metric aggregation reduce context
description: Use a Painless script to reduce values to produce the result of a scripted metric aggregation. A reduce script is run once on the coordinating node following...
url: https://www.elastic.co/elastic/docs-builder/docs/2509/reference/scripting-languages/painless/painless-metric-agg-reduce-context
products:
  - Painless
---

# Metric aggregation reduce context
Use a Painless script to [reduce](https://www.elastic.co/elastic/docs-builder/docs/2509/reference/aggregations/search-aggregations-metrics-scripted-metric-aggregation) values to produce the result of a scripted metric aggregation. A reduce script is run once on the coordinating node following a [combine script](https://www.elastic.co/elastic/docs-builder/docs/2509/reference/scripting-languages/painless/painless-metric-agg-combine-context) (or a [map script](https://www.elastic.co/elastic/docs-builder/docs/2509/reference/scripting-languages/painless/painless-metric-agg-map-context) if no combine script is specified) and is optional as part of a full metric aggregation.
**Variables**
<definitions>
  <definition term="params (Map, read-only)">
    User-defined parameters passed in as part of the query.
  </definition>
  <definition term="states (Map)">
    `Map` with values available from the prior combine script (or a map script if no combine script is specified).
  </definition>
</definitions>

**Return**
<definitions>
  <definition term="List, Map, String, or primitive">
    A value used as the result.
  </definition>
</definitions>

**API**
The standard [Painless API](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference-shared.html) is available.