代码拉取完成,页面将自动刷新
/**
* @author huqiuyun
* 数据Elasticsearch
*/
package db
import (
"context"
"gitee.com/h79/goutils/common/debug"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/dao/config"
"gitee.com/h79/goutils/plugins"
"github.com/olivere/elastic/v7"
"go.uber.org/zap"
"time"
)
type esClient struct {
Client *elastic.Client
}
var _ EsDatabase = (*elasticGroup)(nil)
type elasticGroup struct {
selectName string
esMap map[string]*esClient
}
func NewES(confs []config.Elastic) (EsDatabase, error) {
logger.L().Info("Elastic", zap.Any("config", confs))
ess := &elasticGroup{esMap: map[string]*esClient{}}
for _, cfg := range confs {
if len(cfg.Host) <= 0 {
continue
}
interval := cfg.Interval
if interval <= 0 {
interval = 10
} else if interval > 3600 {
interval = 3600
}
interval = interval * time.Second
url := cfg.Host[0]
var client, err = elastic.NewClient(
elastic.SetURL(cfg.Host...),
elastic.SetBasicAuth(cfg.User, cfg.Pwd),
elastic.SetHealthcheckInterval(interval),
elastic.SetSniff(cfg.Sniff),
elastic.SetErrorLog(&esLogger{
LogLevel: logger.ErrorLevel,
Level: cfg.Logger.LogLevel,
}),
elastic.SetInfoLog(&esLogger{
LogLevel: logger.InfoLevel,
Level: cfg.Logger.LogLevel,
}),
elastic.SetTraceLog(&esLogger{
LogLevel: logger.DebugLevel,
Level: cfg.Logger.LogLevel,
}))
if err != nil {
d := debug.NewStack(result.ErrEsClientInternal).
WithDetailFormat("NewElastic(%s) : %v", cfg.Name, err).
WithLevel(debug.DFatalLevel)
_ = plugins.DoWithError(plugins.KAlarm, context.Background(), d)
return nil, err
}
_, _, err = client.Ping(url).Do(context.Background())
if err != nil {
d := debug.NewStack(result.ErrEsPingInternal).
WithDetailFormat("Ping Elastic(%s)", cfg.Name).
WithError(err).
WithLevel(debug.DFatalLevel)
_ = plugins.DoWithError(plugins.KAlarm, context.Background(), d)
return nil, err
}
ver, err := client.ElasticsearchVersion(url)
logger.L().Info("Elastic",
zap.String("Name", cfg.Name),
zap.String("Version", ver))
ess.esMap[cfg.Name] = &esClient{client}
}
return ess, nil
}
func (conns *elasticGroup) Get(name string) (*elastic.Client, error) {
if len(name) == 0 {
name = conns.selectName
}
es, exists := conns.esMap[name]
if exists == true {
return es.Client, nil
}
return nil, result.Error(result.ErrNotFound, "Not found")
}
func (conns *elasticGroup) Do(name string, call func(es *elastic.Client) error) error {
if len(name) == 0 {
name = conns.selectName
}
if es, exists := conns.esMap[name]; exists == true {
return call(es.Client)
}
return result.Error(result.ErrNotFound, "Not found")
}
func (conns *elasticGroup) Close(name string) error {
if len(name) == 0 {
name = conns.selectName
}
es, exists := conns.esMap[name]
if exists == true {
es.Client.Stop()
delete(conns.esMap, name)
}
return nil
}
func (conns *elasticGroup) CloseAll() {
for _, es := range conns.esMap {
es.Client.Stop()
}
conns.esMap = nil
}
func (conns *elasticGroup) Select(name string) {
conns.selectName = name
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。