# Document count after metric aggregation

**URL:** <https://discuss.elastic.co/t/document-count-after-metric-aggregation/166298>\
**Category:** Elasticsearch\
**Created:** [January 30, 2019, 8:14am UTC](https://discuss.elastic.co/t/document-count-after-metric-aggregation/166298 "2019-01-30T08:14:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dclouard](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@dclouard](https://discuss.elastic.co/u/dclouard)\
**Post date:** [January 30, 2019, 8:14am UTC](https://discuss.elastic.co/t/document-count-after-metric-aggregation/166298/1 "2019-01-30T08:14:54Z")

</div>

Hi,

I have a document representing of event on activity. This document is defined by

- activity id
- creation date of event
- event type
- message

I have to count the number of events per type but only on the last event for each activity id. I have to keep all events in the index for other purposes.

I know how to retrieve the last document from each id activity with a "top-hit" aggregation. I can count the number of documents by type of events. but I can not make the connection between these two requests.

Is there a way to link these two queries ? Should we create an index containing the latest events of each activity at the insertion of an new event ? Or is there another way to achieve the result?

Thanks for you help

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 27, 2019, 8:14am UTC](https://discuss.elastic.co/t/document-count-after-metric-aggregation/166298/2 "2019-02-27T08:14:54Z")

</div>

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