88

How would I get a list of the names of an index in Python? Here is what I have so far:

>>> es=e.es
>>> es
<Elasticsearch([{'host': '14555f777d8097.us-east-1.aws.found.io', 'port': 9200}])>
>>> es.indices
<elasticsearch.client.indices.IndicesClient object at 0x10de86790>
# how to get a list of all indexes in this cluster?
1
  • 1
    Have you tried the get_aliases() method? According to this example, you should be able to use a wildcard * to get all indexes. Commented Aug 10, 2015 at 20:33

9 Answers 9

106

This question comes up when searching for information on retrieving aliases using the python-elasticsearch library. The accepted answer says to use get_aliases but that method has been removed (as of 2017). To get aliases, you can use the following:

 es.indices.get_alias("*")

UPDATE

The latest usage should be with a keyword arg:

es.indices.get_alias(index="*")
Sign up to request clarification or add additional context in comments.

1 Comment

This now raises a DeprecationWarning (see github.com/elastic/elasticsearch-py/issues/1698). I believe that the correct way to use it is now: ` es.indices.get_alias(index="*")`.
52

To get a list of all the indexes in a cluster, use a wildcard.

This works with elasticsearch-py.

# Python 2
for index in es.indices.get('*'):
  print index

# Python 3
for index in es.indices.get('*'):
  print(index)

1 Comment

raise TypeError( TypeError: Positional arguments can't be used with Elasticsearch API methods. Instead only use keyword arguments.
43

Here is one way to do it with the get_alias() method:

>>> indices=es.indices.get_alias().keys()
>>> sorted(indices)
[u'avails', u'hey', u'kibana-int']

5 Comments

Any idea why this doesn't work? res = es.search() sorted(set(res["hits"]["hits"][k]["_index"] for k in xrange(len(res["hits"]["hits"]))))
Looks like this method has been removed from recent versions of python-elasticsearch (2017).
This answer is not valid for es5.x onwards
it is actually get_alias not get_aliases() ex: es_client.indices.get_alias().keys() api changed probably since then
I personally find indices_dict = es.indices.get_alias() just easier to interpret on the fly
4

You can use the Cat API:es.cat.indices(h='index', s='index').split()

Comments

3

If you are willing to use pyelasticsearch module they have support for the GET _mapping command, which produces the schema of the cluster. This will allow you to see the indices, and drill into each index to see doc_types, and their fields, etc. Here's an example:

import pyelasticsearch as pyes
es = pyes.ElasticSearch(["http://hostname0:9200", "http://hostname1:9200"]) ## don't accidentally type Elasticsearch, the class from the other two modules
schema = es.get_mapping() ## python dict with the map of the cluster

To get just the list of indices,

indices_full_list = schema.keys()
just_indices = [index for index in indices_full_list if not index.startswith(".")] ## remove the objects created by marvel, e.g. ".marvel-date"

This is related to this question

Comments

2

I use curl to call the stats API and get information about the indices. Then I parse the JSON object that is returned to find the index names.

curl localhost:9200/_stats

In Python you can call curl using the requests library. I don't know of a way to do this using the Elasticsearch or Elasticsearch-DSL Python library.

1 Comment

For some reason */_stats wasn't working for me, but */_mapping did work.
2

You can get _mapping to get list of all indexes by doing something like that.

requests.get(full_elastic_url + "/_mapping")

Comments

1

If you want 'alias name' and not 'index name', here the perfect solution:

response = es.indices.get(indexname)
alias_names = list(response[indexname]['aliases'].keys())

In alias_names we get list of alias names on a particular index.

Comments

1

_cat API seems the right way to do this, since the _aliases way of doing will soon be removed by elasticsearch since it exposes the system indices.

indices = es.cat.indices(h='index', s='index').split()

It did the job for me.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.