# How to keep on x no of recent documents based on field

**URL:** https://discuss.elastic.co/t/how-to-keep-on-x-no-of-recent-documents-based-on-field/196822
**Category:** Elasticsearch
**Created:** [August 26, 2019, 6:29pm UTC](https://discuss.elastic.co/t/how-to-keep-on-x-no-of-recent-documents-based-on-field/196822 "2019-08-26T18:29:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![saurabh\_baid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurabh_baid/32/53054_2.png) [@saurabh\_baid](https://discuss.elastic.co/u/saurabh_baid)
#### Post date: [August 26, 2019, 6:29pm UTC](https://discuss.elastic.co/t/how-to-keep-on-x-no-of-recent-documents-based-on-field/196822/1 "2019-08-26T18:29:01Z")

</div>

We are trying to evaluate elasticsearch for keeping up our events and very impressed so far.  
Though there are two issues we need help with

- We need to only keep X no of events per MAC address and old events should be purged. Since MAC could be in order of thousands obviously having separate indexes per mac is out of question.  
I am assuming that this should be possible using aggregation and pipelines but now sure how
- Is there a way we can get metric of No of query served during a time period, we need this info for our own benchmarking purposes.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![saurabh\_baid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurabh_baid/32/53054_2.png) [@saurabh\_baid](https://discuss.elastic.co/u/saurabh_baid)
#### Post date: [August 26, 2019, 8:15pm UTC](https://discuss.elastic.co/t/how-to-keep-on-x-no-of-recent-documents-based-on-field/196822/2 "2019-08-26T20:15:29Z")

</div>

So currently I am using following approach

- Iterate through all the macs

- For each mac

- Once I have that

Now this is certainly not the efficient way, Is there a way to combine all three steps into one aggregation/pipeline.

---

<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: [September 23, 2019, 8:15pm UTC](https://discuss.elastic.co/t/how-to-keep-on-x-no-of-recent-documents-based-on-field/196822/3 "2019-09-23T20:15:32Z")

</div>

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