# "How to get the oldest document of all indexes matching an index pattern

**URL:** https://discuss.elastic.co/t/how-to-get-the-oldest-document-of-all-indexes-matching-an-index-pattern/68542
**Category:** Elasticsearch
**Created:** [December 9, 2016, 9:49am UTC](https://discuss.elastic.co/t/how-to-get-the-oldest-document-of-all-indexes-matching-an-index-pattern/68542 "2016-12-09T09:49:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Guido2016](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Guido2016](https://discuss.elastic.co/u/Guido2016)
#### Post date: [December 9, 2016, 9:49am UTC](https://discuss.elastic.co/t/how-to-get-the-oldest-document-of-all-indexes-matching-an-index-pattern/68542/1 "2016-12-09T09:49:40Z")

</div>

Hello, is there a solution to get this information?

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [December 9, 2016, 3:56pm UTC](https://discuss.elastic.co/t/how-to-get-the-oldest-document-of-all-indexes-matching-an-index-pattern/68542/2 "2016-12-09T15:56:57Z")

</div>

We don't add a timestamp metadata field to the documents so, not in general. If you added a timestamp to all the documents yourself then you can [sort](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html) on it.

---

<div class="post-metadata">

### Author: ![Guido2016](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Guido2016](https://discuss.elastic.co/u/Guido2016)
#### Post date: [December 27, 2016, 1:29pm UTC](https://discuss.elastic.co/t/how-to-get-the-oldest-document-of-all-indexes-matching-an-index-pattern/68542/3 "2016-12-27T13:29:43Z")

</div>

Thank's for reply i'm new on elastic, if possible please give me a short howto to build my own sort to all the documents (howto add a timestamp to all the documents... and to use it in a query) THK's for helping me 🙂

---

<div class="post-metadata">

### Author: ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)
#### Post date: [December 27, 2016, 3:01pm UTC](https://discuss.elastic.co/t/how-to-get-the-oldest-document-of-all-indexes-matching-an-index-pattern/68542/4 "2016-12-27T15:01:21Z")

</div>

You should take a look at this:

[https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-request-sort.html#search-request-sort](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-request-sort.html#search-request-sort)

Just add a date field to the document you index, and then add a sort DESC to this field.

---

<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: [January 24, 2017, 3:01pm UTC](https://discuss.elastic.co/t/how-to-get-the-oldest-document-of-all-indexes-matching-an-index-pattern/68542/5 "2017-01-24T15:01:24Z")

</div>

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