# Retrieve the last Documents from ElasticSearch in Ascending Order

**URL:** https://discuss.elastic.co/t/retrieve-the-last-documents-from-elasticsearch-in-ascending-order/169137
**Category:** Elasticsearch
**Created:** [February 20, 2019, 6:05am UTC](https://discuss.elastic.co/t/retrieve-the-last-documents-from-elasticsearch-in-ascending-order/169137 "2019-02-20T06:05:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![donnadulcinea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/donnadulcinea/32/39276_2.png) [@donnadulcinea](https://discuss.elastic.co/u/donnadulcinea)
#### Post date: [February 20, 2019, 6:05am UTC](https://discuss.elastic.co/t/retrieve-the-last-documents-from-elasticsearch-in-ascending-order/169137/1 "2019-02-20T06:05:59Z")

</div>

I want to retrieve the last 3 documents from a set taking into account that the desired elements order is given by a field named _created_ . To do that I'm exploiting the combination of _sort + order_ mechanism, as also pointed out in many SO Q&A:

- [Return the most recent record from ElasticSearch index](https://stackoverflow.com/questions/20723379/return-the-most-recent-record-from-elasticsearch-index)
- [Sorting and latest records in ElasticSearch](https://stackoverflow.com/questions/11607808/sorting-and-latest-records-in-elasticsearch)
- [Get last document from ElasticSearch](https://stackoverflow.com/questions/38062504/get-last-document-from-elasticsearch)

Note: The field _created_ in my case is a timestamp but wrapped on a long.

```auto
{
    "size" : 3,
    "sort": [
    {
      "created": {
        "order": "desc"
      }
    }
  ]
}

```

But with this stratagem the results are retrieved in descending order. I'd like them to be returned in ascending order. Which way may I combine these two requirements?

Example, having this set (in the form `field:value;` ):

```auto
doc:A; created:1;
doc:C; created:2;
doc:F; created:3;
doc:D; created:4;
doc:B; created:5;
doc:H; created:6;
doc:G; created:7;

```

I want to retrieve the last three documents according to _created_ , which is OK using the above query, but the results are given in in the order _G, H, B_ corresponding to the value _7, 6, 5_ , while I would like to keep the order _B, H, G_ , corresponding to the values _5, 6, 7_ of the field _created_ .

How to achieve this?

---

<div class="post-metadata">

### Author: ![donnadulcinea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/donnadulcinea/32/39276_2.png) [@donnadulcinea](https://discuss.elastic.co/u/donnadulcinea)
#### Post date: [February 20, 2019, 10:35am UTC](https://discuss.elastic.co/t/retrieve-the-last-documents-from-elasticsearch-in-ascending-order/169137/3 "2019-02-20T10:35:16Z")

</div>

Sorry, what does that mean? I didn't withdraw the post, unless I touched something for mistake. And I can't see any flag, in case could you help me to reopen the post?

---

<div class="post-metadata">

### Author: ![Magnus\_Kessler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnus_kessler/32/42001_2.png) [@Magnus\_Kessler](https://discuss.elastic.co/u/Magnus_Kessler)
#### Post date: [February 20, 2019, 10:43am UTC](https://discuss.elastic.co/t/retrieve-the-last-documents-from-elasticsearch-in-ascending-order/169137/4 "2019-02-20T10:43:43Z")

</div>

Sorry for any confusion caused. I started to provide an answer, but realised that I hadn't thought things through. Therefore I withdrew my own response.

---

<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: [March 20, 2019, 10:43am UTC](https://discuss.elastic.co/t/retrieve-the-last-documents-from-elasticsearch-in-ascending-order/169137/5 "2019-03-20T10:43:48Z")

</div>

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