# Scripted\_metric: \_agg variable not defined

**URL:** <https://discuss.elastic.co/t/scripted-metric--agg-variable-not-defined/65802>\
**Category:** Elasticsearch\
**Created:** [November 11, 2016, 12:26pm UTC](https://discuss.elastic.co/t/scripted-metric--agg-variable-not-defined/65802 "2016-11-11T12:26:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![michel-kraemer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michel-kraemer/32/5326_2.png) [@michel-kraemer](https://discuss.elastic.co/u/michel-kraemer)\
**Post date:** [November 11, 2016, 12:26pm UTC](https://discuss.elastic.co/t/scripted-metric--agg-variable-not-defined/65802/1 "2016-11-11T12:26:29Z")

</div>

Hi!

I'm trying to run a `scripted_metric` aggregation but I keep getting the same error. Here is what I do:

```
docker run -it -p 9200:9200 elasticsearch

curl -X PUT http://localhost:9200/index

curl -X POST -d '{
    "query" : {
        "match_all" : {}
    },
    "aggs": {
        "profit": {
            "scripted_metric": {
                "init_script" : "_agg['transactions'] = []",
                "map_script" : "if (doc['type'].value == \"sale\") { _agg.transactions.add(doc['amount'].value) } else { _agg.transactions.add(-1 * doc['amount'].value) }",
                "combine_script" : "profit = 0; for (t in _agg.transactions) { profit += t }; return profit",
                "reduce_script" : "profit = 0; for (a in _aggs) { profit += a }; return profit"
            }
        }
    }
}' "http://localhost:9200/index/type/_search?pretty"

```

I took this aggregation from the documentation ([https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)) but it doesn't matter what I do I always get the following error:

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "_agg[transactions] = []",
          "^---- HERE"
        ],
        "script" : "_agg[transactions] = []",
        "lang" : "painless"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "index",
        "node" : "Nrcqm2e-TxGLI7wYR4kfgw",
        "reason" : {
          "type" : "script_exception",
          "reason" : "compile error",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Variable [_agg] is not defined."
          },
          "script_stack" : [
            "_agg[transactions] = []",
            "^---- HERE"
          ],
          "script" : "_agg[transactions] = []",
          "lang" : "painless"
        }
      }
    ],
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "compile error",
      "caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "Variable [_agg] is not defined."
      },
      "script_stack" : [
        "_agg[transactions] = []",
        "^---- HERE"
      ],
      "script" : "_agg[transactions] = []",
      "lang" : "painless"
    }
  },
  "status" : 500
}

```

Any idea what I'm doing wrong? Thanks for your help!

Cheers,  
Michel

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [November 14, 2016, 12:25pm UTC](https://discuss.elastic.co/t/scripted-metric--agg-variable-not-defined/65802/2 "2016-11-14T12:25:47Z")

</div>

Hi @michel-kraemer,

the default scripting language changed with Elasticsearch 5.0 from Groovy to Painless but unfortunately, the documentation does not reflect this (update: I've raised [#21536](https://github.com/elastic/elasticsearch/issues/21536)). To make it work, you need to specify the scripting language explicitly:

```auto
GET /test/_search
{
   "query": {
      "match_all": {}
   },
   "aggs": {
      "profit": {
         "scripted_metric": {
            "params": {
               "_agg": {}
            },
            "init_script": {
               "lang": "groovy",
               "inline": "_agg['transactions'] = []"
            },
            "map_script": {
               "lang": "groovy",
               "inline": "if (doc['type'].value == \"sale\") { _agg.transactions.add(doc['amount'].value) } else { _agg.transactions.add(-1 * doc['amount'].value) }"
            },
            "combine_script": {
               "lang": "groovy",
               "inline": "profit = 0; for (t in _agg.transactions) { profit += t }; return profit"
            },
            "reduce_script": {
               "lang": "groovy",
               "inline": "profit = 0; for (a in _aggs) { profit += a }; return profit"
            }
         }
      }
   }
}

```

Please also ensure that you allow inline scripts by adding the following line to `conf/elasticsearch.yml`:

```auto
script.inline: true

```

Daniel

---

<div class="post-metadata">

**Author:** ![michel-kraemer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michel-kraemer/32/5326_2.png) [@michel-kraemer](https://discuss.elastic.co/u/michel-kraemer)\
**Post date:** [November 15, 2016, 9:40am UTC](https://discuss.elastic.co/t/scripted-metric--agg-variable-not-defined/65802/3 "2016-11-15T09:40:45Z")

</div>

Thanks, Daniel!

I see. This explains a lot! Thanks for opening the issue.

Does that mean you cannot write a scripted\_metric aggregation with painless?

Michel

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [November 17, 2016, 6:19am UTC](https://discuss.elastic.co/t/scripted-metric--agg-variable-not-defined/65802/4 "2016-11-17T06:19:52Z")

</div>

Hi @michel-kraemer,

> [@michel-kraemer](#):
>
> Does that mean you cannot write a scripted\_metric aggregation with painless?

You can write such an aggregation with Painless. It's just that the documentation was not up to date. Nik fixed the ticket already and you can [see now in the docs how to write a `scripted_metric` aggregation with Painless](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)

Daniel

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 15, 2016, 6:20am UTC](https://discuss.elastic.co/t/scripted-metric--agg-variable-not-defined/65802/5 "2016-12-15T06:20:09Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
