# Need to get latest 2 records

**URL:** https://discuss.elastic.co/t/need-to-get-latest-2-records/204092
**Category:** Elasticsearch
**Created:** [October 17, 2019, 3:45pm UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092 "2019-10-17T15:45:16Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)
#### Post date: [October 17, 2019, 3:45pm UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/1 "2019-10-17T15:45:17Z")

</div>

Hi,

i need a query to fetch last and 2nd last record entered the index.

What should be the query other than putting size: 2 after sorting and all?

thanks

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 17, 2019, 5:03pm UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/2 "2019-10-17T17:03:54Z")

</div>

Do you have a timestamp in your document?

---

<div class="post-metadata">

### Author: ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)
#### Post date: [October 18, 2019, 4:45am UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/3 "2019-10-18T04:45:35Z")

</div>

yes its there

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 18, 2019, 8:11am UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/4 "2019-10-18T08:11:54Z")

</div>

So a sort on timestamp with size=2 should work, right?

---

<div class="post-metadata">

### Author: ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)
#### Post date: [October 18, 2019, 12:55pm UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/5 "2019-10-18T12:55:12Z")

</div>

it's working but another way.....

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 18, 2019, 2:00pm UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/6 "2019-10-18T14:00:32Z")

</div>

So? Is that ok?

---

<div class="post-metadata">

### Author: ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)
#### Post date: [October 19, 2019, 7:17am UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/7 "2019-10-19T07:17:38Z")

</div>

is this is not the efficient way,  
we are extracting all the documents and just displaying the top one (or two).  
how to write a query which just extracts only these 2 records?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 19, 2019, 8:51am UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/8 "2019-10-19T08:51:18Z")

</div>

I don't understand. Why is this not efficient? What is the query you wrote?

---

<div class="post-metadata">

### Author: ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)
#### Post date: [October 24, 2019, 5:26am UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/9 "2019-10-24T05:26:06Z")

</div>

this is not efficient coz query is fetching ALL the records but displaying only 1.  
is it possible that the query only will fetch 1 record from the index (1 or 2 records)

this is according to me,  
please let me know if i am not correct on this.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 24, 2019, 8:15am UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/10 "2019-10-24T08:15:27Z")

</div>

This is wrong. Just give it a try and if it's too slow, share what you did.

And if you want it to be super fast, use [https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-index-sorting.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-index-sorting.html)

---

<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: [November 21, 2019, 8:15am UTC](https://discuss.elastic.co/t/need-to-get-latest-2-records/204092/11 "2019-11-21T08:15:30Z")

</div>

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