列出ElasticSearch服务器上的所有索引?

398

我想列出ElasticSearch服务器上存在的所有索引。我尝试了这个:

curl -XGET localhost:9200/

但它只是给我这个:

{
  "ok" : true,
  "status" : 200,
  "name" : "El Aguila",
  "version" : {
    "number" : "0.19.3",
    "snapshot_build" : false
  },
  "tagline" : "You Know, for Search"
}

我想要一个所有索引的列表。

23个回答

614

要获取群集中所有索引的简洁列表,请调用

curl http://localhost:9200/_aliases

这将为您提供一个索引及其别名列表。

如果您想要漂亮格式化,添加pretty=true

curl http://localhost:9200/_aliases?pretty=true

如果你的索引被称为old_deuteronomymungojerrie,那么结果将类似于这样:

{
  "old_deuteronomy" : {
    "aliases" : { }
  },
  "mungojerrie" : {
    "aliases" : {
      "rumpleteazer" : { },
      "that_horrible_cat" : { }
    }
  }
}

8
@paweloque的答案现在看起来像是正确的解决方案;看起来更加简洁。curl http://localhost:9200/_stats/indexes\?pretty\=1 - notapatch
1
对于一个普通(非JSON)列表,我的建议是:curl -s localhost:9200/_aliases?pretty=true | awk -F\" '!/aliases/ && $2 != "" {print $2}' - Yaron
1
对于 Elasticsearch 6.5,请使用 /stats 端点或带有参数 _cluster/health?level=indices 的健康端点。 - Justin W.
1
curl http://localhost:9200/_cat/indices?v 对我有效(在 Elastic 6.2.4 上)。 - Matt L.
这个答案已经过时了,需要更新。 - ThangLeQuoc
3
我不知道哪些内容已经过时,但在2022年的LTS ECK上,它完全满足了我的需求。 - anVzdGFub3RoZXJodW1hbg

182
curl 'localhost:9200/_cat/indices?v'
它将以表格形式呈现下面易于理解的输出。
health index    pri rep docs.count docs.deleted store.size pri.store.size
yellow customer   5   1          0            0       495b           495b

通过添加管道到 sort 命令,很容易看到哪些正在进行中。此外,store.size 的更改表示有额外的进展。 - kevpie
您可以通过添加 &h=health,index 来选择和排序列,也可以使用 &s=health:desc 进行排序。 - Georg Engel
2
@FlorianCastelain 说实话,我现在找不到资源了。自从我尝试解决这个问题以来已经过了一段时间。我记得文档中有一个注释,但我再也找不到它了(也许是在其他资源中)。最重要的是,_cat API会在集群中触发一些昂贵的操作。我们的集群非常繁忙,在我们的情况下,当我调用_cat/indices时,集群会崩溃。_cat/nodes也是如此。我们停止使用它,我们在解决ES的许多问题,并正在替换它的过程中。记录一下,我们在ES版本5.6中遇到了这个问题。 - babis21
1
@Florian 刚刚看到这条评论:https://discuss.elastic.co/t/high-cpu-usage-while-bulk-indexing/135023/7,如果有帮助的话 - babis21
这是当前的正确答案。如果您完全按照上面的curl命令使用,记住curl默认为http。根据您的配置,您可能需要使用curl 'https://localhost:9200/_cat/indices?v' - Matthew Thomas
显示剩余2条评论

39
您可以查询 localhost:9200/_status,这将为您提供索引列表以及每个索引的信息。响应将类似于以下内容:
{
  "ok" : true,
  "_shards" : { ... },
  "indices" : {
    "my_index" : { ... },
    "another_index" : { ... }
  }
}

3
如果你只是想知道索引名称列表,那么这个方法过于繁琐且速度较慢。最好使用 GET /_stats/indexes - asyncwait
6
我建议使用 /_stats/indices,因为它是正确的复数形式,也是在 /_status/_stats 中使用的键。 - Nicholas Shanks
8
在5.6版本中,这个URL似乎不再有效。 - The Unknown Dev
6
API终端点已更改为_nodes/stats_nodes/status - maxymoo
1
已弃用于1.2.0版本。 - jarmod
在v.7中,“invalid_index_name_exception” - mike rodent

32
_stats命令提供了通过指定所需度量标准来自定义结果的方法。要获取查询的索引,可以使用以下查询语句:
GET /_stats/indices

_stats查询的通用格式如下:

/_stats
/_stats/{metric}
/_stats/{metric}/{indexMetric}
/{index}/_stats
/{index}/_stats/{metric}

度量标准在哪里:

indices, docs, store, indexing, search, get, merge, 
refresh, flush, warmer, filter_cache, id_cache, 
percolate, segments, fielddata, completion

为了提升自我能力,我编写了一个小型的elasticsearch插件,可以列出elasticsearch索引但没有其他信息。您可以在以下网址找到它:

http://blog.iterativ.ch/2014/04/11/listindices-writing-your-first-elasticsearch-java-plugin/

https://github.com/iterativ/elasticsearch-listindices


