37

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

curl "localhost:9200/_aliases?pretty"
10 cls
  • 1,325
  • 2
  • 17
  • 31

6 Answers6

83

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

Andrey Regentov
  • 3,687
  • 4
  • 34
  • 40
Romain
  • 1,203
  • 2
  • 9
  • 10
  • 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 Dec 28 '17 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 Dec 28 '17 at 20:21
  • 6
    You can also change the sort order: `/_cat/indices?v&s=docs.count:desc` – Miles O'Keefe Feb 04 '18 at 05:45
26

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,

Dennis Dyallo
  • 448
  • 6
  • 15
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

Felipe
  • 11,557
  • 7
  • 56
  • 103
  • 1
    Ah, nice. I've changed it slightly to: `curl --silent 'http://path.to.cluster:9200/_cat/aliases' | cut -d ' ' -f1 | sort` – 10 cls Jan 19 '15 at 20:05
  • 5
    I had to change the cut to use `-f3` instead of `-f2`. – Rylander Oct 07 '15 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` – Miae Kim Sep 12 '16 at 21:55
  • How to sort in descending order using the above solution ? – Rupesh Jul 14 '21 at 05: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 Sep 02 '21 at 04:52
  • @Rupesh: `sort --reverse` – F. Pareto Sep 02 '21 at 04:52
10

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

Ben Ahmed Nejib
  • 129
  • 1
  • 3
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
DARK_C0D3R
  • 2,075
  • 16
  • 21
-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.

progrrammer
  • 4,475
  • 2
  • 30
  • 38
  • Ah, I'm interested to see if there's an api alternative. Along with simple re-indexing such as _copy myindex testindex_ ( https://github.com/elasticsearch/elasticsearch/issues/1077 closed? ) there seem to be some unexpected omissions. – 10 cls Jul 25 '14 at 18:25