# NEST Search and return most recent document?

**URL:** https://discuss.elastic.co/t/nest-search-and-return-most-recent-document/62938
**Category:** Elasticsearch
**Created:** [October 13, 2016, 12:43pm UTC](https://discuss.elastic.co/t/nest-search-and-return-most-recent-document/62938 "2016-10-13T12:43:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![agonzalez](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@agonzalez](https://discuss.elastic.co/u/agonzalez)
#### Post date: [October 13, 2016, 12:43pm UTC](https://discuss.elastic.co/t/nest-search-and-return-most-recent-document/62938/1 "2016-10-13T12:43:49Z")

</div>

I am indexing documents using NEST, they dont have a timestamp, is there any way to retrieve/search for the most recent inserted document? or do i have to add a timestamp to my docs when indexing and then sort by that and size(1)?

---

<div class="post-metadata">

### Author: ![KodrAus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kodraus/32/22798_2.png) [@KodrAus](https://discuss.elastic.co/u/KodrAus)
#### Post date: [October 13, 2016, 9:13pm UTC](https://discuss.elastic.co/t/nest-search-and-return-most-recent-document/62938/2 "2016-10-13T21:13:48Z")

</div>

Yeh, you'll have to add a timestamp to your docs, and can use a simple `match_all` query with `size = 1` and `sort` on your timestamp field.

---

<div class="post-metadata">

### Author: ![agonzalez](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@agonzalez](https://discuss.elastic.co/u/agonzalez)
#### Post date: [October 13, 2016, 9:16pm UTC](https://discuss.elastic.co/t/nest-search-and-return-most-recent-document/62938/3 "2016-10-13T21:16:30Z")

</div>

I am trying to set a Timestamp to documents on Index but the parameter is Nest.Time how can i convert DateTime.Now to Nest.Time TimeSpan?

```
index_result = elk.Index(c, i => i
    .Index("index-" + date)
    .Id(asset_tag + "_appliance")
    .Timestamp(DateTime.Now)
    );
```

---

<div class="post-metadata">

### Author: ![KodrAus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kodraus/32/22798_2.png) [@KodrAus](https://discuss.elastic.co/u/KodrAus)
#### Post date: [October 13, 2016, 9:29pm UTC](https://discuss.elastic.co/t/nest-search-and-return-most-recent-document/62938/4 "2016-10-13T21:29:44Z")

</div>

That's probably not the timestamp you want to be using.

What you want to do is add a `DateTime` (or `DateTimeOffset`) field to your document type, and that's the field you then sort on.

---

<div class="post-metadata">

### Author: ![agonzalez](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@agonzalez](https://discuss.elastic.co/u/agonzalez)
#### Post date: [October 13, 2016, 9:48pm UTC](https://discuss.elastic.co/t/nest-search-and-return-most-recent-document/62938/5 "2016-10-13T21:48:07Z")

</div>

Thanks that worked!

---

<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, 10:12pm UTC](https://discuss.elastic.co/t/nest-search-and-return-most-recent-document/62938/6 "2017-07-05T22:12:33Z")

</div>


