Yes, collapse only manipulate the output and not affect aggregation.
If you want aggregation on top 1000 documents for each ids, just exchange the place of sampler aggregation and terms aggregation.
GET /kibana_sample_data_flights/_search
{
"size":0,
"query":{
"function_score": {
"field_value_factor": {
"field": "timestamp"
}
}
},
"aggs":{
"ids":{
"terms": {
"field": "DestAirportID",
"size": 10
},
"aggs":{
"top1000":{
"sampler": {
"shard_size": 1000
},
"aggs":{
"percentile":{
"percentiles": {
"field": "AvgTicketPrice",
"percents": [
50
]
}
}
}
}
}
}
}
}