﻿---
title: Metric aggregation combine context
description: Use a Painless script to combine values for use in a scripted metric aggregation. A combine script is run once per shard following a map script and is...
url: https://www.elastic.co/elastic/docs-builder/docs/2452/reference/scripting-languages/painless/painless-metric-agg-combine-context
products:
  - Painless
---

# Metric aggregation combine context
Use a Painless script to [combine](https://www.elastic.co/elastic/docs-builder/docs/2452/reference/aggregations/search-aggregations-metrics-scripted-metric-aggregation) values for use in a scripted metric aggregation. A combine script is run once per shard following a [map script](https://www.elastic.co/elastic/docs-builder/docs/2452/reference/scripting-languages/painless/painless-metric-agg-map-context) 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="state (Map)">
    `Map` with values available from the prior map script.
  </definition>
</definitions>

**Return**
<definitions>
  <definition term="List, Map, String, or primitive">
    A value collected for use in a [reduce script](https://www.elastic.co/elastic/docs-builder/docs/2452/reference/scripting-languages/painless/painless-metric-agg-reduce-context). If no reduce script is specified, the value is used as part of 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.