package elastic import ( "errors" "91porn-server/common/log" "github.com/elastic/go-elasticsearch/v8" ) var esClient *Client // Client 获取elastic客户端 func InitElastic(opt Options) (*Client, error) { if len(opt.Address) == 0 { return nil, errors.New("elasticSearch address is empty") } cfg := elasticsearch.Config{ Addresses: opt.Address, Username: opt.UserName, Password: opt.PassWord, } es, err := elasticsearch.NewClient(cfg) if err != nil { log.Error("create elasticSearch client occour error", log.E(err)) return nil, err } esClient = &Client{Client: es} if err = esClient.Ping(); err != nil { log.Warn("elasticSarch ping failed", log.E(err)) return nil, err } log.Info("init elasticSearch successful") return esClient, nil } func Init() *Client { return esClient }