How to get a list of all indexes in python-elasticsearch

后端 未结 7 1881
北海茫月
北海茫月 2021-02-06 20:32

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


        
相关标签:
7条回答
  • 2021-02-06 21:07

    how to get a list of all indexes in this cluster?

    Use the wildcard. Works with elasticsearch-py.

    for index in es.indices.get('*'):
      print index
    
    0 讨论(0)
  • 2021-02-06 21:11

    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.

    0 讨论(0)
  • 2021-02-06 21:11

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

    requests.get(full_elastic_url + "/_mapping")
    
    0 讨论(0)
  • 2021-02-06 21:11

    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

    0 讨论(0)
  • 2021-02-06 21:16

    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']
    
    0 讨论(0)
  • 2021-02-06 21:16

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

    0 讨论(0)
提交回复
热议问题