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!