How to make a get request to retrieve the latest document of an index in the elasticsearch cluster?

I am developing a node.js application to get the latest value of an index from an elastic cluster. My logstash server pipes data to elasticsearch every second. So, the elasticsearch index gets updated every second. Every second a new document is added to the elasticsearch index.

My node.js code looks like this,

let express = require('express');
let app = express();
let elasticsearch = require('elasticsearch');

app.get('/', function(req, res) {
    res.send('Hello World!');
});
app.listen(3000, function() {
    console.log('Example app listening on port 3000!');
});

let client = new elasticsearch.Client({
    host: ['http://localhost:9200']
});

client.ping({
    requestTimeout: 30000,
}, function(error) {
    if (error) {
        console.error('elasticsearch cluster is down!');
    } else {
        console.log('Everything is ok');
    }
});

async function getResponse() {
    const response = await client.get({
        index: 'weather',
        type: 'doc',
        id: 'KsHW_GQBol0Vk4cfl2WY'
    });
    console.log(response);
}

getResponse();

I am able to retrieve the JSON document based on the id of the index. But, I want to retrieve the latest JSON document. How can I configure my server to read the latest document every second from the server? Is there a way to retrieve the latest JSON document(without knowing the id in advance)?

Can someone please help me with this? I would really appreciate if you could help.

Thanks in advance!

If you'd like assistance here, then please don't just post a link to SO assuming people will open it, include detail of what you are after.

Thanks! Sure, I have edited my question. Hope this helps.

Thanks :slight_smile:

I don't know JS so can't help with syntax, but if you have a timestamp in the docs then you could either run a top hits agg on the timestamp field and grab the latest that way. But that will only give you aggregated into, not the doc.

Or if you want the doc, just run a query with a filter on the last N seconds (where N is small) and use a size of 1 to only get the latest doc - https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-request-from-size.html

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