Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch: get a list of indexes

Is it possible to get a list of indexes that match a certain pattern e.g

this is how to get a list of indexes:

curl -XGET 'localhost:9200/_stats/' 

but I couldn't find a way of filter them so that this list would only include only indexes witch match "my_index_nr_1*" where "*" would be a wild card

Solution

After using ES for quite a while here is what I use now, hope it will help someone else:

curl -XGET '/_cat/indices/my_index_nr_1*' 

You can also add ?v at the end which will give you headers of each column in result.

like image 991
Thezeus Avatar asked Jun 23 '14 17:06

Thezeus


People also ask

How do I list all indexes in Elasticsearch?

You can query localhost:9200/_status and that will give you a list of indices and information about each. The response will look something like this: { "ok" : true, "_shards" : { ... }, "indices" : { "my_index" : { ... }, "another_index" : { ... } } }

Where are indexes stored in Elasticsearch?

Indexes are stored on disk as configured in elasticsearch. yml with the configuration option path. data ; localhost on port 9200 is the default connection port for the HTTP REST interface, the path of the url generally defines an action to be taken (like searching for documents);

How do I get Elasticsearch index in Python?

You'll need to use Python's PIP3 package manager to install the Elasticsearch low-level client for Python. Use the pip3 -V command to check if PIP is installed, and you can input pip3 list to see which packages are already installed.


2 Answers

There is a neat trick using the _aliases command that when combined with a wildcard (my_index_nr_1* below) will only show you matching index names and associated indexes:

curl -XGET 'http://localhost:9200/my_index_nr_1*/_aliases?pretty' 

The result I get is:

{   "my_index_nr_1_test" : {     "aliases" : { }   } } 

Very helpful when you have a lot of indexes on a cluster but don't want to see all the other stats information.

like image 55
John Petrone Avatar answered Oct 19 '22 11:10

John Petrone


For humans, the best answer is the modified summary:

curl -XGET localhost:9200/_cat/indices/my_index_nr_1*?v 

For machines, the best answer is likely a variation of (?pretty is there for you to see its output):

curl -XGET localhost:9200/my_index_nr_1*/_settings?pretty 

This will get the list of all indices that match, with their settings. The _aliases answer above is just a variation of this request. You can even trim the request down to:

curl -XGET localhost:9200/my_index_nr_1*?pretty 

However, this will respond with both the settings, aliases, and mappings of each index.

like image 29
pickypg Avatar answered Oct 19 '22 10:10

pickypg