Getting aggregations data / paginations on aggregation results

Hi,
We have a lot of records and a lot of duplicates in the our storage.

I need to search and return results without duplicates, so I wrote a query which does an aggregation on a specific term, and returns one record per bucket so in my bucket results we have no duplications.

Is there a way to add an aggregation to get facets values for additional terms - but only for the bucket results?

My example:
lets say I have these docs:
{
"id": 123,
"type": "Good",
"timestamp": 12345
}, {
"id": 123,
"type": "Bad",
"timestamp": 12346
}, {
"id": 125,
"type": "Bad",
"timestamp": 12347
}, {
"id": 126,
"type": "Good",
"timestamp": 12348
}

After an aggregation on 'id' and sorting by 'timestamp' desc to eliminate duplicates i get 3 buckets with the records:
'id': 123, 'type': "Bad"
'id': 125, 'type': "Bad"
'id': 126, 'type': "Good"

And I need to get the data:
'type': "Good", 'doc_count': 1, 'type': "Bad", 'doc_count': 2.

Also is it possible to apply pagination on the aggregations buckets?

Thanks!

To my knowledge not yet. Check Paging support for aggregations · Issue #4915 · elastic/elasticsearch · GitHub for details.

Hope this helps,
Isabel