---
title: Metric aggregation initialization context
description: Use a Painless script to initialize values for use in a scripted metric aggregation. An initialization script is run prior to document collection once...
url: https://www.elastic.co/elastic/docs-builder/docs/2341/reference/scripting-languages/painless/painless-metric-agg-init-context
products:
  - Painless
---

# Metric aggregation initialization context
Use a Painless script to [initialize](https://www.elastic.co/elastic/docs-builder/docs/2341/reference/aggregations/search-aggregations-metrics-scripted-metric-aggregation) values for use in a scripted metric aggregation. An initialization script is run prior to document collection once per shard and is optional as part of the 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)">
    Empty `Map` used to add values for use in a [map script](https://www.elastic.co/elastic/docs-builder/docs/2341/reference/scripting-languages/painless/painless-metric-agg-map-context).
  </definition>
</definitions>

**Side Effects**
<definitions>
  <definition term="state (Map)">
    Add values to this `Map` to for use in a map. Additional values must be of the type `Map`, `List`, `String` or primitive.
  </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.