-
Notifications
You must be signed in to change notification settings - Fork 37
/
goes.go
305 lines (253 loc) · 7 KB
/
goes.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
// Copyright 2013 Belogik. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package goes provides an API to access Elasticsearch.
package goes
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
BULK_COMMAND_INDEX = "index"
BULK_COMMAND_DELETE = "delete"
)
func (err *SearchError) Error() string {
return fmt.Sprintf("[%d] %s", err.StatusCode, err.Msg)
}
// NewConnection initiates a new Connection to an elasticsearch server
//
// This function is pretty useless for now but might be useful in a near future
// if wee need more features like connection pooling or load balancing.
func NewConnection(host string, port string) *Connection {
return &Connection{host, port}
}
// CreateIndex creates a new index represented by a name and a mapping
func (c *Connection) CreateIndex(name string, mapping map[string]interface{}) (Response, error) {
r := Request{
Conn: c,
Query: mapping,
IndexList: []string{name},
method: "PUT",
}
return r.Run()
}
// DeleteIndex deletes an index represented by a name
func (c *Connection) DeleteIndex(name string) (Response, error) {
r := Request{
Conn: c,
IndexList: []string{name},
method: "DELETE",
}
return r.Run()
}
// RefreshIndex refreshes an index represented by a name
func (c *Connection) RefreshIndex(name string) (Response, error) {
r := Request{
Conn: c,
IndexList: []string{name},
method: "POST",
api: "_refresh",
}
return r.Run()
}
// Stats fetches statistics (_stats) for the current elasticsearch server
func (c *Connection) Stats(indexList []string, extraArgs url.Values) (Response, error) {
r := Request{
Conn: c,
IndexList: indexList,
ExtraArgs: extraArgs,
method: "GET",
api: "_stats",
}
return r.Run()
}
// IndexStatus fetches the status (_status) for the indices defined in
// indexList. Use _all in indexList to get stats for all indices
func (c *Connection) IndexStatus(indexList []string) (Response, error) {
r := Request{
Conn: c,
IndexList: indexList,
method: "GET",
api: "_status",
}
return r.Run()
}
// Bulk adds multiple documents in bulk mode to the index for a given type
func (c *Connection) BulkSend(index string, documents []Document) (Response, error) {
// We do not generate a traditionnal JSON here (often a one liner)
// Elasticsearch expects one line of JSON per line (EOL = \n)
// plus an extra \n at the very end of the document
//
// More informations about the Bulk JSON format for Elasticsearch:
//
// - http://www.elasticsearch.org/guide/reference/api/bulk.html
//
// This is quite annoying for us as we can not use the simple JSON
// Marshaler available in Run().
//
// We have to generate this special JSON by ourselves which leads to
// the code below.
//
// I know it is unreadable I must find an elegant way to fix this.
bulkData := []byte{}
for _, doc := range documents {
header := map[string]interface{}{
doc.BulkCommand: map[string]interface{}{
"_index": doc.Index,
"_type": doc.Type,
"_id": doc.Id,
},
}
temp, err := json.Marshal(header)
if err != nil {
return Response{}, err
}
temp = append(temp, '\n')
bulkData = append(bulkData, temp[:]...)
if len(doc.Fields) > 0 {
fields := map[string]interface{}{}
for fieldName, fieldValue := range doc.Fields {
fields[fieldName] = fieldValue
}
temp, err = json.Marshal(fields)
if err != nil {
return Response{}, err
}
temp = append(temp, '\n')
bulkData = append(bulkData, temp[:]...)
}
}
r := Request{
Conn: c,
IndexList: []string{index},
method: "POST",
api: "_bulk",
bulkData: bulkData,
}
return r.Run()
}
// Search executes a search query against an index
func (c *Connection) Search(query map[string]interface{}, indexList []string, typeList []string) (Response, error) {
r := Request{
Conn: c,
Query: query,
IndexList: indexList,
TypeList: typeList,
method: "POST",
api: "_search",
}
return r.Run()
}
// Get a typed document by its id
func (c *Connection) Get(index string, documentType string, id string, extraArgs url.Values) (Response, error) {
r := Request{
Conn: c,
IndexList: []string{index},
method: "GET",
api: documentType + "/" + id,
ExtraArgs: extraArgs,
}
return r.Run()
}
// Index indexes a Document
// The extraArgs is a list of url.Values that you can send to elasticsearch as
// URL arguments, for example, to control routing, ttl, version, op_type, etc.
func (c *Connection) Index(d Document, extraArgs url.Values) (Response, error) {
r := Request{
Conn: c,
Query: d.Fields,
IndexList: []string{d.Index.(string)},
TypeList: []string{d.Type},
ExtraArgs: extraArgs,
method: "POST",
}
if d.Id != nil {
r.method = "PUT"
r.id = d.Id.(string)
}
return r.Run()
}
// Delete deletes a Document d
// The extraArgs is a list of url.Values that you can send to elasticsearch as
// URL arguments, for example, to control routing.
func (c *Connection) Delete(d Document, extraArgs url.Values) (Response, error) {
r := Request{
Conn: c,
IndexList: []string{d.Index.(string)},
TypeList: []string{d.Type},
ExtraArgs: extraArgs,
method: "DELETE",
id: d.Id.(string),
}
return r.Run()
}
// Run executes an elasticsearch Request. It converts data to Json, sends the
// request and return the Response obtained
func (req *Request) Run() (Response, error) {
postData := []byte{}
// XXX : refactor this
if req.api == "_bulk" {
postData = req.bulkData
} else {
b, err := json.Marshal(req.Query)
if err != nil {
return Response{}, err
}
postData = b
}
reader := bytes.NewReader(postData)
client := http.DefaultClient
newReq, err := http.NewRequest(req.method, req.Url(), reader)
if err != nil {
return Response{}, err
}
if req.method == "POST" || req.method == "PUT" {
newReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
resp, err := client.Do(newReq)
if err != nil {
return Response{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Response{}, err
}
if resp.StatusCode > 201 && resp.StatusCode < 400 {
return Response{}, errors.New(string(body))
}
esResp := new(Response)
err = json.Unmarshal(body, &esResp)
if err != nil {
return Response{}, err
}
if esResp.Error != "" {
return Response{}, &SearchError{esResp.Error, esResp.Status}
}
return *esResp, nil
}
// Url builds a Request for a URL
func (r *Request) Url() string {
path := "/" + strings.Join(r.IndexList, ",")
if len(r.TypeList) > 0 {
path += "/" + strings.Join(r.TypeList, ",")
}
// XXX : for indexing documents using the normal (non bulk) API
if len(r.api) == 0 && len(r.id) > 0 {
path += "/" + r.id
}
path += "/" + r.api
u := url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%s", r.Conn.Host, r.Conn.Port),
Path: path,
RawQuery: r.ExtraArgs.Encode(),
}
return u.String()
}