【问题标题】:List all indexes on ElasticSearch server?列出 ElasticSearch 服务器上的所有索引?
【发布时间】:2013-06-29 21:01:05
【问题描述】:

我想列出 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"
}

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

【问题讨论】:

    标签: curl elasticsearch


    【解决方案1】:

    您可以查询localhost:9200/_status,这将为您提供索引列表和有关每个索引的信息。响应将如下所示:

    {
      "ok" : true,
      "_shards" : { ... },
      "indices" : {
        "my_index" : { ... },
        "another_index" : { ... }
      }
    }
    

    【讨论】:

    • 如果您只想知道索引名称列表,那么这种方法太多且速度较慢。更好地使用 - GET /_stats/indexes
    • @asyncwait 我推荐/_stats/indices,因为它是正确的复数形式,也是/_status/_stats 中使用的键。
    • 在 5.6 版上似乎不再是有效的 URL。
    • API 端点已更改为 _nodes/stats_nodes/status @KimberlyW
    • 在 1.2.0 中已弃用。
    【解决方案2】:

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

    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" : { }
        }
      }
    }
    

    【讨论】:

    • @paweloque answer now 看起来这是正确的解决方案;看起来更干净。 curl http://localhost:9200/_stats/indexes\?pretty\=1
    • 我的 2 美分普通(非 json)列表:curl -s localhost:9200/_aliases?pretty=true | awk -F\" '!/aliases/ && $2 != "" {print $2}'
    • 对于 Elasticsearch 6.5,要么点击/stats 端点,要么点击带有参数_cluster/health?level=indices 的健康端点
    • curl localhost:9200/_cat/indices?v 为我工作(在 Elastic 6.2.4 上)
    • 这个答案已经过时了,应该更新
    【解决方案3】:

    _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

    【讨论】:

    • 不起作用:"type": "illegal_argument_exception", "reason": "request [/_stats/indices] contains unrecognized metric: [indices]"
    • @IvanYurchenko 我很久以前就实现了我的弹性搜索插件。 API 很可能从那以后发生了变化,它不再起作用了。最好是使用“_aliases”命令。它还将提供有关 elasticsearch 中所有索引的信息。
    【解决方案4】:

    这是查看数据库中索引的另一种方法:

    curl -sG somehost-dev.example.com:9200/_status --user "credentials:password" | sed 's/,/\n/g' | grep index | grep -v "size_in" | uniq
    
    
    { "index":"tmpdb"}
    
    { "index":"devapp"}
    

    【讨论】:

      【解决方案5】:

      我还建议执行 /_cat/indices ,它可以提供一个很好的人类可读的索引列表。

      【讨论】:

        【解决方案6】:

        我用它来获取所有索引:

        $ 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
        

        要获取上面的第 3 列(索引名称):

        $ 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
        

        【讨论】:

        • curl -s 'http://localhost:9200/_cat/indices?h=index' 将只打印出索引名称。无需使用 shell 技巧来过滤列。
        • 你不仅可以使用 awk,你应该使用 awk(或者在 cut 之前使用 tr -s ' ' 来压缩空格),否则你不会得到如果状态为red,则索引名称,因为它将用空格填充,cut 将每个单独的空格视为分隔一个新字段,即使该“字段”最终为空
        【解决方案7】:

        _stats/indices 给出indices 的结果。

        $ curl -XGET "localhost:9200/_stats/indices?pretty=true"
        {
          "_shards" : {
            "total" : 10,
            "successful" : 5,
            "failed" : 0
          },
          "_all" : {
            "primaries" : { },
            "total" : { }
          },
          "indices" : {
            "visitors" : {
              "primaries" : { },
              "total" : { }
            }
          }
        }
        

        【讨论】:

          【解决方案8】:

          试试

          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
          

          【讨论】:

          • 添加一个管道进行排序可以很容易地看到什么是绿色的。 store.size 的变化也表明了额外的进展。
          • 您还可以选择和排序添加的列,例如&h=health,index 以及 &s=health:desc 排序
          • 使用此命令时要小心,它可能会导致集群崩溃!!!
          • @FlorianCastelain 老实说我现在找不到资源。我已经有一段时间没有尝试处理这个问题了。我记得文档中有一条注释,但我再也看不到它了(也许它在其他资源中)。底线是 _cat API 在集群中触发了一些昂贵的操作。我们有一个非常繁忙的集群,在我们的例子中,当我调用 _cat/indices 时集群崩溃了。 _cat/nodes 也是如此。我们刚刚停止使用它,ES 有很多问题,我们正在更换它。作为记录,我们在 ES 版本 5.6 中遇到了这个问题。
          • @Florian 刚刚看到这条评论:discuss.elastic.co/t/high-cpu-usage-while-bulk-indexing/135023/… 来自旧线程,以防万一
          【解决方案9】:

          这里的人已经回答了如何在 curl 和 sense 中执行此操作,有些人可能需要在 java 中执行此操作。

          来了

          client.admin().indices().stats(new IndicesStatsRequest()).actionGet().getIndices().keySet()
          

          【讨论】:

            【解决方案10】:

            列出索引 + 与 list 一起显示其状态的最佳方法之一是简单地执行以下查询。

            注意:最好使用 Sense 来获得正确的输出。

            curl -XGET 'http://localhost:9200/_cat/shards'
            

            示例输出如下。主要优点是,它基本上显示了索引名称和它保存到的分片,索引大小和分片 ip 等

            index1     0 p STARTED     173650  457.1mb 192.168.0.1 ip-192.168.0.1 
            index1     0 r UNASSIGNED                                                 
            index2     1 p STARTED     173435  456.6mb 192.168.0.1 ip-192.168.0.1 
            index2     1 r UNASSIGNED                                                 
            ...
            ...
            ...
            

            【讨论】:

              【解决方案11】:

              我使用 _stats/indexes 端点获取 json blob 数据,然后使用 jq 进行过滤。

              curl 'localhost:9200/_stats/indexes' | jq '.indices | keys | .[]'
              
              "admin"
              "blazeds"
              "cgi-bin"
              "contacts_v1"
              "flex2gateway"
              "formmail"
              "formmail.pl"
              "gw"
              ...
              

              如果您不想要引号,请在 jq 中添加 -r 标志。

              是的,端点是indexes,数据键是indices,所以他们也拿不定主意:)

              我需要它来清理这些由内部安全扫描 (nessus) 创建的垃圾索引。

              PS。如果您要从命令行与 ES 交互,我强烈建议您熟悉 jq

              【讨论】:

                【解决方案12】:
                <dependency>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                    <version>2.4.0</version>
                </dependency>
                

                Java API

                Settings settings = Settings.settingsBuilder().put("cluster.name", Consts.ES_CLUSTER_NAME).build();
                TransportClient client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("52.43.207.11"), 9300));
                IndicesAdminClient indicesAdminClient = client.admin().indices();
                GetIndexResponse getIndexResponse = indicesAdminClient.getIndex(new GetIndexRequest()).get();
                for (String index : getIndexResponse.getIndices()) {
                    logger.info("[index:" + index + "]");
                }
                

                【讨论】:

                • 您可以为代码提供一些解释,并使答案更具可读性...How to Answer
                【解决方案13】:

                如果您在 Scala 中工作,使用Future 的一种方法是创建一个RequestExecutor,然后使用IndicesStatsRequestBuilder 和管理客户端提交您的请求。

                import org.elasticsearch.action.{ ActionRequestBuilder, ActionListener, ActionResponse }
                import scala.concurrent.{ Future, Promise, blocking }
                
                /** Convenice wrapper for creating RequestExecutors */
                object RequestExecutor {
                    def apply[T <: ActionResponse](): RequestExecutor[T] = {
                        new RequestExecutor[T]
                    }
                }
                
                /** Wrapper to convert an ActionResponse into a scala Future
                 *
                 *  @see http://chris-zen.github.io/software/2015/05/10/elasticsearch-with-scala-and-akka.html
                 */
                class RequestExecutor[T <: ActionResponse] extends ActionListener[T] {
                    private val promise = Promise[T]()
                
                    def onResponse(response: T) {
                        promise.success(response)
                    }
                
                    def onFailure(e: Throwable) {
                        promise.failure(e)
                    }
                
                    def execute[RB <: ActionRequestBuilder[_, T, _, _]](request: RB): Future[T] = {
                        blocking {
                            request.execute(this)
                            promise.future
                        }
                    }
                }
                

                执行器是从this blog post 提取的,如果您尝试以编程方式而不是通过 curl 查询 ES,这绝对是一本好书。如果你有这个,你可以很容易地创建一个所有索引的列表,如下所示:

                def totalCountsByIndexName(): Future[List[(String, Long)]] = {
                    import scala.collection.JavaConverters._
                    val statsRequestBuider = new IndicesStatsRequestBuilder(client.admin().indices())
                    val futureStatResponse = RequestExecutor[IndicesStatsResponse].execute(statsRequestBuider)
                    futureStatResponse.map { indicesStatsResponse =>
                        indicesStatsResponse.getIndices().asScala.map {
                            case (k, indexStats) => {
                                val indexName = indexStats.getIndex()
                                val totalCount = indexStats.getTotal().getDocs().getCount()
                                    (indexName, totalCount)
                                }
                        }.toList
                    }
                }
                

                clientClient 的一个实例,它可以是一个节点或一个传输客户端,这取决于您的需要。您还需要在此请求的范围内有一个隐含的 ExecutionContext。如果您尝试在没有它的情况下编译此代码,那么您将从 scala 编译器收到一条警告,说明如果您还没有导入该代码,您将如何获得它。

                我需要文档计数,但如果您真的只需要索引的名称,您可以从地图的键中提取它们,而不是从 IndexStats

                indicesStatsResponse.getIndices().keySet()
                

                即使您尝试以编程方式执行此操作,当您搜索如何执行此操作时也会出现此问题,因此我希望这对希望在 scala/java 中执行此操作的任何人有所帮助。否则,curl 用户可以按照最上面的答案说的去做并使用

                curl http://localhost:9200/_aliases
                

                【讨论】:

                  【解决方案14】:

                  我会给你可以在 kibana 上运行的查询。

                  GET /_cat/indices?v
                  

                  CURL 版本将是

                  CURL -XGET http://localhost:9200/_cat/indices?v
                  

                  【讨论】:

                  • 我认为这是查看弹性索引的最佳选择。非常感谢:)
                  【解决方案15】:

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

                  【讨论】:

                  • 所有其他端点都不适合我。你的回答奏效了!谢谢。见stackoverflow.com/questions/49204526/…
                  • 我也是,这是一个较新版本的东西。主要答案似乎适用于 2.x 但不适用于 6.x
                  【解决方案16】:

                  您还可以使用

                  获取特定索引
                  curl -X GET "localhost:9200/<INDEX_NAME>"
                  e.g.   curl -X GET "localhost:9200/twitter"
                  You may get output like:
                  {
                    "twitter": {
                       "aliases": { 
                  
                       },
                       "mappings": { 
                  
                       },
                       "settings": {
                       "index": {
                          "creation_date": "1540797250479",
                          "number_of_shards": "3",
                          "number_of_replicas": "2",
                          "uuid": "CHYecky8Q-ijsoJbpXP95w",
                          "version": {
                              "created": "6040299"
                          },
                         "provided_name": "twitter"
                        }
                      }
                    }
                  }
                  

                  更多信息

                  https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html

                  【讨论】:

                    【解决方案17】:

                    我在一台机器上安装了 Kibana 和 ES。但我不知道那台机器上的 ES 节点的详细信息(在什么路径或端口)。

                    那么如何从 Kibana(5.6 版)做到这一点?

                    • 转到开发工具
                    • 查看控制台部分,然后运行以下查询:

                    GET _cat/indices

                    我对查找特定 ES 索引的大小很感兴趣

                    【讨论】:

                      【解决方案18】:

                      获取唯一索引列表的最简单方法是使用上面的答案,并带有 'h=index' 参数:

                      curl -XGET "localhost:9200/_cat/indices?h=index"
                      

                      【讨论】:

                        【解决方案19】:

                        对于 Elasticsearch 6.X,我发现以下内容最有帮助。每个都在响应中提供不同的数据。

                        # more verbose
                        curl -sS 'localhost:9200/_stats' | jq -C ".indices" | less
                        
                        # less verbose, summary
                        curl -sS 'localhost:9200/_cluster/health?level=indices' | jq -C ".indices" | less
                        

                        【讨论】:

                          【解决方案20】:

                          通过 Curl 访问安全的弹性搜索(2020 年更新)

                          如果Elastic Search 是安全的,您可以使用此命令列出索引

                          curl http://username:password@localhost:9200/_aliases?pretty=true
                          

                          【讨论】:

                          • 它对我有用。考虑一下,如果您的密码包含符号字符。在它们之前,您必须插入反斜杠 (\) 例如,如果您的密码是 12@3,您必须输入 12\@3
                          【解决方案21】:

                          如果你的系统上安装了 curl,那么试试这个简单的命令: curl -XGET xx.xx.xx.xx:9200/_cat/indices?v

                          上述命令为您提供以下格式的结果: result to fetch all indices

                          【讨论】:

                            【解决方案22】:

                            使用 kibana 发送请求并获得响应,kibana 可以自动完成弹性查询构建器并拥有更多工具

                            look at the kibana

                             GET /_cat/indices
                            

                            kibana 开发工具

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

                            【讨论】:

                              【解决方案23】:
                              To get all the details in Kibana.
                               GET /_cat/indices
                              
                              
                              
                              
                              To get names only in Kibana.
                              GET /_cat/indices?h=index
                              

                              在不使用 Kibana 的情况下,您可以在 postman 中发送 get 请求或在 Brower 中输入,这样您将获得索引名称列表

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

                              【讨论】:

                                猜你喜欢
                                • 1970-01-01
                                • 2016-11-26
                                • 1970-01-01
                                • 2012-01-27
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                相关资源
                                最近更新 更多