forked from go-mysql-org/go-mysql-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
429 lines (348 loc) · 9.77 KB
/
client.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package elastic
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/juju/errors"
)
// Client is the client to communicate with ES.
// Although there are many Elasticsearch clients with Go, I still want to implement one by myself.
// Because we only need some very simple usages.
type Client struct {
Protocol string
Addr string
User string
Password string
c *http.Client
}
// ClientConfig is the configuration for the client.
type ClientConfig struct {
HTTPS bool
Addr string
User string
Password string
}
// NewClient creates the Cient with configuration.
func NewClient(conf *ClientConfig) *Client {
c := new(Client)
c.Addr = conf.Addr
c.User = conf.User
c.Password = conf.Password
if conf.HTTPS {
c.Protocol = "https"
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
c.c = &http.Client{Transport: tr}
} else {
c.Protocol = "http"
c.c = &http.Client{}
}
return c
}
// ResponseItem is the ES item in the response.
type ResponseItem struct {
ID string `json:"_id"`
Index string `json:"_index"`
Type string `json:"_type"`
Version int `json:"_version"`
Found bool `json:"found"`
Source map[string]interface{} `json:"_source"`
}
// Response is the ES response
type Response struct {
Code int
ResponseItem
}
// See http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/bulk.html
const (
ActionCreate = "create"
ActionUpdate = "update"
ActionDelete = "delete"
ActionIndex = "index"
)
// BulkRequest is used to send multi request in batch.
type BulkRequest struct {
Action string
Index string
Type string
ID string
Parent string
Pipeline string
Data map[string]interface{}
}
func (r *BulkRequest) bulk(buf *bytes.Buffer) error {
meta := make(map[string]map[string]string)
metaData := make(map[string]string)
if len(r.Index) > 0 {
metaData["_index"] = r.Index
}
if len(r.Type) > 0 {
metaData["_type"] = r.Type
}
if len(r.ID) > 0 {
metaData["_id"] = r.ID
}
if len(r.Parent) > 0 {
metaData["_parent"] = r.Parent
}
if len(r.Pipeline) > 0 {
metaData["pipeline"] = r.Pipeline
}
meta[r.Action] = metaData
data, err := json.Marshal(meta)
if err != nil {
return errors.Trace(err)
}
buf.Write(data)
buf.WriteByte('\n')
switch r.Action {
case ActionDelete:
//nothing to do
case ActionUpdate:
doc := map[string]interface{}{
"doc": r.Data,
}
data, err = json.Marshal(doc)
if err != nil {
return errors.Trace(err)
}
buf.Write(data)
buf.WriteByte('\n')
default:
//for create and index
data, err = json.Marshal(r.Data)
if err != nil {
return errors.Trace(err)
}
buf.Write(data)
buf.WriteByte('\n')
}
return nil
}
// BulkResponse is the response for the bulk request.
type BulkResponse struct {
Code int
Took int `json:"took"`
Errors bool `json:"errors"`
Items []map[string]*BulkResponseItem `json:"items"`
}
// BulkResponseItem is the item in the bulk response.
type BulkResponseItem struct {
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Version int `json:"_version"`
Status int `json:"status"`
Error json.RawMessage `json:"error"`
Found bool `json:"found"`
}
// MappingResponse is the response for the mapping request.
type MappingResponse struct {
Code int
Mapping Mapping
}
// Mapping represents ES mapping.
type Mapping map[string]struct {
Mappings map[string]struct {
Properties map[string]struct {
Type string `json:"type"`
Fields interface{} `json:"fields"`
} `json:"properties"`
} `json:"mappings"`
}
// DoRequest sends a request with body to ES.
func (c *Client) DoRequest(method string, url string, body *bytes.Buffer) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
req.Header.Add("Content-Type", "application/json")
if err != nil {
return nil, errors.Trace(err)
}
if len(c.User) > 0 && len(c.Password) > 0 {
req.SetBasicAuth(c.User, c.Password)
}
resp, err := c.c.Do(req)
return resp, err
}
// Do sends the request with body to ES.
func (c *Client) Do(method string, url string, body map[string]interface{}) (*Response, error) {
bodyData, err := json.Marshal(body)
if err != nil {
return nil, errors.Trace(err)
}
buf := bytes.NewBuffer(bodyData)
if body == nil {
buf = bytes.NewBuffer(nil)
}
resp, err := c.DoRequest(method, url, buf)
if err != nil {
return nil, errors.Trace(err)
}
defer resp.Body.Close()
ret := new(Response)
ret.Code = resp.StatusCode
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Trace(err)
}
if len(data) > 0 {
err = json.Unmarshal(data, &ret.ResponseItem)
}
return ret, errors.Trace(err)
}
// DoBulk sends the bulk request to the ES.
func (c *Client) DoBulk(url string, items []*BulkRequest) (*BulkResponse, error) {
var buf bytes.Buffer
for _, item := range items {
if err := item.bulk(&buf); err != nil {
return nil, errors.Trace(err)
}
}
resp, err := c.DoRequest("POST", url, &buf)
if err != nil {
return nil, errors.Trace(err)
}
defer resp.Body.Close()
ret := new(BulkResponse)
ret.Code = resp.StatusCode
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Trace(err)
}
if len(data) > 0 {
err = json.Unmarshal(data, &ret)
}
return ret, errors.Trace(err)
}
// CreateMapping creates a ES mapping.
func (c *Client) CreateMapping(index string, docType string, mapping map[string]interface{}) error {
reqURL := fmt.Sprintf("%s://%s/%s", c.Protocol, c.Addr,
url.QueryEscape(index))
r, err := c.Do("HEAD", reqURL, nil)
if err != nil {
return errors.Trace(err)
}
// if index doesn't exist, will get 404 not found, create index first
if r.Code == http.StatusNotFound {
_, err = c.Do("PUT", reqURL, nil)
if err != nil {
return errors.Trace(err)
}
} else if r.Code != http.StatusOK {
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
}
reqURL = fmt.Sprintf("%s://%s/%s/%s/_mapping", c.Protocol, c.Addr,
url.QueryEscape(index),
url.QueryEscape(docType))
_, err = c.Do("POST", reqURL, mapping)
return errors.Trace(err)
}
// GetMapping gets the mapping.
func (c *Client) GetMapping(index string, docType string) (*MappingResponse, error) {
reqURL := fmt.Sprintf("%s://%s/%s/%s/_mapping", c.Protocol, c.Addr,
url.QueryEscape(index),
url.QueryEscape(docType))
buf := bytes.NewBuffer(nil)
resp, err := c.DoRequest("GET", reqURL, buf)
if err != nil {
return nil, errors.Trace(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Trace(err)
}
ret := new(MappingResponse)
err = json.Unmarshal(data, &ret.Mapping)
if err != nil {
return nil, errors.Trace(err)
}
ret.Code = resp.StatusCode
return ret, errors.Trace(err)
}
// DeleteIndex deletes the index.
func (c *Client) DeleteIndex(index string) error {
reqURL := fmt.Sprintf("%s://%s/%s", c.Protocol, c.Addr,
url.QueryEscape(index))
r, err := c.Do("DELETE", reqURL, nil)
if err != nil {
return errors.Trace(err)
}
if r.Code == http.StatusOK || r.Code == http.StatusNotFound {
return nil
}
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
}
// Get gets the item by id.
func (c *Client) Get(index string, docType string, id string) (*Response, error) {
reqURL := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
url.QueryEscape(index),
url.QueryEscape(docType),
url.QueryEscape(id))
return c.Do("GET", reqURL, nil)
}
// Update creates or updates the data
func (c *Client) Update(index string, docType string, id string, data map[string]interface{}) error {
reqURL := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
url.QueryEscape(index),
url.QueryEscape(docType),
url.QueryEscape(id))
r, err := c.Do("PUT", reqURL, data)
if err != nil {
return errors.Trace(err)
}
if r.Code == http.StatusOK || r.Code == http.StatusCreated {
return nil
}
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
}
// Exists checks whether id exists or not.
func (c *Client) Exists(index string, docType string, id string) (bool, error) {
reqURL := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
url.QueryEscape(index),
url.QueryEscape(docType),
url.QueryEscape(id))
r, err := c.Do("HEAD", reqURL, nil)
if err != nil {
return false, err
}
return r.Code == http.StatusOK, nil
}
// Delete deletes the item by id.
func (c *Client) Delete(index string, docType string, id string) error {
reqURL := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
url.QueryEscape(index),
url.QueryEscape(docType),
url.QueryEscape(id))
r, err := c.Do("DELETE", reqURL, nil)
if err != nil {
return errors.Trace(err)
}
if r.Code == http.StatusOK || r.Code == http.StatusNotFound {
return nil
}
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
}
// Bulk sends the bulk request.
// only support parent in 'Bulk' related apis
func (c *Client) Bulk(items []*BulkRequest) (*BulkResponse, error) {
reqURL := fmt.Sprintf("%s://%s/_bulk", c.Protocol, c.Addr)
return c.DoBulk(reqURL, items)
}
// IndexBulk sends the bulk request for index.
func (c *Client) IndexBulk(index string, items []*BulkRequest) (*BulkResponse, error) {
reqURL := fmt.Sprintf("%s://%s/%s/_bulk", c.Protocol, c.Addr,
url.QueryEscape(index))
return c.DoBulk(reqURL, items)
}
// IndexTypeBulk sends the bulk request for index and doc type.
func (c *Client) IndexTypeBulk(index string, docType string, items []*BulkRequest) (*BulkResponse, error) {
reqURL := fmt.Sprintf("%s://%s/%s/%s/_bulk", c.Protocol, c.Addr,
url.QueryEscape(index),
url.QueryEscape(docType))
return c.DoBulk(reqURL, items)
}