Hello ,
i created an index with date, product and revenue fields. i need to calculate the total revenue made in 2016 then divide for each document that its product is sold in 2017 the revenue by the total revenue of 2016 . i am using the scripted metrics of elasticsearch 's aggregatons .
it's my first time using it ,this the script
GET /retails_transactions
{
"query": {
"match_all": {}
},
"aggs": {
"revenue_year_to_day": {
"scripted_metric": {
"init_script": "_agg["tempArray"] = [];",
"map_script": "revenue_previous_year= 0;if ( doc.transaction_date.value in [1-1-2016 ,12-30-2016]) { revenue_previous_year += doc.revenue.value } { _agg.tempArray.add(revenue_previous_year); } ",
"combine_script": "revenue_per_ytd = 0; if ( doc.transaction_date.value in [1-1-2017, 12-30-2017]){for (i in _agg.tempArray) { revenue_per_ytd += doc.revenue.value / i }}; return revenue_per_ytd;",
"reduce_script": "revenue_per_ytd = 0; for (j in _aggs) { revenue_per_ytd += doc.revenue.value /j }; return revenue_per_ytd ;"
}
}
}
}
it doesn't work and i don't get where maintains the problem ??