11
无效:"type": "illegal_argument_exception", "reason": "请求[/_stats/indices]包含无法识别的指标:[indices]" - Ivan Yurchenko
2
@IvanYurchenko 我很久以前实现了我的elasticsearch插件。由于API可能已经发生了变化,因此它可能不再起作用。最好使用“_aliases”命令。它还将提供有关elasticsearch中所有索引的信息。 - paweloque
非法参数异常 - Asher

30

我使用这个来获取所有索引:

$ curl --silent 'http://127.0.0.1:9200/_cat/indices' | cut -d\  -f3

通过这个列表,您可以处理...

示例

$ curl -s 'http://localhost:9200/_cat/indices' | head -5
green open qa-abcdefq_1458925279526           1 6       0     0   1008b    144b
green open qa-test_learnq_1460483735129    1 6       0     0   1008b    144b
green open qa-testimportd_1458925361399       1 6       0     0   1008b    144b
green open qa-test123p_reports                1 6 3868280 25605   5.9gb 870.5mb
green open qa-dan050216p_1462220967543        1 6       0     0   1008b    144b
获取上面第三列(指数名称)的方法:
$ curl -s 'http://localhost:9200/_cat/indices' | head -5 | cut -d\  -f3
qa-abcdefq_1458925279526
qa-test_learnq_1460483735129
qa-testimportd_1458925361399
qa-test123p_reports
qa-dan050216p_1462220967543

注意: 你也可以使用awk '{print $3}'代替cut -d\ -f3

列头

你也可以在查询后面加上?v来添加列头,这样做会破坏cut...方法,因此建议此时使用awk..选择。

$ curl -s 'http://localhost:9200/_cat/indices?v' | head -5
health status index                              pri rep docs.count docs.deleted store.size pri.store.size
green  open   qa-abcdefq_1458925279526             1   6          0            0      1008b           144b
green  open   qa-test_learnq_1460483735129      1   6          0            0      1008b           144b
green  open   qa-testimportd_1458925361399         1   6          0            0      1008b           144b
green  open   qa-test123p_reports                  1   6    3868280        25605      5.9gb        870.5mb

2
curl -s 'http://localhost:9200/_cat/indices?h=index' 将仅打印出索引名称。无需使用 shell 技巧来过滤列。 - hgf
不仅可以使用awk,而且应该使用awk(否则在使用cut之前使用tr -s' '来压缩空格运行),否则如果状态为“red”,您将无法获取索引名称,因为它将填充空格,并且cut即使每个空格作为分隔符分隔一个新字段,即使该“字段”最终为空。 - kbolino

29
最简单的方法是使用上面的答案,并加上'h=index'参数来获取仅索引列表:
curl -XGET "localhost:9200/_cat/indices?h=index"

19
To get all the details in Kibana.
 GET /_cat/indices




To get names only in Kibana.
GET /_cat/indices?h=index

不使用Kibana,您可以在Postman中发送GET请求或在浏览器中键入此内容,以获取索引名称列表。

http://localhost:9200/_cat/indices?h=index

图片描述 图片描述


16
我建议使用 /_cat/indices 命令,它可以给出您索引的易读列表。

13

curl -XGET 'http://localhost:9200/_cluster/health?level=indices'

这将会输出类似下面的内容

{
  "cluster_name": "XXXXXX:name",
  "status": "green",
  "timed_out": false,
  "number_of_nodes": 3,
  "number_of_data_nodes": 3,
  "active_primary_shards": 199,
  "active_shards": 398,
  "relocating_shards": 0,
  "initializing_shards": 0,
  "unassigned_shards": 0,
  "delayed_unassigned_shards": 0,
  "number_of_pending_tasks": 0,
  "number_of_in_flight_fetch": 0,
  "task_max_waiting_in_queue_millis": 0,
  "active_shards_percent_as_number": 100,
  "indices": {
    "logstash-2017.06.19": {
      "status": "green",
      "number_of_shards": 3,
      "number_of_replicas": 1,
      "active_primary_shards": 3,
      "active_shards": 6,
      "relocating_shards": 0,
      "initializing_shards": 0,
      "unassigned_shards": 0
    },
    "logstash-2017.06.18": {
      "status": "green",
      "number_of_shards": 3,
      "number_of_replicas": 1,
      "active_primary_shards": 3,
      "active_shards": 6,
      "relocating_shards": 0,
      "initializing_shards": 0,
      "unassigned_shards": 0
    }}

所有其他端点对我都不起作用,你的答案有效!谢谢。请参见https://stackoverflow.com/questions/49204526/cat-indices-gives-all-not-found - arun
我也是,这是新版本的问题吗?主要答案似乎适用于2.x但不适用于6.x。 - Andrew Jon Dodds

8

使用Kibana发送请求并获取响应,Kibana可以自动完成Elasticsearch查询构建,并提供更多工具。

查看Kibana

 GET /_cat/indices

Kibana开发工具

http://localhost:5601/app/kibana#/dev_tools/console


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,