forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_test.go
361 lines (323 loc) · 10.5 KB
/
bulk_test.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
// Copyright 2012-2015 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"encoding/json"
"testing"
)
func TestBulk(t *testing.T) {
client := setupTestClientAndCreateIndex(t)
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
tweet2 := tweet{User: "sandrae", Message: "Dancing all night long. Yeah."}
index1Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(tweet1)
index2Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("2").Doc(tweet2)
delete1Req := NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1")
bulkRequest := client.Bulk()
bulkRequest = bulkRequest.Add(index1Req)
bulkRequest = bulkRequest.Add(index2Req)
bulkRequest = bulkRequest.Add(delete1Req)
if bulkRequest.NumberOfActions() != 3 {
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 3, bulkRequest.NumberOfActions())
}
bulkResponse, err := bulkRequest.Do()
if err != nil {
t.Fatal(err)
}
if bulkResponse == nil {
t.Errorf("expected bulkResponse to be != nil; got nil")
}
if bulkRequest.NumberOfActions() != 0 {
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 0, bulkRequest.NumberOfActions())
}
// Document with Id="1" should not exist
exists, err := client.Exists().Index(testIndexName).Type("tweet").Id("1").Do()
if err != nil {
t.Fatal(err)
}
if exists {
t.Errorf("expected exists %v; got %v", false, exists)
}
// Document with Id="2" should exist
exists, err = client.Exists().Index(testIndexName).Type("tweet").Id("2").Do()
if err != nil {
t.Fatal(err)
}
if !exists {
t.Errorf("expected exists %v; got %v", true, exists)
}
// Update
updateDoc := struct {
Retweets int `json:"retweets"`
}{
42,
}
update1Req := NewBulkUpdateRequest().Index(testIndexName).Type("tweet").Id("2").Doc(&updateDoc)
bulkRequest = client.Bulk()
bulkRequest = bulkRequest.Add(update1Req)
if bulkRequest.NumberOfActions() != 1 {
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 1, bulkRequest.NumberOfActions())
}
bulkResponse, err = bulkRequest.Do()
if err != nil {
t.Fatal(err)
}
if bulkResponse == nil {
t.Errorf("expected bulkResponse to be != nil; got nil")
}
if bulkRequest.NumberOfActions() != 0 {
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 0, bulkRequest.NumberOfActions())
}
// Document with Id="1" should have a retweets count of 42
doc, err := client.Get().Index(testIndexName).Type("tweet").Id("2").Do()
if err != nil {
t.Fatal(err)
}
if doc == nil {
t.Fatal("expected doc to be != nil; got nil")
}
if !doc.Found {
t.Fatalf("expected doc to be found; got found = %v", doc.Found)
}
if doc.Source == nil {
t.Fatal("expected doc source to be != nil; got nil")
}
var updatedTweet tweet
err = json.Unmarshal(*doc.Source, &updatedTweet)
if err != nil {
t.Fatal(err)
}
if updatedTweet.Retweets != 42 {
t.Errorf("expected updated tweet retweets = %v; got %v", 42, updatedTweet.Retweets)
}
}
func TestBulkWithIndexSetOnClient(t *testing.T) {
client := setupTestClientAndCreateIndex(t)
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
tweet2 := tweet{User: "sandrae", Message: "Dancing all night long. Yeah."}
index1Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(tweet1)
index2Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("2").Doc(tweet2)
delete1Req := NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1")
bulkRequest := client.Bulk().Index(testIndexName).Type("tweet")
bulkRequest = bulkRequest.Add(index1Req)
bulkRequest = bulkRequest.Add(index2Req)
bulkRequest = bulkRequest.Add(delete1Req)
if bulkRequest.NumberOfActions() != 3 {
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 3, bulkRequest.NumberOfActions())
}
bulkResponse, err := bulkRequest.Do()
if err != nil {
t.Fatal(err)
}
if bulkResponse == nil {
t.Errorf("expected bulkResponse to be != nil; got nil")
}
// Document with Id="1" should not exist
exists, err := client.Exists().Index(testIndexName).Type("tweet").Id("1").Do()
if err != nil {
t.Fatal(err)
}
if exists {
t.Errorf("expected exists %v; got %v", false, exists)
}
// Document with Id="2" should exist
exists, err = client.Exists().Index(testIndexName).Type("tweet").Id("2").Do()
if err != nil {
t.Fatal(err)
}
if !exists {
t.Errorf("expected exists %v; got %v", true, exists)
}
}
func TestBulkRequestsSerialization(t *testing.T) {
client := setupTestClientAndCreateIndex(t)
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
tweet2 := tweet{User: "sandrae", Message: "Dancing all night long. Yeah."}
index1Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(tweet1)
index2Req := NewBulkIndexRequest().OpType("create").Index(testIndexName).Type("tweet").Id("2").Doc(tweet2)
delete1Req := NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1")
update2Req := NewBulkUpdateRequest().Index(testIndexName).Type("tweet").Id("2").
Doc(struct {
Retweets int `json:"retweets"`
}{
Retweets: 42,
})
bulkRequest := client.Bulk()
bulkRequest = bulkRequest.Add(index1Req)
bulkRequest = bulkRequest.Add(index2Req)
bulkRequest = bulkRequest.Add(delete1Req)
bulkRequest = bulkRequest.Add(update2Req)
if bulkRequest.NumberOfActions() != 4 {
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 4, bulkRequest.NumberOfActions())
}
expected := `{"index":{"_id":"1","_index":"` + testIndexName + `","_type":"tweet"}}
{"user":"olivere","message":"Welcome to Golang and Elasticsearch.","retweets":0,"created":"0001-01-01T00:00:00Z"}
{"create":{"_id":"2","_index":"` + testIndexName + `","_type":"tweet"}}
{"user":"sandrae","message":"Dancing all night long. Yeah.","retweets":0,"created":"0001-01-01T00:00:00Z"}
{"delete":{"_id":"1","_index":"` + testIndexName + `","_type":"tweet"}}
{"update":{"_id":"2","_index":"` + testIndexName + `","_type":"tweet"}}
{"doc":{"retweets":42}}
`
got, err := bulkRequest.bodyAsString()
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if got != expected {
t.Errorf("expected\n%s\ngot:\n%s", expected, got)
}
// Run the bulk request
bulkResponse, err := bulkRequest.Do()
if err != nil {
t.Fatal(err)
}
if bulkResponse == nil {
t.Errorf("expected bulkResponse to be != nil; got nil")
}
if bulkResponse.Took == 0 {
t.Errorf("expected took to be > 0; got %d", bulkResponse.Took)
}
if bulkResponse.Errors {
t.Errorf("expected errors to be %v; got %v", false, bulkResponse.Errors)
}
if len(bulkResponse.Items) != 4 {
t.Fatalf("expected 4 result items; got %d", len(bulkResponse.Items))
}
// Indexed actions
indexed := bulkResponse.Indexed()
if indexed == nil {
t.Fatal("expected indexed to be != nil; got nil")
}
if len(indexed) != 1 {
t.Fatalf("expected len(indexed) == %d; got %d", 1, len(indexed))
}
if indexed[0].Id != "1" {
t.Errorf("expected indexed[0].Id == %s; got %s", "1", indexed[0].Id)
}
if indexed[0].Status != 201 {
t.Errorf("expected indexed[0].Status == %d; got %d", 201, indexed[0].Status)
}
// Created actions
created := bulkResponse.Created()
if created == nil {
t.Fatal("expected created to be != nil; got nil")
}
if len(created) != 1 {
t.Fatalf("expected len(created) == %d; got %d", 1, len(created))
}
if created[0].Id != "2" {
t.Errorf("expected created[0].Id == %s; got %s", "2", created[0].Id)
}
if created[0].Status != 201 {
t.Errorf("expected created[0].Status == %d; got %d", 201, created[0].Status)
}
// Deleted actions
deleted := bulkResponse.Deleted()
if deleted == nil {
t.Fatal("expected deleted to be != nil; got nil")
}
if len(deleted) != 1 {
t.Fatalf("expected len(deleted) == %d; got %d", 1, len(deleted))
}
if deleted[0].Id != "1" {
t.Errorf("expected deleted[0].Id == %s; got %s", "1", deleted[0].Id)
}
if deleted[0].Status != 200 {
t.Errorf("expected deleted[0].Status == %d; got %d", 200, deleted[0].Status)
}
if !deleted[0].Found {
t.Errorf("expected deleted[0].Found == %v; got %v", true, deleted[0].Found)
}
// Updated actions
updated := bulkResponse.Updated()
if updated == nil {
t.Fatal("expected updated to be != nil; got nil")
}
if len(updated) != 1 {
t.Fatalf("expected len(updated) == %d; got %d", 1, len(updated))
}
if updated[0].Id != "2" {
t.Errorf("expected updated[0].Id == %s; got %s", "2", updated[0].Id)
}
if updated[0].Status != 200 {
t.Errorf("expected updated[0].Status == %d; got %d", 200, updated[0].Status)
}
if updated[0].Version != 2 {
t.Errorf("expected updated[0].Version == %d; got %d", 2, updated[0].Version)
}
// ById
id1Results := bulkResponse.ById("1")
if id1Results == nil {
t.Fatal("expected id1Results to be != nil; got nil")
}
if len(id1Results) != 2 {
t.Fatalf("expected len(id1Results) == %d; got %d", 2, len(id1Results))
}
if id1Results[0].Id != "1" {
t.Errorf("expected id1Results[0].Id == %s; got %s", "1", id1Results[0].Id)
}
if id1Results[0].Status != 201 {
t.Errorf("expected id1Results[0].Status == %d; got %d", 201, id1Results[0].Status)
}
if id1Results[0].Version != 1 {
t.Errorf("expected id1Results[0].Version == %d; got %d", 1, id1Results[0].Version)
}
if id1Results[1].Id != "1" {
t.Errorf("expected id1Results[1].Id == %s; got %s", "1", id1Results[1].Id)
}
if id1Results[1].Status != 200 {
t.Errorf("expected id1Results[1].Status == %d; got %d", 200, id1Results[1].Status)
}
if id1Results[1].Version != 2 {
t.Errorf("expected id1Results[1].Version == %d; got %d", 2, id1Results[1].Version)
}
}
func TestFailedBulkRequests(t *testing.T) {
js := `{
"took" : 2,
"errors" : true,
"items" : [ {
"index" : {
"_index" : "elastic-test",
"_type" : "tweet",
"_id" : "1",
"_version" : 1,
"status" : 201
}
}, {
"create" : {
"_index" : "elastic-test",
"_type" : "tweet",
"_id" : "2",
"_version" : 1,
"status" : 423,
"error" : "Locked"
}
}, {
"delete" : {
"_index" : "elastic-test",
"_type" : "tweet",
"_id" : "1",
"_version" : 2,
"status" : 404,
"found" : false
}
}, {
"update" : {
"_index" : "elastic-test",
"_type" : "tweet",
"_id" : "2",
"_version" : 2,
"status" : 200
}
} ]
}`
var resp BulkResponse
err := json.Unmarshal([]byte(js), &resp)
if err != nil {
t.Fatal(err)
}
failed := resp.Failed()
if len(failed) != 2 {
t.Errorf("expected %d failed items; got: %d", 2, len(failed))
}
}