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

**URL:** <https://discuss.elastic.co/t/how-to-make-a-get-request-to-retrieve-the-latest-document-of-an-index-in-the-elasticsearch-cluster/142828>\
**Category:** Elasticsearch\
**Created:** [August 3, 2018, 12:17am UTC](https://discuss.elastic.co/t/how-to-make-a-get-request-to-retrieve-the-latest-document-of-an-index-in-the-elasticsearch-cluster/142828 "2018-08-03T00:17:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![APJ](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@APJ](https://discuss.elastic.co/u/APJ)\
**Post date:** [August 3, 2018, 12:17am UTC](https://discuss.elastic.co/t/how-to-make-a-get-request-to-retrieve-the-latest-document-of-an-index-in-the-elasticsearch-cluster/142828/1 "2018-08-03T00:17:29Z")

</div>

> <https://stackoverflow.com/questions/51663413/how-to-make-a-get-request-to-retrieve-the-latest-document-of-an-index-in-the-ela>

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!

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 3, 2018, 12:28am UTC](https://discuss.elastic.co/t/how-to-make-a-get-request-to-retrieve-the-latest-document-of-an-index-in-the-elasticsearch-cluster/142828/2 "2018-08-03T00:28:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![APJ](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@APJ](https://discuss.elastic.co/u/APJ)\
**Post date:** [August 3, 2018, 12:32am UTC](https://discuss.elastic.co/t/how-to-make-a-get-request-to-retrieve-the-latest-document-of-an-index-in-the-elasticsearch-cluster/142828/3 "2018-08-03T00:32:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 3, 2018, 1:15am UTC](https://discuss.elastic.co/t/how-to-make-a-get-request-to-retrieve-the-latest-document-of-an-index-in-the-elasticsearch-cluster/142828/4 "2018-08-03T01:15:57Z")

</div>

Thanks 🙂

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](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-aggregations-metrics-top-hits-aggregation.html) 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](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-request-from-size.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 31, 2018, 1:15am UTC](https://discuss.elastic.co/t/how-to-make-a-get-request-to-retrieve-the-latest-document-of-an-index-in-the-elasticsearch-cluster/142828/5 "2018-08-31T01:15:59Z")

</div>

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