Variable [state] is not defined


(Robinson Ma) #1

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

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"?


(Ryan Ernst) #2

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.


(Robinson Ma) #3

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


(system) #4

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