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

# Metric aggregation map context
Use a Painless script to [map](https://www.elastic.co/elastic/docs-builder/docs/2439/reference/aggregations/search-aggregations-metrics-scripted-metric-aggregation) values for use in a scripted metric aggregation. A map script is run once per collected document following an optional [initialization script](https://www.elastic.co/elastic/docs-builder/docs/2439/reference/scripting-languages/painless/painless-metric-agg-init-context) and is required 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` used to add values for processing in a [combine script](https://www.elastic.co/elastic/docs-builder/docs/2439/reference/scripting-languages/painless/painless-metric-agg-combine-context) or to be returned from the aggregation.
  </definition>
  <definition term="doc (Map, read-only)">
    Contains the fields of the current document where each field is a `List` of values.
  </definition>
  <definition term="_score (double read-only)">
    The similarity score of the current document.
  </definition>
</definitions>

**Side Effects**
<definitions>
  <definition term="state (Map)">
    Use this `Map` to add values for processing in a combine script. Additional values must be of the type `Map`, `List`, `String` or primitive. The same `state` `Map` is shared between all aggregated documents on a given shard. If an initialization script is provided as part of the aggregation then values added from the initialization script are available. If no combine script is specified, values must be directly stored in `state` in a usable form. If no combine script and no [reduce script](https://www.elastic.co/elastic/docs-builder/docs/2439/reference/scripting-languages/painless/painless-metric-agg-reduce-context) are specified, the `state` values are used as the result.
  </definition>
</definitions>

**Return**
<definitions>
  <definition term="void">
    No expected return value.
  </definition>
</definitions>

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