-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
55 lines (44 loc) · 990 Bytes
/
cluster.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
package es
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
)
type Cluster struct {
URL url.URL
}
func NewCluster(address string) (*Cluster, error) {
parsed, parseErr := url.Parse(address)
if nil != parseErr {
return nil, parseErr
}
return &Cluster{*parsed}, nil
}
func (c *Cluster) Search(search Search) (*http.Response, error) {
url := c.URL
url.Path = search.Path()
query := search.Query()
if search.Type() != SEARCH_TYPE_SCROLL {
query["search_type"] = []string{search.Type().String()}
}
url.RawQuery = query.Encode()
var (
req *http.Request
createErr error
)
body := search.Data()
if body == nil {
req, createErr = http.NewRequest("GET", url.String(), nil)
} else {
var buffer bytes.Buffer
if err := json.NewEncoder(&buffer).Encode(body); err != nil {
return nil, err
}
req, createErr = http.NewRequest("POST", url.String(), &buffer)
}
if nil != createErr {
return nil, createErr
}
return (&http.Client{}).Do(req)
}