Retrieve the last Documents from ElasticSearch in Ascending Order

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:

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

{
    "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; ):

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?

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?

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.

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