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

我在 go https://pkg.go.dev/github.com/elastic/go-elasticsearch/esapi#CatIndicesRequest中使用这个库从 Elasticsearch 查询。

我有一些查询示例,但我正在寻找一种 API 方法来从 Elasticsearch 集群获取所有索引。但我无法从他们的文档中找到我可以使用的。有谁知道获取所有索引的最佳方法是什么?像 http api_cat/indices


守着星空守着你
浏览 246回答 1
1回答

慕妹3146593

这是一个工作示例:package mainimport (    "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())}您可以调整CatIndicesRequest以格式化您的案例的输出。例如,如果您使用CatIndicesRequest{Pretty: true, Human: true}. 它会返回这样的东西:[200 OK] green open .ent-search-actastic-workplace_search_accounts_v16                                                                           yvaDvj9RTMOoWqIpKdC_kw 1 1       1      0  12.1kb     6kbgreen open .ent-search-workplace-search-content-events-ecs-ilm-logs-production-2022.01.23-000003                                        1D1BagTFQ6ypoZh2RdoUhQ 1 1       0      0    416b    208bgreen open .ent-search-actastic-workplace_search_search_groups_v4-name-unique-constraint                                                b_FRbLWJQfqXXxyTdcU2cQ 1 1       1      0     7kb   3.5kbgreen open .ent-search-actastic-crawler_crawl_requests_v4                                                                               kaUWb7YlTEeFH-Gcpz50qA 1 1       0      0    416b    208bgreen open .ent-search-api-ecs-ilm-logs-production-2022.03.09-000016                                                                    EKZZOtqOR_e8pOztXsLU1g 1 1       0      0    416b    208b
打开App,查看更多内容
随时随地看视频慕课网APP