Elasticsearch list indices sorted by name
Asked Answered
B

6

43

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

curl "localhost:9200/_aliases?pretty"
Burnejones answered 25/7, 2014 at 17:5 Comment(5)
Can't you sort by your own? I don't think it exists.Dykes
@ErBnAcharya The result has no field name to specify.Burnejones
@ErBnAcharya How is that done?Burnejones
have you tried curl localhost:9200/_cat/aliases | sort? I realize it's a completely different format, but it might be what you really wantedLawrencelawrencium
I recommend to use sort as @Lawrencelawrencium said. Try curl localhost:9200/_cat/indices | sort -nk2Felicitasfelicitate
B
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

Bret answered 19/1, 2015 at 19:28 Comment(6)
Ah, nice. I've changed it slightly to: curl --silent 'http://path.to.cluster:9200/_cat/aliases' | cut -d ' ' -f1 | sortBurnejones
I had to change the cut to use -f3 instead of -f2.Smallsword
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 3Felicitasfelicitate
How to sort in descending order using the above solution ?Valerie
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 }' | sortStratus
@Rupesh: sort --reverseStratus
J
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

Jocularity answered 4/12, 2017 at 10:0 Comment(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 languageJocularity
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]"Sweetscented
You can also change the sort order: /_cat/indices?v&s=docs.count:descColly
S
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,

Shabbir answered 4/1, 2018 at 16:41 Comment(2)
Plus +1 hands up for reverse orderingAmaranthine
This answer tells about so many features in a very clean fashion.Sachs
B
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

Bret answered 19/1, 2015 at 19:28 Comment(6)
Ah, nice. I've changed it slightly to: curl --silent 'http://path.to.cluster:9200/_cat/aliases' | cut -d ' ' -f1 | sortBurnejones
I had to change the cut to use -f3 instead of -f2.Smallsword
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 3Felicitasfelicitate
How to sort in descending order using the above solution ?Valerie
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 }' | sortStratus
@Rupesh: sort --reverseStratus
H
14

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

Howitzer answered 14/7, 2020 at 1:23 Comment(0)
C
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
Contrapose answered 28/4, 2020 at 5:50 Comment(0)
D
-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.

Dykes answered 25/7, 2014 at 18:1 Comment(1)
Ah, I'm interested to see if there's an api alternative. Along with simple re-indexing such as copy myindex testindex ( github.com/elasticsearch/elasticsearch/issues/1077 closed? ) there seem to be some unexpected omissions.Burnejones

© 2022 - 2024 — McMap. All rights reserved.