如何获取python-elasticsearch中所有索引的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31928506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a list of all indexes in python-elasticsearch
提问by David542
How would I get a list of the names of an index in Python? Here is what I have so far:
我如何获得 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?
采纳答案by erewok
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:
在搜索有关aliases
使用python-elasticsearch
库检索的信息时会出现此问题。接受的答案说要使用,get_aliases
但该方法已被删除(截至 2017 年)。要获取aliases
,您可以使用以下方法:
es.indices.get_alias("*")
回答by MauricioRoman
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 调用 stats API 并获取有关索引的信息。然后我解析返回的 JSON 对象以查找索引名称。
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.
在 Python 中,您可以使用 requests 库调用 curl。我不知道有什么方法可以使用 Elasticsearch 或 Elasticsearch-DSL Python 库来做到这一点。
回答by David542
Here is one way to do it with the get_alias()
method:
这是使用该get_alias()
方法执行此操作的一种方法:
>>> indices=es.indices.get_alias().keys()
>>> sorted(indices)
[u'avails', u'hey', u'kibana-int']
回答by Ahmed
You can get _mapping to get list of all indexes by doing something like that.
您可以通过执行类似操作来获取 _mapping 以获取所有索引的列表。
requests.get(full_elastic_url + "/_mapping")
回答by travelingbones
If you are willing to use pyelasticsearch modulethey 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:
如果您愿意使用pyelasticsearch 模块,它们会支持该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
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
这与这个问题有关
回答by pascal
how to get a list of all indexes in this cluster?
如何获取该集群中所有索引的列表?
Use the wildcard. Works with elasticsearch-py.
使用通配符。与 elasticsearch-py 一起使用。
for index in es.indices.get('*'):
print index