-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.go
212 lines (192 loc) · 5.57 KB
/
crud.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
package goose
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
)
type ElasticObject interface {
Key() string
}
// builds the path to an object. If object.BuildPath() string exists, it is called
// else, goose tries to create an index from the object `reflected` data
// Define precisely
// object.BuildPath(nil) string
// or buildPath will crash (index out of range or invalid type assertion)
//
// object.BuildPath() must return a string with the following constraints:
// - trailing slash
// - at least two character long (including the trailing slash)
func buildPath(object ElasticObject) (string, error) {
v := reflect.ValueOf(object)
t := v.Type()
if t == nil {
return "", errors.New("Object cannot be nil")
}
var path string
selfBuildPath := v.MethodByName("BuildPath")
if selfBuildPath.IsValid() {
valuePath := selfBuildPath.Call(nil)
// if len(valuePath) != 1 {
// return "", errors.New(fmt.Sprintf("%s.BuildPath() does not return a value.", t.String())) }
path = valuePath[0].Interface().(string)
// if !ok {
// return "", errors.New(fmt.Sprintf("%s.BuildPath() does not return a string (invalid type assertion).", t.String()))
// }
if len(path) < 2 || !strings.HasSuffix(path, "/") {
return path, errors.New(fmt.Sprintf("%s.BuildPath() returned invalid path.", t.String()))
}
} else {
path = fmt.Sprintf("%s__%s/", strings.Replace(t.PkgPath(), "/", "_", -1), strings.ToLower(t.Name()))
if len(path) == 2 {
return path, errors.New("Object cannot be an unnamed type.")
}
}
return path, nil
}
// adds an element to the index. Caller must ensure that id is unique for each inserted object.
func (se *ElasticSearch) Insert(object ElasticObject) error {
path, err := buildPath(object)
if err != nil {
return err
}
jsondata, err := json.Marshal(object)
if err != nil {
return err
}
body := strings.NewReader(string(jsondata))
return se.sendRequest(PUT, se.serverUrl+se.basePath+path+object.Key(), body)
}
// BulkInsert indexes several objects at once using the ES bulk API.
func (se *ElasticSearch) BulkInsert(objects []ElasticObject) error {
if len(objects) == 0 {
return errors.New("no object to bulk insert")
}
path, err := buildPath(objects[0])
if err != nil {
return err
}
type index struct {
Id string `json:"_id"`
}
// Index action
type action struct {
Index index `json:"index"`
}
var buf bytes.Buffer
for _, object := range objects {
// Index action then source on next line
for _, d := range []interface{}{&action{index{object.Key()}}, object} {
jsondata, err := json.Marshal(d)
if err != nil {
return err
}
_, err = buf.Write(jsondata)
if err != nil {
return err
}
buf.Write([]byte("\n")) // Required
}
}
return se.sendRequest(POST, se.serverUrl+se.basePath+path+actionBulk, &buf)
}
// updates an element in the index. TODO: check _update
func (se *ElasticSearch) Update(object ElasticObject) error {
path, err := buildPath(object)
if err != nil {
return err
}
jsondata, err := json.Marshal(Doc{Doc: object})
if err != nil {
return err
}
body := strings.NewReader(string(jsondata))
return se.sendRequest(POST, se.serverUrl+se.basePath+path+strictSlash(object.Key())+actionUpdate, body)
}
// adds an element to the index. Caller must ensure that id is unique for each inserted object.
func (se *ElasticSearch) Get(object ElasticObject) (bool, error) {
path, err := buildPath(object)
if err != nil {
return false, err
}
jsondata, err := json.Marshal(object)
if err != nil {
return false, err
}
body := strings.NewReader(string(jsondata))
resp, err := se.sendRequestAndGetResponse(GET, se.serverUrl+se.basePath+path+object.Key(), body)
defer resp.Body.Close()
if err != nil {
return false, err
}
dec := json.NewDecoder(resp.Body)
var res = new(result)
if err = dec.Decode(res); err != nil {
return false, err
}
if res.Found {
bj, _ := json.Marshal(res.Src)
err = json.Unmarshal(bj, object)
return true, err
}
return false, nil
}
// deletes an element from the index
func (se *ElasticSearch) Delete(object ElasticObject) error {
path, err := buildPath(object)
if err != nil {
return err
}
return se.sendRequest(DELETE, se.serverUrl+se.basePath+path+object.Key(), nil)
}
// deletes objects with a `query`
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
type DeletedIndex struct {
Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Failed int `json:"failed"`
} `json:"_shards"`
}
type deleteResponse struct {
Indices interface{} `json:"_indices"`
}
func (se *ElasticSearch) DeleteByQuery(object ElasticObject, q *QueryBuilder) (*DeletedIndex, error) {
if q == nil {
return nil, errors.New("Query is not valid")
}
path, err := buildPath(object)
if err != nil {
return nil, err
}
// delete query does not accept from and size, so set them to 0 for `omitempty` to be triggered
q.Size = 0
data, err := q.ToJSON()
if err != nil {
return nil, err
}
body := strings.NewReader(data)
resp, err := se.sendRequestAndGetResponse(DELETE, se.serverUrl+se.basePath+path+actionQuery, body)
defer resp.Body.Close()
if err != nil {
return nil, err
}
if resp == nil {
return nil, errors.New("No response from ES server")
}
dec := json.NewDecoder(resp.Body)
dresp := new(deleteResponse)
err = dec.Decode(dresp)
v := reflect.ValueOf(dresp.Indices)
keys := v.MapKeys()
for _, key := range keys {
js, _ := json.Marshal(v.MapIndex(key).Interface())
index := new(DeletedIndex)
if err = json.Unmarshal(js, index); err == nil {
return index, nil
}
}
return nil, nil
}