| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- package main
- import (
- "context"
- "fmt"
- "log"
- "github.com/olivere/elastic/v7"
- )
- func main() {
- // 创建一个新的 Elasticsearch 客户端
- client, err := elastic.NewClient(
- elastic.SetURL("http://elasticsearch-o-00g0ootdnc99.escloud.volces.com:9200"),
- elastic.SetBasicAuth("admin", "F37ZMTyjwTNuQC"), // 设置用户名和密码
- //elastic.SetURL("https://410bb6fe.r11.cpolar.top"),
- //elastic.SetBasicAuth("elastic", "123456"), // 设置用户名和密码
- )
- if err != nil {
- log.Fatalf("Error creating the client: %s", err)
- }
- // 检查 Elasticsearch 版本
- info, code, err := client.Ping("http://elasticsearch-o-00g0ootdnc99.escloud.volces.com:9200").Do(context.Background())
- if err != nil {
- log.Fatalf("Error connecting to Elasticsearch: %s", err)
- }
- fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
- // 获取所有索引
- indices, err := client.CatIndices().Do(context.Background())
- if err != nil {
- log.Fatalf("Error getting the indices: %s", err)
- }
- // 打印所有索引
- for _, index := range indices {
- fmt.Printf("Index: %s\n", index.Index)
- }
- }
|