# A query to match most recent and then bucketing the results

**URL:** <https://discuss.elastic.co/t/a-query-to-match-most-recent-and-then-bucketing-the-results/38940>\
**Category:** Elasticsearch\
**Created:** [January 12, 2016, 6:29am UTC](https://discuss.elastic.co/t/a-query-to-match-most-recent-and-then-bucketing-the-results/38940 "2016-01-12T06:29:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviv.ratzon](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@aviv.ratzon](https://discuss.elastic.co/u/aviv.ratzon)\
**Post date:** [January 12, 2016, 6:29am UTC](https://discuss.elastic.co/t/a-query-to-match-most-recent-and-then-bucketing-the-results/38940/1 "2016-01-12T06:29:08Z")

</div>

Hello,  
I have in my database something like this:

> @timestamp: 12/01/2015, BUG\_ID: 1, Status: open  
> @timestamp: 12/01/2015, BUG\_ID: 2, Status: open  
> @timestamp: 13/01/2015, BUG\_ID: 3, Status: open  
> @timestamp: 14/01/2015, BUG\_ID: 1, Status: closed  
> @timestamp: 14/01/2015, BUG\_ID: 4, Status: open  
> @timestamp: 18/01/2015, BUG\_ID: 2, Status: closed  
> @timestamp: 20/01/2015, BUG\_ID: 5, Status: open  
> @timestamp: 21/01/2015, BUG\_ID: 1, Status: reopened  
> @timestamp: 22/01/2015, BUG\_ID: 4, Status: fixed

I want to have the possibility to display the current (or at some point of time in the past) status of all the bugs. To do this I have to bucket all bugs by their ID, sort them by their time and set "size = 1".  
But what can I do after that?  
I am pretty new to elasticsearch and so even the first part is not so easy on me, but I can't think of a way, after I manage to get the most recent documents, how do I analaize the data. I though maybe the most recent could be preformed at the query part but I didn't find an option to do so.  
To my understanding if I'll use aggregations I'll get many buckets, each containing one bug and I wouldn't be able to do anything on those results.

I am running ES 2.1.0

Thank you very much,  
Aviv

---

<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:** [July 5, 2017, 11:25pm UTC](https://discuss.elastic.co/t/a-query-to-match-most-recent-and-then-bucketing-the-results/38940/2 "2017-07-05T23:25:16Z")

</div>


