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) but it doesn't matter what I do I always get the following error:
{
"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