I need to get top hits of scripted metric aggregation. can anyone suggest me how to do that?

here is my query
{
"size": 0,
"query": {
"nested": {
"path": "items",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "items"
}
}
]
}
}
}
},
"aggs": {
"delayed_m": {
"scripted_metric": {
"init_script": "params._agg.transactions = ",
"map_script": "params.max = 0; for(item in params._source.items){ if(item.deliveryLeadTime != null && item.deliveryLeadTime > params.max){params.max = item.deliveryLeadTime }} params.delayed = ((params._source.expirationDate + 86400000) - params.max2460601000 - new Date().getTime()) < 0; params._agg.transactions.add(params.delayed)",
"combine_script": "params.sum = 0; for (max in params._agg.transactions) { if(max)params.sum += 1 } return params.sum",
"reduce_script": "params.sum =0; for(max in params._aggs){params.sum += max} return params.sum"
}
}
}
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.