@@ -7,13 +7,15 @@ import (
7
7
"io/ioutil"
8
8
"net/http"
9
9
"net/url"
10
+ "crypto/tls"
10
11
11
12
"github.com/juju/errors"
12
13
)
13
14
14
15
// Although there are many Elasticsearch clients with Go, I still want to implement one by myself.
15
16
// Because we only need some very simple usages.
16
17
type Client struct {
18
+ Protocol string
17
19
Addr string
18
20
User string
19
21
Password string
@@ -22,6 +24,7 @@ type Client struct {
22
24
}
23
25
24
26
type ClientConfig struct {
27
+ Https bool
25
28
Addr string
26
29
User string
27
30
Password string
@@ -34,7 +37,16 @@ func NewClient(conf *ClientConfig) *Client {
34
37
c .User = conf .User
35
38
c .Password = conf .Password
36
39
37
- c .c = & http.Client {}
40
+ if conf .Https {
41
+ c .Protocol = "https"
42
+ tr := & http.Transport {
43
+ TLSClientConfig : & tls.Config {InsecureSkipVerify : true },
44
+ }
45
+ c .c = & http.Client {Transport : tr }
46
+ } else {
47
+ c .Protocol = "http"
48
+ c .c = & http.Client {}
49
+ }
38
50
39
51
return c
40
52
}
@@ -237,7 +249,7 @@ func (c *Client) DoBulk(url string, items []*BulkRequest) (*BulkResponse, error)
237
249
}
238
250
239
251
func (c * Client ) CreateMapping (index string , docType string , mapping map [string ]interface {}) error {
240
- reqUrl := fmt .Sprintf ("http ://%s/%s" , c .Addr ,
252
+ reqUrl := fmt .Sprintf ("%s ://%s/%s" , c . Protocol , c .Addr ,
241
253
url .QueryEscape (index ))
242
254
243
255
r , err := c .Do ("HEAD" , reqUrl , nil )
@@ -256,7 +268,7 @@ func (c *Client) CreateMapping(index string, docType string, mapping map[string]
256
268
return errors .Errorf ("Error: %s, code: %d" , http .StatusText (r .Code ), r .Code )
257
269
}
258
270
259
- reqUrl = fmt .Sprintf ("http ://%s/%s/%s/_mapping" , c .Addr ,
271
+ reqUrl = fmt .Sprintf ("%s ://%s/%s/%s/_mapping" , c . Protocol , c .Addr ,
260
272
url .QueryEscape (index ),
261
273
url .QueryEscape (docType ))
262
274
@@ -265,7 +277,7 @@ func (c *Client) CreateMapping(index string, docType string, mapping map[string]
265
277
}
266
278
267
279
func (c * Client ) GetMapping (index string , docType string ) (* MappingResponse , error ){
268
- reqUrl := fmt .Sprintf ("http ://%s/%s/%s/_mapping" , c .Addr ,
280
+ reqUrl := fmt .Sprintf ("%s ://%s/%s/%s/_mapping" , c . Protocol , c .Addr ,
269
281
url .QueryEscape (index ),
270
282
url .QueryEscape (docType ))
271
283
buf := bytes .NewBuffer (nil )
@@ -293,7 +305,7 @@ func (c *Client) GetMapping(index string, docType string) (*MappingResponse, err
293
305
}
294
306
295
307
func (c * Client ) DeleteIndex (index string ) error {
296
- reqUrl := fmt .Sprintf ("http ://%s/%s" , c .Addr ,
308
+ reqUrl := fmt .Sprintf ("%s ://%s/%s" , c . Protocol , c .Addr ,
297
309
url .QueryEscape (index ))
298
310
299
311
r , err := c .Do ("DELETE" , reqUrl , nil )
@@ -309,7 +321,7 @@ func (c *Client) DeleteIndex(index string) error {
309
321
}
310
322
311
323
func (c * Client ) Get (index string , docType string , id string ) (* Response , error ) {
312
- reqUrl := fmt .Sprintf ("http ://%s/%s/%s/%s" , c .Addr ,
324
+ reqUrl := fmt .Sprintf ("%s ://%s/%s/%s/%s" , c . Protocol , c .Addr ,
313
325
url .QueryEscape (index ),
314
326
url .QueryEscape (docType ),
315
327
url .QueryEscape (id ))
@@ -319,7 +331,7 @@ func (c *Client) Get(index string, docType string, id string) (*Response, error)
319
331
320
332
// Can use Update to create or update the data
321
333
func (c * Client ) Update (index string , docType string , id string , data map [string ]interface {}) error {
322
- reqUrl := fmt .Sprintf ("http ://%s/%s/%s/%s" , c .Addr ,
334
+ reqUrl := fmt .Sprintf ("%s ://%s/%s/%s/%s" , c . Protocol , c .Addr ,
323
335
url .QueryEscape (index ),
324
336
url .QueryEscape (docType ),
325
337
url .QueryEscape (id ))
@@ -337,7 +349,7 @@ func (c *Client) Update(index string, docType string, id string, data map[string
337
349
}
338
350
339
351
func (c * Client ) Exists (index string , docType string , id string ) (bool , error ) {
340
- reqUrl := fmt .Sprintf ("http ://%s/%s/%s/%s" , c .Addr ,
352
+ reqUrl := fmt .Sprintf ("%s ://%s/%s/%s/%s" , c . Protocol , c .Addr ,
341
353
url .QueryEscape (index ),
342
354
url .QueryEscape (docType ),
343
355
url .QueryEscape (id ))
@@ -351,7 +363,7 @@ func (c *Client) Exists(index string, docType string, id string) (bool, error) {
351
363
}
352
364
353
365
func (c * Client ) Delete (index string , docType string , id string ) error {
354
- reqUrl := fmt .Sprintf ("http ://%s/%s/%s/%s" , c .Addr ,
366
+ reqUrl := fmt .Sprintf ("%s ://%s/%s/%s/%s" , c . Protocol , c .Addr ,
355
367
url .QueryEscape (index ),
356
368
url .QueryEscape (docType ),
357
369
url .QueryEscape (id ))
@@ -370,20 +382,20 @@ func (c *Client) Delete(index string, docType string, id string) error {
370
382
371
383
// only support parent in 'Bulk' related apis
372
384
func (c * Client ) Bulk (items []* BulkRequest ) (* BulkResponse , error ) {
373
- reqUrl := fmt .Sprintf ("http ://%s/_bulk" , c .Addr )
385
+ reqUrl := fmt .Sprintf ("%s ://%s/_bulk" , c . Protocol , c .Addr )
374
386
375
387
return c .DoBulk (reqUrl , items )
376
388
}
377
389
378
390
func (c * Client ) IndexBulk (index string , items []* BulkRequest ) (* BulkResponse , error ) {
379
- reqUrl := fmt .Sprintf ("http ://%s/%s/_bulk" , c .Addr ,
391
+ reqUrl := fmt .Sprintf ("%s ://%s/%s/_bulk" , c . Protocol , c .Addr ,
380
392
url .QueryEscape (index ))
381
393
382
394
return c .DoBulk (reqUrl , items )
383
395
}
384
396
385
397
func (c * Client ) IndexTypeBulk (index string , docType string , items []* BulkRequest ) (* BulkResponse , error ) {
386
- reqUrl := fmt .Sprintf ("http ://%s/%s/%s/_bulk" , c .Addr ,
398
+ reqUrl := fmt .Sprintf ("%s ://%s/%s/%s/_bulk" , c . Protocol , c .Addr ,
387
399
url .QueryEscape (index ),
388
400
url .QueryEscape (docType ))
389
401
0 commit comments