# READ ONLY LATEST RECORDS FROM AN INDEX USING SPARK-SCALA API

**URL:** <https://discuss.elastic.co/t/read-only-latest-records-from-an-index-using-spark-scala-api/274035>\
**Category:** Elasticsearch\
**Created:** [May 26, 2021, 9:49am UTC](https://discuss.elastic.co/t/read-only-latest-records-from-an-index-using-spark-scala-api/274035 "2021-05-26T09:49:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [May 26, 2021, 9:49am UTC](https://discuss.elastic.co/t/read-only-latest-records-from-an-index-using-spark-scala-api/274035/1 "2021-05-26T09:49:59Z")

</div>

i want to read only the latest records from an index so i am passing the below query in my spark-scala code  
conf.set("es.query","""{  
"query": {  
"match\_all": {}  
},  
"sort": [  
{  
"@timestamp": {  
"order": "desc"  
}  
}  
],  
"size": 1  
}""")

but this query doesn't fetch me the latest records instead it's fetching the older records. below is the output  
 ![data](https://us1.discourse-cdn.com/elastic/original/3X/e/f/ef2c3d0180a56c4be801f459e50cf9503187d11c.png)

and when i run this query in Kibana Dev Tools it's fetching me the latest records. please help 🙂

---

<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:** [June 23, 2021, 9:50am UTC](https://discuss.elastic.co/t/read-only-latest-records-from-an-index-using-spark-scala-api/274035/2 "2021-06-23T09:50:33Z")

</div>

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