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