You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
package elasticsearch |
|
|
|
import ( |
|
"git.gz.internal.jumaiyx.cn/jm/jmproto/conf" |
|
elastic "git.gz.internal.jumaiyx.cn/pkg/elasticsearch/v7" |
|
"git.gz.internal.jumaiyx.cn/pkg/log" |
|
"github.com/pkg/errors" |
|
) |
|
|
|
var ( |
|
ErrHostEmpty = errors.New("Host cannot be empty") |
|
) |
|
|
|
type Client struct { |
|
log log.Logger |
|
} |
|
|
|
func NewElastic(log log.Logger, elasticsearch *conf.Elasticsearch) (client *elastic.Client, err error) { |
|
var options []elastic.ClientOptionFunc |
|
if len(elasticsearch.Hosts) == 0 { |
|
return nil, ErrHostEmpty |
|
} |
|
options = append(options, elastic.SetSniff(elasticsearch.Sniff)) // SetSniff启用或禁用嗅探器(默认情况下启用)。 |
|
options = append(options, elastic.SetURL(elasticsearch.Hosts...)) // |
|
|
|
// 账号密码 |
|
if elasticsearch.Username != "" && elasticsearch.Password != "" { |
|
options = append(options, elastic.SetBasicAuth(elasticsearch.Username, elasticsearch.Password)) |
|
} |
|
client, err = elastic.NewClient( |
|
options..., |
|
//elastic.SetURL("http://192.168.1.169:9200"), |
|
) |
|
if err != nil { |
|
log.Errorf("Elasticsearch connection failed:%v", err) |
|
return nil, err |
|
} |
|
return |
|
}
|
|
|