-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
409 lines (343 loc) · 11.8 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
package elasticsearch
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
// Searcher set the contract to manage indices, synchronize data and request
type Client interface {
CreateIndex(indexName, mapping string) (*Response, error)
DeleteIndex(indexName string) (*Response, error)
UpdateIndexSetting(indexName, mapping string) (*Response, error)
IndexSettings(indexName string) (Settings, error)
IndexExists(indexName string) (bool, error)
Status(indices string) (*Settings, error)
InsertDocument(indexName, documentType, identifier string, data []byte) (*InsertDocument, error)
Document(indexName, documentType, identifier string) (*Document, error)
DeleteDocument(indexName, documentType, identifier string) (*Document, error)
Bulk(indexName string, data []byte) (*Bulk, error)
Search(indexName, documentType, data string, explain bool) (*SearchResult, error)
MSearch(queries []MSearchQuery) (*MSearchResult, error)
Suggest(indexName, data string) ([]byte, error)
GetIndicesFromAlias(alias string) ([]string, error)
UpdateAlias(remove []string, add []string, alias string) (*Response, error)
UpdateByQuery(indexName, query string) (*UpdateByQueryResult, error)
}
// A SearchClient describes the client configuration to manage an ElasticSearch index.
type client struct {
Host url.URL
}
// NewSearchClient creates and initializes a new ElasticSearch client, implements core api for Indexing and searching.
func NewClient(scheme, host, port string) Client {
u := url.URL{
Scheme: scheme,
Host: host + ":" + port,
}
return &client{Host: u}
}
// NewSearchClient creates and initializes a new ElasticSearch client, implements core api for Indexing and searching.
func NewClientFromUrl(rawurl string) Client {
u, err := url.Parse(rawurl)
if err != nil {
log.Fatal(err)
return nil
}
return &client{Host: *u}
}
// CreateIndex instantiates an index
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
func (c *client) CreateIndex(indexName, mapping string) (*Response, error) {
url := c.Host.String() + "/" + indexName
reader := bytes.NewBufferString(mapping)
response, err := sendHTTPRequest("PUT", url, reader)
if err != nil {
return &Response{}, err
}
esResp := &Response{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &Response{}, err
}
return esResp, nil
}
// DeleteIndex deletes an existing index.
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
func (c *client) DeleteIndex(indexName string) (*Response, error) {
url := c.Host.String() + "/" + indexName
response, err := sendHTTPRequest("DELETE", url, nil)
if err != nil {
return &Response{}, err
}
esResp := &Response{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &Response{}, err
}
return esResp, nil
}
// UpdateIndexSetting changes specific index level settings in real time
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
func (c *client) UpdateIndexSetting(indexName, mapping string) (*Response, error) {
url := c.Host.String() + "/" + indexName + "/_settings"
reader := bytes.NewBufferString(mapping)
response, err := sendHTTPRequest("PUT", url, reader)
if err != nil {
return &Response{}, err
}
esResp := &Response{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &Response{}, err
}
return esResp, nil
}
// IndexSettings allows to retrieve settings of index
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html
func (c *client) IndexSettings(indexName string) (Settings, error) {
url := c.Host.String() + "/" + indexName + "/_settings"
response, err := sendHTTPRequest("GET", url, nil)
if err != nil {
return Settings{}, err
}
type settingsArray map[string]Settings
dec := json.NewDecoder(bytes.NewBuffer(response))
var info settingsArray
err = dec.Decode(&info)
if err != nil {
return Settings{}, err
}
return info[indexName], nil
}
// IndexExists allows to check if the index exists or not.
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html
func (c *client) IndexExists(indexName string) (bool, error) {
url := c.Host.String() + "/" + indexName
httpClient := &http.Client{}
newReq, err := httpClient.Head(url)
if err != nil {
return false, err
}
return newReq.StatusCode == http.StatusOK, nil
}
// Status allows to get a comprehensive status information
func (c *client) Status(indices string) (*Settings, error) {
url := c.Host.String() + "/" + indices + "/_status"
response, err := sendHTTPRequest("GET", url, nil)
if err != nil {
return &Settings{}, err
}
esResp := &Settings{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &Settings{}, err
}
return esResp, nil
}
// InsertDocument adds or updates a typed JSON document in a specific index, making it searchable
// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
func (c *client) InsertDocument(indexName, documentType, identifier string, data []byte) (*InsertDocument, error) {
url := c.Host.String() + "/" + indexName + "/_doc/" + identifier
reader := bytes.NewBuffer(data)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
return &InsertDocument{}, err
}
esResp := &InsertDocument{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &InsertDocument{}, err
}
return esResp, nil
}
// Document gets a typed JSON document from the index based on its id
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html
func (c *client) Document(indexName, documentType, identifier string) (*Document, error) {
url := c.Host.String() + "/" + indexName + "/" + documentType + "/" + identifier
response, err := sendHTTPRequest("GET", url, nil)
if err != nil {
return &Document{}, err
}
esResp := &Document{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &Document{}, err
}
return esResp, nil
}
// DeleteDocument deletes a typed JSON document from a specific index based on its id
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html
func (c *client) DeleteDocument(indexName, documentType, identifier string) (*Document, error) {
url := c.Host.String() + "/" + indexName + "/" + documentType + "/" + identifier
response, err := sendHTTPRequest("DELETE", url, nil)
if err != nil {
return &Document{}, err
}
esResp := &Document{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &Document{}, err
}
return esResp, nil
}
// Bulk makes it possible to perform many index/delete operations in a single API call.
// This can greatly increase the indexing speed.
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html
func (c *client) Bulk(indexName string, data []byte) (*Bulk, error) {
url := c.Host.String() + "/" + indexName + "/_bulk"
reader := bytes.NewBuffer(data)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
return &Bulk{}, err
}
esResp := &Bulk{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &Bulk{}, err
}
return esResp, nil
}
// Search allows to execute a search query and get back search hits that match the query
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html
func (c *client) Search(indexName, documentType, data string, explain bool) (*SearchResult, error) {
url := c.Host.String() + "/" + indexName + "/_search"
if explain {
url += "?explain"
}
reader := bytes.NewBufferString(data)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
return &SearchResult{}, err
}
esResp := &SearchResult{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &SearchResult{}, err
}
return esResp, nil
}
// MSearch allows to execute a multi-search and get back result
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html
func (c *client) MSearch(queries []MSearchQuery) (*MSearchResult, error) {
replacer := strings.NewReplacer("\n", " ")
queriesList := make([]string, len(queries))
for i, query := range queries {
queriesList[i] = query.Header + "\n" + replacer.Replace(query.Body)
}
mSearchQuery := strings.Join(queriesList, "\n") + "\n" // Don't forget trailing \n
url := c.Host.String() + "/_msearch"
reader := bytes.NewBufferString(mSearchQuery)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
return &MSearchResult{}, err
}
esResp := &MSearchResult{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &MSearchResult{}, err
}
return esResp, nil
}
// Suggest allows basic auto-complete functionality.
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
func (c *client) Suggest(indexName, data string) ([]byte, error) {
url := c.Host.String() + "/" + indexName + "/_suggest"
reader := bytes.NewBufferString(data)
response, err := sendHTTPRequest("POST", url, reader)
return response, err
}
// GetIndicesFromAlias returns the list of indices the alias points to
func (c *client) GetIndicesFromAlias(alias string) ([]string, error) {
url := c.Host.String() + "/*/_alias/" + alias
response, err := sendHTTPRequest("GET", url, nil)
if err != nil {
return []string{}, err
}
esResp := make(map[string]*json.RawMessage)
err = json.Unmarshal(response, &esResp)
if err != nil {
return []string{}, err
}
indices := make([]string, len(esResp))
i := 0
for k := range esResp {
indices[i] = k
i++
}
return indices, nil
}
// UpdateAlias updates the indices on which the alias point to.
// The change is atomic.
func (c *client) UpdateAlias(remove []string, add []string, alias string) (*Response, error) {
url := c.Host.String() + "/_aliases"
body := getAliasQuery(remove, add, alias)
reader := bytes.NewBufferString(body)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
return &Response{}, err
}
esResp := &Response{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &Response{}, err
}
return esResp, nil
}
// UpdateByQuery updates documents that match the specified query.
// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html
func (c *client) UpdateByQuery(indexName, query string) (*UpdateByQueryResult, error) {
url := c.Host.String() + "/" + indexName + "/_update_by_query"
reader := bytes.NewBufferString(query)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
return &UpdateByQueryResult{}, err
}
esResp := &UpdateByQueryResult{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &UpdateByQueryResult{}, err
}
return esResp, nil
}
func getAliasQuery(remove []string, add []string, alias string) string {
actions := make([]string, len(remove)+len(add))
i := 0
for _, index := range remove {
actions[i] = "{ \"remove\": { \"index\": \"" + index + "\", \"alias\": \"" + alias + "\" }}"
i++
}
for _, index := range add {
actions[i] = "{ \"add\": { \"index\": \"" + index + "\", \"alias\": \"" + alias + "\" }}"
i++
}
return "{\"actions\": [ " + strings.Join(actions, ",") + " ]}"
}
func sendHTTPRequest(method, url string, body io.Reader) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
// if method == "POST" || method == "PUT" {
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// }
req.Header.Set("Content-Type", "application/json")
newReq, err := client.Do(req)
if err != nil {
return nil, err
}
defer newReq.Body.Close()
response, err := ioutil.ReadAll(newReq.Body)
if err != nil {
return nil, err
}
if newReq.StatusCode > http.StatusCreated && newReq.StatusCode < http.StatusNotFound {
return nil, errors.New(string(response))
}
return response, nil
}