Bharat Bittu
Bharat Bittu

Reputation: 525

How do we get the last document from the elasticsearch index?

I'm using NodeJS to find the first and last document in Elasticsearch Index.

client.search({index: 'test',size:1,sort:[{"_timestamp":{"order":
"desc"}}]},function(err,resp,status) {     console.log("data",resp);
});

Where am I going wrong?

Upvotes: 0

Views: 1080

Answers (1)

Tarek Essam
Tarek Essam

Reputation: 4000

You should use a query to match all docs and then sort them :

client.search({
  index: 'test',
  query: {    
      "match_all": {}    
  }, 
  sort: [{
    "_timestamp": {
      "order":
      "desc"
    }
  }],
  size: 1 
}, function (err, resp, status) {
  console.log("data", resp);
});

Upvotes: 2

Related Questions