简体   繁体   English

如何使用 golang 客户端库从 Elasticsearch 获取所有索引名称?

[英]How can I get all index name from Elasticsearch by using golang client library?

I am using this library in go https://pkg.go.dev/github.com/elastic/go-elasticsearch/esapi#CatIndicesRequest to query from Elasticsearch.我在 go https://pkg.go.dev/github.com/elastic/go-elasticsearch/esapi#CatIndicesRequest中使用这个库从 Elasticsearch 查询。

I has some examples on querying but I am looking for a API method to get all index from Elasticsearch cluster.我有一些查询示例,但我正在寻找一种 API 方法来从 Elasticsearch 集群获取所有索引。 But I can't find one I can use from their doc.但我无法从他们的文档中找到我可以使用的。 Does anyone know what the best way to get all index?有谁知道获取所有索引的最佳方法是什么? like the http api _cat/indices像 http api _cat/indices

Here's a working example:这是一个工作示例:

package main

import (
    "context"
    "fmt"

    "github.com/elastic/go-elasticsearch/v7"
    "github.com/elastic/go-elasticsearch/v7/esapi"
)

func main() {

    cfg := elasticsearch.Config{
        Addresses: []string{
            "http://localhost:9243",
        },
        Username: "foo",
        Password: "bar",
    }
    es, err := elasticsearch.NewClient(cfg)
    if err != nil {
        panic(err)
    }
    res, err := esapi.CatIndicesRequest{Format: "json"}.Do(context.Background(), es)
    if err != nil {
        return
    }
    defer res.Body.Close()

    fmt.Println(res.String())
}

You can tweak the CatIndicesRequest to format the output for your case.您可以调整CatIndicesRequest以根据您的情况格式化 output。 For example, if you use CatIndicesRequest{Pretty: true, Human: true} .例如,如果您使用CatIndicesRequest{Pretty: true, Human: true} It will return something like this:它会返回这样的东西:

[200 OK] green open .ent-search-actastic-workplace_search_accounts_v16                                                                           yvaDvj9RTMOoWqIpKdC_kw 1 1       1      0  12.1kb     6kb
green open .ent-search-workplace-search-content-events-ecs-ilm-logs-production-2022.01.23-000003                                        1D1BagTFQ6ypoZh2RdoUhQ 1 1       0      0    416b    208b
green open .ent-search-actastic-workplace_search_search_groups_v4-name-unique-constraint                                                b_FRbLWJQfqXXxyTdcU2cQ 1 1       1      0     7kb   3.5kb
green open .ent-search-actastic-crawler_crawl_requests_v4                                                                               kaUWb7YlTEeFH-Gcpz50qA 1 1       0      0    416b    208b
green open .ent-search-api-ecs-ilm-logs-production-2022.03.09-000016                                                                    EKZZOtqOR_e8pOztXsLU1g 1 1       0      0    416b    208b

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Golang:使用名为Goes的ElasticSearch库,如何为bool应该方法编写可执行代码? - Golang: Using ElasticSearch library called Goes, how can I write executable code for bool should method? 如何使用 golang 获取特定网站上的卖家名称? - How can I get the name of a seller on a specific website using golang? 如何使用“go-elasticsearch”库在 Elasticsearch 中创建索引? - How can I create a index in Elasticsearch with `go-elasticsearch` library? 如何显示使用Golang客户端库从Kubernetes中运行的所有Pod中捕获的Prometheus中的自定义应用程序指标 - How to show custom application metrics in Prometheus captured using the golang client library from all pods running in Kubernetes 如何使用 golang 获取 Windows 上所有驱动器的列表? - How can I get a listing of all drives on Windows using golang? 如何从 Golang 客户端创建 ElasticSearch 策略 - How to create ElasticSearch policy from Golang client 我如何从 docker golang 客户端获取 kube.netes 创建的容器信息作为 types.StatsJSON? - How can i get the info of container created by kubernetes as types.StatsJSON from docker golang client? 如何从 golang 中的 .ttf 文件中获取字体系列名称? - How can I get the font family name from a .ttf file in golang? 如何在Golang中的包下获取所有结构? - How can i get all struct under a package in Golang? Redigo:如何使用 Golang 从 Redis 获取键值 map? - Redigo: How can I get a key-value map from Redis using Golang?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM