I am trying to do a bucket script aggregation on a range aggregation (Date Histogram being the parent) and is ending up with an exception. The query that I am trying is
{
"aggs": {
"perhour": {
"date_histogram": {
"field": "@timestamp",
"interval": "1h",
"min_doc_count": 0
},
"aggs": {
"apdsatrange": {
"range": {
"field": "timetaken",
"ranges": [
{
"from": 0,
"to": 500
}
],
"keyed": true
},
"aggs" : {
"sat_count" : { "value_count" : { "field" : "timetaken" } }
}
},
"apdtolrange": {
"range": {
"field": "timetaken",
"ranges": [
{
"from": 500,
"to": 2000
}
],
"keyed": true
},
"aggs" : {
"tol_count" : { "value_count" : { "field" : "timetaken" } }
}
},
"apdcalc": {
"bucket_script": {
"buckets_path": {
"sat": "apdsatrange>sat_count",
"tol": "apdtolrange>tol_count"
},
"script": "sat + tol/2"
}
}
}
}
}
}
I have used two different range aggregation so as to refer the count for each in the bucket script. I end up with the following exception on execution
{
"error" : {
"root_cause" : [ ],
"type" : "reduce_search_phase_exception",
"reason" : "[reduce] ",
"phase" : "fetch",
"grouped" : true,
"failed_shards" : [ ],
"caused_by" : {
"type" : "aggregation_execution_exception",
"reason" : "buckets_path must reference either a number value or a single value numeric metric aggregation, got: java.lang.Object[]"
}
},
"status" : 503
}
Without the bucket aggregation, I am getting the individual range values as expected. eg:
"aggregations" : {
"perhour" : {
"buckets" : [ {
"key_as_string" : "2016-01-04T00:00:00.000Z",
"key" : 1451865600000,
"doc_count" : 4413,
"apdtolrange" : {
"buckets" : {
"500.0-2000.0" : {
"from" : 500.0,
"from_as_string" : "500.0",
"to" : 2000.0,
"to_as_string" : "2000.0",
"doc_count" : 48,
"tol_count" : {
"value" : 48
}
}
}
},
"apdsatrange" : {
"buckets" : {
"0.0-500.0" : {
"from" : 0.0,
"from_as_string" : "0.0",
"to" : 500.0,
"to_as_string" : "500.0",
"doc_count" : 4357,
"sat_count" : {
"value" : 4357
}
}
}
}
},....
Can anyone please help me in identifying what is wrong with the query or with a better way of doing this.?
ES version is 2.1