如何获取python-elasticsearch中所有索引的列表

时间:2015-08-10 20:26:18

标签: python elasticsearch

如何在Python中获取索引名称列表?以下是我到目前为止的情况:

>>> 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?

8 个答案:

答案 0 :(得分:27)

  

如何获取此群集中所有索引的列表?

使用通配符。适用于elasticsearch-py。

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

答案 1 :(得分:24)

在使用aliases库搜索有关检索python-elasticsearch的信息时,会出现此问题。接受的答案是使用get_aliases,但该方法已被删除(截至2017年)。要获得aliases,您可以使用以下内容:

 es.indices.get_alias("*")

答案 2 :(得分:22)

以下是使用get_alias()方法执行此操作的一种方法:

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

答案 3 :(得分:3)

如果您愿意使用pyelasticsearch module,则他们支持GET _mapping命令,该命令会生成群集的架构。这将允许您查看索引,并深入查看每个索引以查看doc_types及其字段等。以下是一个示例:

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

仅获取索引列表

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 question

有关

答案 4 :(得分:2)

我使用curl来调用stats API并获取有关索引的信息。然后我解析返回的JSON对象以查找索引名称。

curl localhost:9200/_stats

在Python中,您可以使用请求库调用curl。我不知道使用Elasticsearch或Elasticsearch-DSL Python库的方法。

答案 5 :(得分:2)

您可以通过执行类似的操作来获取_mapping以获取所有索引的列表。

requests.get(full_elastic_url + "/_mapping")

答案 6 :(得分:0)

您可以使用Cat API:es.cat.indices(h='index', s='index').split()

答案 7 :(得分:0)

如果您想要“别名”而不是“索引名称”,这里是完美的解决方案:

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

alias_names 中,我们获得特定索引上的别名列表。

相关问题