2

I have a need to query the latest index that is created on Elasticsearch.

I have formed the below curl command

curl -X GET "localhost/_cat/indices?h=i,creation.date.string"

It is returning the response as follows:

accounting       2020-03-03T02:46:29.285Z
kibana_1         2020-03-02T00:58:14.386Z 

Is there any way I can return this in a JSON format? I have been trying this and couldn't get a JSON response.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35

2 Answers2

4

It is indeed possible to return the response from the _cat API in JSON. It is also possible to sort the results by creation.date.string.

Try this and the first element from the resulting array will be the last create index.

curl -H "Content-type: application/json" -X GET "localhost/_cat/indices?h=i,creation.date.string&format=json&s=creation.date.string:desc"
Val
  • 207,596
  • 13
  • 358
  • 360
  • 1
    Little modification if the prefix of the index is known. `curl -H "Content-type: application/json" -X GET "localhost:9200/_cat/indices/index-prefix-*?h=i,creation.date.string&format=json&s=creation.date.string:desc&pretty"` – punjabi4life Dec 22 '20 at 16:18
0

You can pass the format in query /_cat/indices?format=json&h=i,creation.date.string

jaspreet chahal
  • 8,817
  • 2
  • 11
  • 29