# Variable \[state\] is not defined

**URL:** <https://discuss.elastic.co/t/variable-state-is-not-defined/154728>\
**Category:** Elasticsearch\
**Created:** [October 30, 2018, 9:30pm UTC](https://discuss.elastic.co/t/variable-state-is-not-defined/154728 "2018-10-30T21:30:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robinsonmhj](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@robinsonmhj](https://discuss.elastic.co/u/robinsonmhj)\
**Post date:** [October 30, 2018, 9:30pm UTC](https://discuss.elastic.co/t/variable-state-is-not-defined/154728/1 "2018-10-30T21:30:58Z")

</div>

Hi Guys,

I was playing with elastic search "Scripted Metric Aggregation", I get the error like this

```
{

```

"error": {  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "compile error",  
"script\_stack": [  
"state.transactions = ",  
"^---- HERE"  
],  
"script": "state.transactions = ",  
"lang": "painless"  
}  
],  
"type": "search\_phase\_execution\_exception",  
"reason": "all shards failed",  
"phase": "query",  
"grouped": true,  
"failed\_shards": [  
{  
"shard": 0,  
"index": "transactions",  
"node": "O3HVsMQ3Tsamd\_rc9ECGUA",  
"reason": {  
"type": "script\_exception",  
"reason": "compile error",  
"script\_stack": [  
"state.transactions = ",  
"^---- HERE"  
],  
"script": "state.transactions = ",  
"lang": "painless",  
"caused\_by": {  
"type": "illegal\_argument\_exception",  
"reason": "Variable [state] is not defined."  
}  
}  
}  
]  
},  
"status": 500  
}

I was using the code in the ElasticSearch 6.4 document. Here is the link

[https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-aggregations-metrics-scripted-metric-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-aggregations-metrics-scripted-metric-aggregation.html)

```
POST ledger/_search?size=0

```

{  
"query" : {  
"match\_all" : {}  
},  
"aggs": {  
"profit": {  
"scripted\_metric": {  
"init\_script" : "state.transactions = ",  
"map\_script" : "state.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 \* doc.amount.value)",  
"combine\_script" : "double profit = 0; for (t in state.transactions) { profit += t } return profit",  
"reduce\_script" : "double profit = 0; for (a in states) { profit += a } return profit"  
}  
}  
}  
}

Can you please point out what is wrong? Do I have to declare them in the "params"?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 30, 2018, 9:59pm UTC](https://discuss.elastic.co/t/variable-state-is-not-defined/154728/2 "2018-10-30T21:59:13Z")

</div>

What version of elasticsearch are you on? `state` was added in 6.4, that is why it is in the docs there, but not in 6.3 and earlier.

---

<div class="post-metadata">

**Author:** ![robinsonmhj](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@robinsonmhj](https://discuss.elastic.co/u/robinsonmhj)\
**Post date:** [October 30, 2018, 10:08pm UTC](https://discuss.elastic.co/t/variable-state-is-not-defined/154728/3 "2018-10-30T22:08:51Z")

</div>

My fault. I though I was using 6.4, but I am using 6.3. Thanks

---

<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:** [November 27, 2018, 10:08pm UTC](https://discuss.elastic.co/t/variable-state-is-not-defined/154728/4 "2018-11-27T22:08:59Z")

</div>

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