# Most recent occurrence of a document

**URL:** <https://discuss.elastic.co/t/most-recent-occurrence-of-a-document/6757>\
**Category:** Elasticsearch\
**Created:** [February 20, 2012, 3:09pm UTC](https://discuss.elastic.co/t/most-recent-occurrence-of-a-document/6757 "2012-02-20T15:09:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yehuda\_Zargarov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yehuda_zargarov/32/2164_2.png) [@Yehuda\_Zargarov](https://discuss.elastic.co/u/Yehuda_Zargarov)\
**Post date:** [February 20, 2012, 3:09pm UTC](https://discuss.elastic.co/t/most-recent-occurrence-of-a-document/6757/1 "2012-02-20T15:09:17Z")

</div>

Hi

I am trying to build a query on an index which has 3 fields: name,  
date and sum.  
for a given name, I want to fetch the most recent (by date) occurrence  
of that name, and get the sum field.

for example, if I have 2 docs:  
"yehuda", "2012-02-20", "5"  
"yehuda", "2012-02-19", "4"

I want to get "5".  
The order of insertion is of course something I can't rely on.

Any suggestions?

Thanks!

Yehuda

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [February 20, 2012, 10:29pm UTC](https://discuss.elastic.co/t/most-recent-occurrence-of-a-document/6757/2 "2012-02-20T22:29:31Z")

</div>

Hi Yehuda,

You can execute a match all query and sort by the date field. Make  
sure the mapping for the date field is correct (date, not string) and  
that it is stored.

--  
Ivan

On Mon, Feb 20, 2012 at 7:09 AM, Yehuda Zargarov [yz1583@gmail.com](mailto:yz1583@gmail.com) wrote:

> Hi
> 
> I am trying to build a query on an index which has 3 fields: name,  
> date and sum.  
> for a given name, I want to fetch the most recent (by date) occurrence  
> of that name, and get the sum field.
> 
> for example, if I have 2 docs:  
> "yehuda", "2012-02-20", "5"  
> "yehuda", "2012-02-19", "4"
> 
> I want to get "5".  
> The order of insertion is of course something I can't rely on.
> 
> Any suggestions?
> 
> Thanks!
> 
> Yehuda

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 24, 2012, 1:11pm UTC](https://discuss.elastic.co/t/most-recent-occurrence-of-a-document/6757/3 "2012-02-24T13:11:34Z")

</div>

Yea, just sort by date. It doesn't have to be stored, just indexed (which is the default).

On Tuesday, February 21, 2012 at 12:29 AM, Ivan Brusic wrote:

> Hi Yehuda,
> 
> You can execute a match all query and sort by the date field. Make  
> sure the mapping for the date field is correct (date, not string) and  
> that it is stored.
> 
> --  
> Ivan
> 
> On Mon, Feb 20, 2012 at 7:09 AM, Yehuda Zargarov \<[yz1583@gmail.com](mailto:yz1583@gmail.com) ([mailto:yz1583@gmail.com](mailto:yz1583@gmail.com))\> wrote:
> 
> > Hi
> > 
> > I am trying to build a query on an index which has 3 fields: name,  
> > date and sum.  
> > for a given name, I want to fetch the most recent (by date) occurrence  
> > of that name, and get the sum field.
> > 
> > for example, if I have 2 docs:  
> > "yehuda", "2012-02-20", "5"  
> > "yehuda", "2012-02-19", "4"
> > 
> > I want to get "5".  
> > The order of insertion is of course something I can't rely on.
> > 
> > Any suggestions?
> > 
> > Thanks!
> > 
> > Yehuda

---

<div class="post-metadata">

**Author:** ![Yehuda\_Zargarov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yehuda_zargarov/32/2164_2.png) [@Yehuda\_Zargarov](https://discuss.elastic.co/u/Yehuda_Zargarov)\
**Post date:** [March 4, 2012, 2:20pm UTC](https://discuss.elastic.co/t/most-recent-occurrence-of-a-document/6757/4 "2012-03-04T14:20:09Z")

</div>

Sorting is an option, but when you have to get the last date result  
for 600 users, it's not a very good one.

I mean, the example above is for 1 user. the real situation will be:

"yehuda", "2012-02-20", "5"  
"yehuda", "2012-02-19", "4"  
"Moshe", "2012-02-20", "5"  
"Moshe", "2012-02-19", "4"  
"Leon", "2012-02-20", "5"  
"Leon", "2012-02-19", "4"  
"Ben", "2012-02-20", "5"  
"Ben", "2012-02-19", "4"  
..

I need to sum up the values of the latest date of Yehuda, Moshe, Leon  
and Ben..

Is there a more efficient way to do this?

Thanks

On Feb 24, 3:11 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Yea, just sort by date. It doesn't have to be stored, just indexed (which is the default).
> 
> On Tuesday, February 21, 2012 at 12:29 AM, Ivan Brusic wrote:
> 
> > Hi Yehuda,
> 
> > You can execute a match all query and sort by the date field. Make  
> > sure the mapping for the date field is correct (date, not string) and  
> > that it is stored.
> 
> > --  
> > Ivan
> 
> > On Mon, Feb 20, 2012 at 7:09 AM, Yehuda Zargarov \<[yz1...@gmail.com](mailto:yz1...@gmail.com) ([mailto:yz1...@gmail.com](mailto:yz1...@gmail.com))\> wrote:
> > 
> > > Hi
> 
> > > I am trying to build a query on an index which has 3 fields: name,  
> > > date and sum.  
> > > for a given name, I want to fetch the most recent (by date) occurrence  
> > > of that name, and get the sum field.
> 
> > > for example, if I have 2 docs:  
> > > "yehuda", "2012-02-20", "5"  
> > > "yehuda", "2012-02-19", "4"
> 
> > > I want to get "5".  
> > > The order of insertion is of course something I can't rely on.
> 
> > > Any suggestions?
> 
> > > Thanks!
> 
> > > Yehuda

---

<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:04pm UTC](https://discuss.elastic.co/t/most-recent-occurrence-of-a-document/6757/6 "2017-07-05T23:04:02Z")

</div>


