feat:完善网站信息

This commit is contained in:
henry
2021-12-22 14:11:14 +08:00
parent b0a9ef3569
commit 332f67d1c1
14 changed files with 637 additions and 28 deletions

View File

@ -1,20 +1,22 @@
package es
import (
"github.com/elastic/go-elasticsearch/v7"
"context"
"github.com/olivere/elastic/v7"
)
type Instance struct {
client *elasticsearch.Client
client *elastic.Client
address []string
username, password string
logger elastic.Logger
}
type Option func(*Instance)
var (
client *elasticsearch.Client
client *elastic.Client
)
func WithEsAddress(address []string) Option {
@ -35,24 +37,29 @@ func WithEsPassword(password string) Option {
}
}
func WithEsLog(logger elastic.Logger) Option {
return func(instance *Instance) {
instance.logger = logger
}
}
func (this *Instance) Init() *Instance {
obj := elasticsearch.Config{
Addresses: this.address,
Username: this.username,
Password: this.password,
}
client, err := elasticsearch.NewClient(obj)
client, err := elastic.NewClient(
elastic.SetSniff(false),
elastic.SetURL(this.address...),
//elastic.SetInfoLog(this.logger),
)
if err != nil {
panic("Elasticsearch New Error " + err.Error())
panic(err)
}
ctx := context.Background()
if _, err = client.Ping(); err != nil {
panic("Elasticsearch Ping Error " + err.Error())
for _, address := range this.address {
if _, _, err = client.Ping(address).Do(ctx); err != nil {
panic(err)
}
}
this.client = client
return this
}
@ -69,3 +76,7 @@ func NewInstance(option ...Option) *Instance {
}
return instance
}
func GetInstance() *elastic.Client {
return client
}