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

问题描述 投票:199回答:23

我想列出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
23个回答
344
投票

有关群集中所有索引的简明列表,请致电

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

2
投票

_stats/indicesindices给出了结果。

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

2
投票

这里的人已经回答了如何在卷曲和感觉上做到这一点,有些人可能需要在java中这样做。

在这里

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

2
投票

试试这个猫API:它将为您提供所有具有健康和其他详细信息的索引的列表。

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


1
投票

我使用_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


1
投票
<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 + "]");
}

1
投票

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

那你怎么能从Kibana(5.6版)做到这一点?

  • 转到开发工具
  • 请参阅控制台部分,并运行以下查询:

GET _cat/indices

我有兴趣找到特定ES索引的大小


1
投票
You may use this command line.

curl -X GET“localhost:9200 / _cat / indices?v”

For more(Elasticsearch官方网站)


0
投票

这是另一种只看到db中的索引的方法:

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

0
投票

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

注意:最好使用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                                                 
...
...
...

0
投票

如果您在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解除,如果你试图以编程方式而不是通过卷曲来查询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

56
投票

尝试

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

0
投票
You can also get specific index using 

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"
      }
    }
  }
}
For more info [https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html][1]

0
投票

你可以尝试这个命令

卷曲-X GET http://localhost:9200/_cat/indices?v


0
投票

对于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

0
投票

要列出您可以执行的索引:curl'localhost:9200 / _cat / indices?v'Elasticsearch Documentation


31
投票

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

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

26
投票

_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


15
投票

我用它来获取所有索引:

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

有了这个列表,你可以继续......

Example

$ 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

Column Headers

您还可以使用?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

10
投票

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


7
投票

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

4
投票

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

GET /_cat/indices?v

而CURL版本将是

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

4
投票

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

curl -XGET "localhost:9200/_cat/indices?h=index"
© www.soinside.com 2019 - 2024. All rights reserved.