generated from edwinvautier/go-boilerplate
/
es_connector.go
57 lines (48 loc) · 1.27 KB
/
es_connector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package database
import (
es "github.com/elastic/go-elasticsearch/v8"
"github.com/olivere/elastic/v7"
log "github.com/sirupsen/logrus"
"time"
)
type EsCfg struct {
Url string
}
var EsClient *elastic.Client
// GetESClient Attempts to connect to elastic search using "github.com/olivere/elastic/v7" package.
// Affect the EsClient to elastic client.
func GetESClient(escfg *EsCfg) {
client, err := elastic.NewClient(
elastic.SetSniff(true),
elastic.SetURL(escfg.Url),
elastic.SetHealthcheckInterval(5*time.Second),
)
if err != nil {
log.Error("Cannot connect to elastic search")
} else {
log.Info("Connected to elastic search")
}
EsClient = client
}
// GetOriginalESClient Attempts to connect to elastisearch service using "github.com/elastic/go-elasticsearch/v8" package.
// Return the client and error
func GetOriginalESClient() (*es.Client, error) {
cfg := es.Config{
Addresses: []string{
"http://es:9200",
},
}
elsrch, err := es.NewClient(cfg)
if err != nil {
log.Error("Error creating the client: %s", err)
}
res, err := elsrch.Info()
if err != nil {
log.Error("Error getting response: %s", err)
}
if err = res.Body.Close(); err != nil {
log.Error("Error closing connection: %s", err)
}
log.Info("Connected to elasticsearch")
return elsrch, err
}