Files
2021-12-09 16:17:23 +08:00

72 lines
1.2 KiB
Go

package es
import (
"github.com/elastic/go-elasticsearch/v7"
)
type Instance struct {
client *elasticsearch.Client
address []string
username, password string
}
type Option func(*Instance)
var (
client *elasticsearch.Client
)
func WithEsAddress(address []string) Option {
return func(instance *Instance) {
instance.address = address
}
}
func WithEsUsername(username string) Option {
return func(instance *Instance) {
instance.username = username
}
}
func WithEsPassword(password string) Option {
return func(instance *Instance) {
instance.password = password
}
}
func (this *Instance) Init() *Instance {
obj := elasticsearch.Config{
Addresses: this.address,
Username: this.username,
Password: this.password,
}
client, err := elasticsearch.NewClient(obj)
if err != nil {
panic("Elasticsearch New Error " + err.Error())
}
if _, err = client.Ping(); err != nil {
panic("Elasticsearch Ping Error " + err.Error())
}
this.client = client
return this
}
func (this *Instance) Local() *Instance {
client = this.client
return this
}
func NewInstance(option ...Option) *Instance {
instance := new(Instance)
for _, v := range option {
v(instance)
}
return instance
}