Skip to main content

Go Client

安装

可以安装go客户端的7.x版本(兼容纳速云Elasticsearch Serverless),请将软件包添加到go.mod文件中。

require github.com/elastic/go-elasticsearch/v7 7.16

连接示例

package main

import (
"log"
"github.com/elastic/go-elasticsearch/v7"
)


func main() {
cfg := elasticsearch.Config{
Addresses: []string{
"https://router.nasuyun.com:9200",
},
Username: <your_app_username>,
Password: <your_app_password>,
}

es, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}

res, err := es.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}

defer res.Body.Close()
log.Println(res)
}

func HttpExample(w http.ResponseWriter, r *http.Request) {
... # Client usage
}