# Get latest documents where field is distinct

**URL:** <https://discuss.elastic.co/t/get-latest-documents-where-field-is-distinct/86162>\
**Category:** Elasticsearch\
**Created:** [May 17, 2017, 6:34pm UTC](https://discuss.elastic.co/t/get-latest-documents-where-field-is-distinct/86162 "2017-05-17T18:34:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnrodey](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@johnrodey](https://discuss.elastic.co/u/johnrodey)\
**Post date:** [May 17, 2017, 6:34pm UTC](https://discuss.elastic.co/t/get-latest-documents-where-field-is-distinct/86162/1 "2017-05-17T18:34:30Z")

</div>

Given an index where it contains user login documents, so there is a userid field and a time field. Can I query for all the last login document for each user?

I don't want to build a list of each user on the query because it could be huge.

Thanks!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 17, 2017, 8:44pm UTC](https://discuss.elastic.co/t/get-latest-documents-where-field-is-distinct/86162/2 "2017-05-17T20:44:06Z")

</div>

There's an example of that very question here: [https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-aggregations-bucket-terms-aggregation.html#\_filtering\_values\_with\_partitions](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_partitions)

---

<div class="post-metadata">

**Author:** ![johnrodey](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@johnrodey](https://discuss.elastic.co/u/johnrodey)\
**Post date:** [May 17, 2017, 10:31pm UTC](https://discuss.elastic.co/t/get-latest-documents-where-field-is-distinct/86162/4 "2017-05-17T22:31:34Z")

</div>

Thanks. Maybe its trivial but I'm not looking for the last login time, but rather the entire login document for that time. It would be nice to get that in a single query.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 18, 2017, 6:54am UTC](https://discuss.elastic.co/t/get-latest-documents-where-field-is-distinct/86162/5 "2017-05-18T06:54:37Z")

</div>

Add a top\_hits agg with size:1 sorted by date, reverse order. The partition advice still stands if you have a lot of users.

---

<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:** [June 15, 2017, 6:55am UTC](https://discuss.elastic.co/t/get-latest-documents-where-field-is-distinct/86162/6 "2017-06-15T06:55:27Z")

</div>

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