Get last document from ElasticSearch

ScipioAfricanus picture ScipioAfricanus · Jun 27, 2016 · Viewed 25.8k times · Source

so I have an elastic search index and I am sending docs to it attached with a timestamp. I am wondering if there is a way to extract the last document based on the time stamp. I.e. say to elastic give me the doc with the last time.

Thanks.

Answer

Val picture Val · Jun 27, 2016

Yes, you can simply request one single document (size: 1) and sorted by decreasing timestamp

POST index/_search
{
   "size": 1,
   "sort": { "timestamp": "desc"},
   "query": {
      "match_all": {}
   }
}