So if I'm reading the example right it is trying to do some kind of standard deviation where each day's total is expressed as a multiple of the average sales for all days.
To do this you'd need something like this:
DELETE test
POST test/sales
{
"amount":10,
"date":"2012-09-21"
}
POST test/sales
{
"amount":20,
"date":"2012-09-21"
}
POST test/sales
{
"amount":5,
"date":"2012-09-22"
}
POST test/sales
{
"amount":15,
"date":"2012-09-22"
}
GET test/sales/_search
{
"size": 0,
"aggs": {
"by_date": {
"date_histogram": {
"field": "date",
"interval": "day"
},
"aggs": {
"Total": {
"sum": {
"field": "amount"
}
},
"normalized_sales": {
"bucket_script": {
"buckets_path": {
"dailySalesTotal": "Total",
//!!!! THIS DOES NOT WORK.... !!!!!
"allDaysAverage": "../global_stats>avg"
},
"script": "params.dailySalesTotal / params.allDaysAverage"
}
}
}
},
"global_stats": {
"stats_bucket": {
"buckets_path": "by_date>Total"
}
}
}
}
But notice this won't work because inside the array of by_date results we can't refer to a parent-level stat (attempted here using the illegal .. syntax). The docs state:
Paths are relative from the position of the pipeline aggregation; they are not absolute paths, and the path cannot go back "up" the aggregation tree