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
- Sorting and latest records in ElasticSearch
- Get last document from ElasticSearch
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?