Files
huangguo_server/common/elastic/elasticInit.go
T
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

40 lines
819 B
Go

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
}