-
Notifications
You must be signed in to change notification settings - Fork 2
/
elasticv7.go
145 lines (121 loc) · 3.57 KB
/
elasticv7.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package elastic
import (
"context"
"encoding/json"
"io/ioutil"
"strings"
es "github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/skema-dev/skema-go/config"
"github.com/skema-dev/skema-go/logging"
)
type elasticClientV7 struct {
client *es.Client
}
func newElasticClientV7(conf *config.Config) *elasticClientV7 {
var err error
addresses := conf.GetStringArray("addresses")
cfg := es.Config{
Addresses: addresses,
}
username := conf.GetString("username")
password := conf.GetString("password")
if username != "" && password != "" {
cfg.Username = username
cfg.Password = password
logging.Debugf("Elastic Account %s:%s", username, password)
}
var cert []byte
if certFile := conf.GetString("cert"); certFile != "" {
cert, err = ioutil.ReadFile("./http_ca.crt")
if err != nil {
logging.Fatalf("ERROR: Unable to read CA from %q: %s", certFile, err)
}
cfg.CACert = cert
}
esclient, err := es.NewClient(cfg)
if err != nil {
logging.Errorf("Failed creating es client: %s", err.Error())
return nil
}
if info, err := esclient.Info(); err == nil {
logging.Infof(info.String())
} else {
logging.Errorf(err.Error())
}
return &elasticClientV7{
client: esclient,
}
}
func (e *elasticClientV7) Index(index string, id string, value interface{}) error {
if index == "" || id == "" {
return logging.Errorf("index and id should not be empty. index: %s, id: %s", index, id)
}
data, err := json.Marshal(value)
if err != nil {
return logging.Errorf(err.Error())
}
s := string(data)
logging.Debugw("elastic index request", "index", index, "id", id, "body", s)
req := esapi.IndexRequest{
Index: index,
DocumentID: id,
Body: strings.NewReader(s),
Refresh: "true",
}
res, err := req.Do(context.Background(), e.client)
if err != nil {
return logging.Errorf(err.Error())
}
defer res.Body.Close()
if res.IsError() {
return logging.Errorf("Elasticsearch indexing error for document id=%s: %s", id, res.Status())
}
return nil
}
func (e *elasticClientV7) Search(index string, termQueryType string, query map[string]interface{}, option *SearchOption) ([]map[string]interface{}, error) {
searchQuery, err := buildTermQuery(termQueryType, query, option)
if err != nil {
return nil, logging.Errorf(err.Error())
}
logging.Debugf("Search Query: %s", searchQuery)
res, err := e.client.Search(
e.client.Search.WithContext(context.Background()),
e.client.Search.WithIndex(index),
e.client.Search.WithBody(strings.NewReader(searchQuery)),
e.client.Search.WithTrackTotalHits(true),
e.client.Search.WithPretty(),
)
if err != nil {
return nil, logging.Errorf(err.Error())
}
if res.IsError() {
return nil, logging.Errorf("Error happend for search %v", res)
}
resMap := map[string]interface{}{}
err = json.NewDecoder(res.Body).Decode(&resMap)
if err != nil {
return nil, logging.Errorf(err.Error())
}
return processSearchResult(resMap)
}
func (e *elasticClientV7) Delete(index string, ids []string) {
searchQuery, err := buildTermQuery("terms", map[string]interface{}{"id": ids}, nil)
logging.Debugw("Delete es docs", "index", index, "ids", ids)
_, err = e.client.DeleteByQuery([]string{index}, strings.NewReader(searchQuery))
if err != nil {
logging.Errorf("failded to deletes: %s", err.Error())
return
}
}
func (e *elasticClientV7) DeleteIndex(indexes []string) {
req := esapi.IndicesDeleteRequest{
Index: indexes,
}
_, err := req.Do(context.Background(), e.client)
if err != nil {
logging.Errorf(err.Error())
return
}
logging.Debugf("index deleted %d", len(indexes))
}