# Elasticsearch query to pull the record based on max timestamp

**URL:** https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131
**Category:** Logstash
**Created:** [February 16, 2018, 9:39am UTC](https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131 "2018-02-16T09:39:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ranganath\_nangineni](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@ranganath\_nangineni](https://discuss.elastic.co/u/ranganath_nangineni)
#### Post date: [February 16, 2018, 9:39am UTC](https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131/1 "2018-02-16T09:39:22Z")

</div>

Hi,

I have 4 records in the elasticsearch DB for the ddocname "CNT1882742" for the below query

# Logstash elasticsearch query:

query =\> '{ "query": {"match": { "ddocname" :"CNT1882742"} }, "sort": ["@timestamp"] }'

# Query output:

{"@timestamp":"2018-02-07T08:30:15.944Z","@version":"1","ddocname":"CNT1882742","ddoctitle":"VSNL\_R12Upgrade\_TECH\_UPG\_Resource\_Mix-DAA1\_V1.12.xls","did":4835074,"tags":["MYO Stage","data"]}  
{"@timestamp":"2018-02-07T10:26:42.139Z","@version":"1","ddocname":"CNT1882742","ddoctitle":"VSNL\_R12Upgrade\_TECH\_UPG\_Resource\_Mix-DAA1\_V1.12.xls","did":4835074,"tags":["MYO Stage","data"]}  
{"@timestamp":"2018-02-07T23:46:38.974Z","@version":"1","ddocname":"CNT1882742","ddoctitle":"VSNL\_R12Upgrade\_TECH\_UPG\_Resource\_Mix-DAA1\_V1.12.xls","did":4835074,"tags":["MYO Stage","data"]}  
{"@timestamp":"2018-02-08T08:07:38.554Z","@version":"1","ddocname":"CNT1882742","ddoctitle":"VSNL\_R12Upgrade\_TECH\_UPG\_Resource\_Mix-DAA1\_V1.12.xls","did":4835074,"tags":["MYO Stage","data"]}

How to get only "one" record based on the "max timestamp" for the above. How to modify the query to achieve this ?

Expected output:  
{"@timestamp":"2018-02-08T08:07:38.554Z","@version":"1","ddocname":"CNT1882742","ddoctitle":"VSNL\_R12Upgrade\_TECH\_UPG\_Resource\_Mix-DAA1\_V1.12.xls","did":4835074,"tags":["MYO Stage","data"]}

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [February 16, 2018, 11:27am UTC](https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131/2 "2018-02-16T11:27:50Z")

</div>

Make sure the sorting on `@timestamp` is in descending order and set the plugin's `size` option to 1.

---

<div class="post-metadata">

### Author: ![ranganath\_nangineni](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@ranganath\_nangineni](https://discuss.elastic.co/u/ranganath_nangineni)
#### Post date: [February 16, 2018, 1:11pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131/3 "2018-02-16T13:11:25Z")

</div>

It didnt work Magnusbaeck.

I tried this  
query =\> '{ "query": {"match": { "ddocname" :"CNT1882742"} },"sort": {"@timestamp":{"order":"desc"}},"from":0,"size":1 }'

Still I m getting 4 results sorted based on timestamp in desc order.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [February 16, 2018, 1:44pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131/4 "2018-02-16T13:44:45Z")

</div>

I said "set the **plugin's** `size` option to 1". I didn't say anything about modifying the query.

---

<div class="post-metadata">

### Author: ![ranganath\_nangineni](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@ranganath\_nangineni](https://discuss.elastic.co/u/ranganath_nangineni)
#### Post date: [February 16, 2018, 1:51pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131/5 "2018-02-16T13:51:39Z")

</div>

Sorry about that.  
If you dont mind, Can you tell me how to change the plugin's size?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [February 16, 2018, 1:58pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131/6 "2018-02-16T13:58:35Z")

</div>

It's a plugin option, just like `query` that you're already using. See the plugin documentation.

---

<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: [March 16, 2018, 1:58pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-pull-the-record-based-on-max-timestamp/120131/7 "2018-03-16T13:58:49Z")

</div>

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