你可以使用golang客户端库来获取Elasticsearch中的所有索引名称。

huangapple go评论127阅读模式
英文:

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

问题

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

我有一些关于查询的示例,但我正在寻找一种从Elasticsearch集群获取所有索引的API方法。但我在他们的文档中找不到可以使用的方法。有人知道获取所有索引的最佳方法吗?就像http API _cat/indices一样。

英文:

I am using this library in go https://pkg.go.dev/github.com/elastic/go-elasticsearch/esapi#CatIndicesRequest to query from Elasticsearch.

I has some examples on querying but I am looking for a API method to get all index from Elasticsearch cluster. 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

答案1

得分: 2

这是一个可工作的示例:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/elastic/go-elasticsearch/v7"
  6. "github.com/elastic/go-elasticsearch/v7/esapi"
  7. )
  8. func main() {
  9. cfg := elasticsearch.Config{
  10. Addresses: []string{
  11. "http://localhost:9243",
  12. },
  13. Username: "foo",
  14. Password: "bar",
  15. }
  16. es, err := elasticsearch.NewClient(cfg)
  17. if err != nil {
  18. panic(err)
  19. }
  20. res, err := esapi.CatIndicesRequest{Format: "json"}.Do(context.Background(), es)
  21. if err != nil {
  22. return
  23. }
  24. defer res.Body.Close()
  25. fmt.Println(res.String())
  26. }

您可以调整CatIndicesRequest以适应您的情况的输出格式。
例如,如果您使用CatIndicesRequest{Pretty: true, Human: true},它将返回类似以下的内容:

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

Here's a working example:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/elastic/go-elasticsearch/v7"
  6. "github.com/elastic/go-elasticsearch/v7/esapi"
  7. )
  8. func main() {
  9. cfg := elasticsearch.Config{
  10. Addresses: []string{
  11. "http://localhost:9243",
  12. },
  13. Username: "foo",
  14. Password: "bar",
  15. }
  16. es, err := elasticsearch.NewClient(cfg)
  17. if err != nil {
  18. panic(err)
  19. }
  20. res, err := esapi.CatIndicesRequest{Format: "json"}.Do(context.Background(), es)
  21. if err != nil {
  22. return
  23. }
  24. defer res.Body.Close()
  25. fmt.Println(res.String())
  26. }

You can tweak the CatIndicesRequest to format the output for your case.
For example, if you use CatIndicesRequest{Pretty: true, Human: true}. It will return something like this:

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

huangapple
  • 本文由 发表于 2022年3月23日 15:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/71583148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定