43

How can the following query's results be sorted by index name?

curl "localhost:9200/_aliases?pretty"
5
  • Can't you sort by your own? I don't think it exists. Commented Jul 25, 2014 at 17:40
  • @ErBnAcharya The result has no field name to specify.
    – 10 cls
    Commented Jul 25, 2014 at 17:49
  • @ErBnAcharya How is that done?
    – 10 cls
    Commented Jul 25, 2014 at 17:56
  • have you tried curl localhost:9200/_cat/aliases | sort? I realize it's a completely different format, but it might be what you really wanted
    – Alcanzar
    Commented Jul 25, 2014 at 18:03
  • 1
    I recommend to use sort as @Alcanzar said. Try curl localhost:9200/_cat/indices | sort -nk2
    – Coffee
    Commented Mar 3, 2016 at 17:04

6 Answers 6

93

You can ask ES to sort the results via the s (sort) searchParameter using s=i or s=index

curl "localhost:9200/_cat/indices?pretty&s=i"
curl "localhost:9200/_cat/aliases?pretty&s=index"

To see the column's headers, add "&v" :

curl "localhost:9200/_cat/indices?pretty&v&s=index"`.

You can find some explanations in the cat/indices documentation

3
  • what do you mean ? curl is just doing an http request, you can use it in a browser or with any http client in any language
    – Romain
    Commented Dec 28, 2017 at 14:59
  • 2
    I couldn't get any of these to work via a postman get on ES 5.2+. I get 400 response "illegal_argument_exception, request [/_aliases/] contains unrecognized parameter: [s]"
    – w00ngy
    Commented Dec 28, 2017 at 20:21
  • 6
    You can also change the sort order: /_cat/indices?v&s=docs.count:desc Commented Feb 4, 2018 at 5:45
27

The best way for Elasticsearch 5x is like this:

GET _cat/aliases?v&s=index:desc&h=alias,index

Will give you:

alias                                     index
app-logs-alias                            app-logs-2017-12-31
backend-logs-read                         backend-logs-2017-12-31

s = sort, v = various extra detail, h = headings to include,

2
  • 4
    Plus +1 hands up for reverse ordering Commented Feb 3, 2019 at 17:26
  • 2
    This answer tells about so many features in a very clean fashion.
    – akki
    Commented Jul 26, 2019 at 5:40
15

I think that the best way to do this would be via the console. Something like this:

$ curl --silent 'http://path.to.cluster:9200/_cat/indices' | cut -d ' ' -f2 | sort

6
  • 1
    Ah, nice. I've changed it slightly to: curl --silent 'http://path.to.cluster:9200/_cat/aliases' | cut -d ' ' -f1 | sort
    – 10 cls
    Commented Jan 19, 2015 at 20:05
  • 5
    I had to change the cut to use -f3 instead of -f2.
    – Rylander
    Commented Oct 7, 2015 at 21:11
  • 4
    You can use -n(numeric sort) option and -k(key) option for sort instead of using cut. For example, curl 'localhost:9200/_cat/indices' | sort -nk 3
    – Coffee
    Commented Sep 12, 2016 at 21:55
  • How to sort in descending order using the above solution ?
    – Rupesh
    Commented Jul 14, 2021 at 5:43
  • I had to use awk because the spaces delimiting each column where not constant for me: $ curl --silent 'http://path.to.cluster:9200/_cat/indices' | awk '{ print $3 }' | sort
    – F. Pareto
    Commented Sep 2, 2021 at 4:52
15

This is an old question, but now in 2020 the best way is :

with kibana :

GET _cat/indices/?pretty&s=store.size:desc

with curl :

http://localhost:9200/_cat/indices/?pretty&s=store.size:desc

Desc at the end for sort by desc

3

Just use this get request it will show all indexes with the column name.

http://localhost:9200/_cat/indices/?pretty&v

Additionally, not only by name you can sort it by any parameter you want with a get parameter of s=column_name.

for example; to sort by the size you can do like:

http://localhost:9200/_cat/indices/?pretty&s=store.size

similarly for name:

http://localhost:9200/_cat/indices/?pretty&s=index
-1

I don't think it exists by elasticsearch api.

Response from elasticsearch can be

{
   "index1": {
      "aliases": {}
   }
}

Here is a pseudocode to get index from response

If aliasresponse is response from elasticsearch, then

indexlist=[]
for (key in aliasresponse) {
    indexlist.add(key)
}

sort(indexlist)

For sorting, you can find libaries or custom methods.

Hope this helps.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.