| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"math/rand" |
| 12 |
"net/http" |
| 13 |
"net/http/httptest" |
| 14 |
"testing" |
| 15 |
) |
| 16 |
|
| 17 |
func TestBulk(t *testing.T) { |
| 18 |
client := setupTestClientAndCreateIndex(t) //, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 19 |
|
| 20 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 21 |
tweet2 := tweet{User: "sandrae", Message: "Dancing all night long. Yeah."} |
| 22 |
|
| 23 |
index1Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(tweet1) |
| 24 |
index2Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("2").Doc(tweet2) |
| 25 |
delete1Req := NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1") |
| 26 |
|
| 27 |
bulkRequest := client.Bulk() |
| 28 |
bulkRequest = bulkRequest.Add(index1Req) |
| 29 |
bulkRequest = bulkRequest.Add(index2Req) |
| 30 |
bulkRequest = bulkRequest.Add(delete1Req) |
| 31 |
|
| 32 |
if bulkRequest.NumberOfActions() != 3 { |
| 33 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 3, bulkRequest.NumberOfActions()) |
| 34 |
} |
| 35 |
|
| 36 |
bulkResponse, err := bulkRequest.Do(context.TODO()) |
| 37 |
if err != nil { |
| 38 |
t.Fatal(err) |
| 39 |
} |
| 40 |
if bulkResponse == nil { |
| 41 |
t.Errorf("expected bulkResponse to be != nil; got nil") |
| 42 |
} |
| 43 |
|
| 44 |
if bulkRequest.NumberOfActions() != 0 { |
| 45 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 0, bulkRequest.NumberOfActions()) |
| 46 |
} |
| 47 |
|
| 48 |
// Document with Id="1" should not exist |
| 49 |
exists, err := client.Exists().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO()) |
| 50 |
if err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
if exists { |
| 54 |
t.Errorf("expected exists %v; got %v", false, exists) |
| 55 |
} |
| 56 |
|
| 57 |
// Document with Id="2" should exist |
| 58 |
exists, err = client.Exists().Index(testIndexName).Type("tweet").Id("2").Do(context.TODO()) |
| 59 |
if err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
if !exists { |
| 63 |
t.Errorf("expected exists %v; got %v", true, exists) |
| 64 |
} |
| 65 |
|
| 66 |
// Update |
| 67 |
updateDoc := struct { |
| 68 |
Retweets int `json:"retweets"` |
| 69 |
}{ |
| 70 |
42, |
| 71 |
} |
| 72 |
update1Req := NewBulkUpdateRequest().Index(testIndexName).Type("tweet").Id("2").Doc(&updateDoc) |
| 73 |
bulkRequest = client.Bulk() |
| 74 |
bulkRequest = bulkRequest.Add(update1Req) |
| 75 |
|
| 76 |
if bulkRequest.NumberOfActions() != 1 { |
| 77 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 1, bulkRequest.NumberOfActions()) |
| 78 |
} |
| 79 |
|
| 80 |
bulkResponse, err = bulkRequest.Do(context.TODO()) |
| 81 |
if err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
if bulkResponse == nil { |
| 85 |
t.Errorf("expected bulkResponse to be != nil; got nil") |
| 86 |
} |
| 87 |
|
| 88 |
if bulkRequest.NumberOfActions() != 0 { |
| 89 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 0, bulkRequest.NumberOfActions()) |
| 90 |
} |
| 91 |
|
| 92 |
// Document with Id="1" should have a retweets count of 42 |
| 93 |
doc, err := client.Get().Index(testIndexName).Type("tweet").Id("2").Do(context.TODO()) |
| 94 |
if err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
if doc == nil { |
| 98 |
t.Fatal("expected doc to be != nil; got nil") |
| 99 |
} |
| 100 |
if !doc.Found { |
| 101 |
t.Fatalf("expected doc to be found; got found = %v", doc.Found) |
| 102 |
} |
| 103 |
if doc.Source == nil { |
| 104 |
t.Fatal("expected doc source to be != nil; got nil") |
| 105 |
} |
| 106 |
var updatedTweet tweet |
| 107 |
err = json.Unmarshal(*doc.Source, &updatedTweet) |
| 108 |
if err != nil { |
| 109 |
t.Fatal(err) |
| 110 |
} |
| 111 |
if updatedTweet.Retweets != 42 { |
| 112 |
t.Errorf("expected updated tweet retweets = %v; got %v", 42, updatedTweet.Retweets) |
| 113 |
} |
| 114 |
|
| 115 |
// Update with script |
| 116 |
update2Req := NewBulkUpdateRequest().Index(testIndexName).Type("tweet").Id("2"). |
| 117 |
RetryOnConflict(3). |
| 118 |
Script(NewScript("ctx._source.retweets += params.v").Param("v", 1)) |
| 119 |
bulkRequest = client.Bulk() |
| 120 |
bulkRequest = bulkRequest.Add(update2Req) |
| 121 |
if bulkRequest.NumberOfActions() != 1 { |
| 122 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 1, bulkRequest.NumberOfActions()) |
| 123 |
} |
| 124 |
bulkResponse, err = bulkRequest.Refresh("wait_for").Do(context.TODO()) |
| 125 |
if err != nil { |
| 126 |
t.Fatal(err) |
| 127 |
} |
| 128 |
if bulkResponse == nil { |
| 129 |
t.Errorf("expected bulkResponse to be != nil; got nil") |
| 130 |
} |
| 131 |
|
| 132 |
if bulkRequest.NumberOfActions() != 0 { |
| 133 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 0, bulkRequest.NumberOfActions()) |
| 134 |
} |
| 135 |
|
| 136 |
// Document with Id="1" should have a retweets count of 43 |
| 137 |
doc, err = client.Get().Index(testIndexName).Type("tweet").Id("2").Do(context.TODO()) |
| 138 |
if err != nil { |
| 139 |
t.Fatal(err) |
| 140 |
} |
| 141 |
if doc == nil { |
| 142 |
t.Fatal("expected doc to be != nil; got nil") |
| 143 |
} |
| 144 |
if !doc.Found { |
| 145 |
t.Fatalf("expected doc to be found; got found = %v", doc.Found) |
| 146 |
} |
| 147 |
if doc.Source == nil { |
| 148 |
t.Fatal("expected doc source to be != nil; got nil") |
| 149 |
} |
| 150 |
err = json.Unmarshal(*doc.Source, &updatedTweet) |
| 151 |
if err != nil { |
| 152 |
t.Fatal(err) |
| 153 |
} |
| 154 |
if updatedTweet.Retweets != 43 { |
| 155 |
t.Errorf("expected updated tweet retweets = %v; got %v", 43, updatedTweet.Retweets) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func TestBulkWithIndexSetOnClient(t *testing.T) { |
| 160 |
client := setupTestClientAndCreateIndex(t) |
| 161 |
|
| 162 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 163 |
tweet2 := tweet{User: "sandrae", Message: "Dancing all night long. Yeah."} |
| 164 |
|
| 165 |
index1Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(tweet1) |
| 166 |
index2Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("2").Doc(tweet2) |
| 167 |
delete1Req := NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1") |
| 168 |
|
| 169 |
bulkRequest := client.Bulk().Index(testIndexName).Type("tweet") |
| 170 |
bulkRequest = bulkRequest.Add(index1Req) |
| 171 |
bulkRequest = bulkRequest.Add(index2Req) |
| 172 |
bulkRequest = bulkRequest.Add(delete1Req) |
| 173 |
|
| 174 |
if bulkRequest.NumberOfActions() != 3 { |
| 175 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 3, bulkRequest.NumberOfActions()) |
| 176 |
} |
| 177 |
|
| 178 |
bulkResponse, err := bulkRequest.Do(context.TODO()) |
| 179 |
if err != nil { |
| 180 |
t.Fatal(err) |
| 181 |
} |
| 182 |
if bulkResponse == nil { |
| 183 |
t.Errorf("expected bulkResponse to be != nil; got nil") |
| 184 |
} |
| 185 |
|
| 186 |
// Document with Id="1" should not exist |
| 187 |
exists, err := client.Exists().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO()) |
| 188 |
if err != nil { |
| 189 |
t.Fatal(err) |
| 190 |
} |
| 191 |
if exists { |
| 192 |
t.Errorf("expected exists %v; got %v", false, exists) |
| 193 |
} |
| 194 |
|
| 195 |
// Document with Id="2" should exist |
| 196 |
exists, err = client.Exists().Index(testIndexName).Type("tweet").Id("2").Do(context.TODO()) |
| 197 |
if err != nil { |
| 198 |
t.Fatal(err) |
| 199 |
} |
| 200 |
if !exists { |
| 201 |
t.Errorf("expected exists %v; got %v", true, exists) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestBulkIndexDeleteUpdate(t *testing.T) { |
| 206 |
client := setupTestClientAndCreateIndex(t) |
| 207 |
//client := setupTestClientAndCreateIndexAndLog(t) |
| 208 |
|
| 209 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 210 |
tweet2 := tweet{User: "sandrae", Message: "Dancing all night long. Yeah."} |
| 211 |
|
| 212 |
index1Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(tweet1) |
| 213 |
index2Req := NewBulkIndexRequest().OpType("create").Index(testIndexName).Type("tweet").Id("2").Doc(tweet2) |
| 214 |
delete1Req := NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1") |
| 215 |
update2Req := NewBulkUpdateRequest().Index(testIndexName).Type("tweet").Id("2"). |
| 216 |
ReturnSource(true). |
| 217 |
Doc(struct { |
| 218 |
Retweets int `json:"retweets"` |
| 219 |
}{ |
| 220 |
Retweets: 42, |
| 221 |
}) |
| 222 |
|
| 223 |
bulkRequest := client.Bulk() |
| 224 |
bulkRequest = bulkRequest.Add(index1Req) |
| 225 |
bulkRequest = bulkRequest.Add(index2Req) |
| 226 |
bulkRequest = bulkRequest.Add(delete1Req) |
| 227 |
bulkRequest = bulkRequest.Add(update2Req) |
| 228 |
|
| 229 |
if bulkRequest.NumberOfActions() != 4 { |
| 230 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 4, bulkRequest.NumberOfActions()) |
| 231 |
} |
| 232 |
|
| 233 |
expected := `{"index":{"_id":"1","_index":"` + testIndexName + `","_type":"tweet"}} |
| 234 |
{"user":"olivere","message":"Welcome to Golang and Elasticsearch.","retweets":0,"created":"0001-01-01T00:00:00Z"} |
| 235 |
{"create":{"_id":"2","_index":"` + testIndexName + `","_type":"tweet"}} |
| 236 |
{"user":"sandrae","message":"Dancing all night long. Yeah.","retweets":0,"created":"0001-01-01T00:00:00Z"} |
| 237 |
{"delete":{"_id":"1","_index":"` + testIndexName + `","_type":"tweet"}} |
| 238 |
{"update":{"_id":"2","_index":"` + testIndexName + `","_type":"tweet"}} |
| 239 |
{"doc":{"retweets":42},"_source":true} |
| 240 |
` |
| 241 |
got, err := bulkRequest.bodyAsString() |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("expected no error, got: %v", err) |
| 244 |
} |
| 245 |
if got != expected { |
| 246 |
t.Errorf("expected\n%s\ngot:\n%s", expected, got) |
| 247 |
} |
| 248 |
|
| 249 |
// Run the bulk request |
| 250 |
bulkResponse, err := bulkRequest.Pretty(true).Do(context.TODO()) |
| 251 |
if err != nil { |
| 252 |
t.Fatal(err) |
| 253 |
} |
| 254 |
if bulkResponse == nil { |
| 255 |
t.Errorf("expected bulkResponse to be != nil; got nil") |
| 256 |
} |
| 257 |
if bulkResponse.Took == 0 { |
| 258 |
t.Errorf("expected took to be > 0; got %d", bulkResponse.Took) |
| 259 |
} |
| 260 |
if bulkResponse.Errors { |
| 261 |
t.Errorf("expected errors to be %v; got %v", false, bulkResponse.Errors) |
| 262 |
} |
| 263 |
if len(bulkResponse.Items) != 4 { |
| 264 |
t.Fatalf("expected 4 result items; got %d", len(bulkResponse.Items)) |
| 265 |
} |
| 266 |
|
| 267 |
// Indexed actions |
| 268 |
indexed := bulkResponse.Indexed() |
| 269 |
if indexed == nil { |
| 270 |
t.Fatal("expected indexed to be != nil; got nil") |
| 271 |
} |
| 272 |
if len(indexed) != 1 { |
| 273 |
t.Fatalf("expected len(indexed) == %d; got %d", 1, len(indexed)) |
| 274 |
} |
| 275 |
if indexed[0].Id != "1" { |
| 276 |
t.Errorf("expected indexed[0].Id == %s; got %s", "1", indexed[0].Id) |
| 277 |
} |
| 278 |
if indexed[0].Status != 201 { |
| 279 |
t.Errorf("expected indexed[0].Status == %d; got %d", 201, indexed[0].Status) |
| 280 |
} |
| 281 |
|
| 282 |
// Created actions |
| 283 |
created := bulkResponse.Created() |
| 284 |
if created == nil { |
| 285 |
t.Fatal("expected created to be != nil; got nil") |
| 286 |
} |
| 287 |
if len(created) != 1 { |
| 288 |
t.Fatalf("expected len(created) == %d; got %d", 1, len(created)) |
| 289 |
} |
| 290 |
if created[0].Id != "2" { |
| 291 |
t.Errorf("expected created[0].Id == %s; got %s", "2", created[0].Id) |
| 292 |
} |
| 293 |
if created[0].Status != 201 { |
| 294 |
t.Errorf("expected created[0].Status == %d; got %d", 201, created[0].Status) |
| 295 |
} |
| 296 |
if want, have := "created", created[0].Result; want != have { |
| 297 |
t.Errorf("expected created[0].Result == %q; got %q", want, have) |
| 298 |
} |
| 299 |
|
| 300 |
// Deleted actions |
| 301 |
deleted := bulkResponse.Deleted() |
| 302 |
if deleted == nil { |
| 303 |
t.Fatal("expected deleted to be != nil; got nil") |
| 304 |
} |
| 305 |
if len(deleted) != 1 { |
| 306 |
t.Fatalf("expected len(deleted) == %d; got %d", 1, len(deleted)) |
| 307 |
} |
| 308 |
if deleted[0].Id != "1" { |
| 309 |
t.Errorf("expected deleted[0].Id == %s; got %s", "1", deleted[0].Id) |
| 310 |
} |
| 311 |
if deleted[0].Status != 200 { |
| 312 |
t.Errorf("expected deleted[0].Status == %d; got %d", 200, deleted[0].Status) |
| 313 |
} |
| 314 |
if !deleted[0].Found { |
| 315 |
t.Errorf("expected deleted[0].Found == %v; got %v", true, deleted[0].Found) |
| 316 |
} |
| 317 |
if want, have := "deleted", deleted[0].Result; want != have { |
| 318 |
t.Errorf("expected deleted[0].Result == %q; got %q", want, have) |
| 319 |
} |
| 320 |
|
| 321 |
// Updated actions |
| 322 |
updated := bulkResponse.Updated() |
| 323 |
if updated == nil { |
| 324 |
t.Fatal("expected updated to be != nil; got nil") |
| 325 |
} |
| 326 |
if len(updated) != 1 { |
| 327 |
t.Fatalf("expected len(updated) == %d; got %d", 1, len(updated)) |
| 328 |
} |
| 329 |
if updated[0].Id != "2" { |
| 330 |
t.Errorf("expected updated[0].Id == %s; got %s", "2", updated[0].Id) |
| 331 |
} |
| 332 |
if updated[0].Status != 200 { |
| 333 |
t.Errorf("expected updated[0].Status == %d; got %d", 200, updated[0].Status) |
| 334 |
} |
| 335 |
if updated[0].Version != 2 { |
| 336 |
t.Errorf("expected updated[0].Version == %d; got %d", 2, updated[0].Version) |
| 337 |
} |
| 338 |
if want, have := "updated", updated[0].Result; want != have { |
| 339 |
t.Errorf("expected updated[0].Result == %q; got %q", want, have) |
| 340 |
} |
| 341 |
if updated[0].GetResult == nil { |
| 342 |
t.Fatalf("expected updated[0].GetResult to be != nil; got nil") |
| 343 |
} |
| 344 |
if updated[0].GetResult.Source == nil { |
| 345 |
t.Fatalf("expected updated[0].GetResult.Source to be != nil; got nil") |
| 346 |
} |
| 347 |
if want, have := true, updated[0].GetResult.Found; want != have { |
| 348 |
t.Fatalf("expected updated[0].GetResult.Found to be != %v; got %v", want, have) |
| 349 |
} |
| 350 |
var doc tweet |
| 351 |
if err := json.Unmarshal(*updated[0].GetResult.Source, &doc); err != nil { |
| 352 |
t.Fatalf("expected to unmarshal updated[0].GetResult.Source; got %v", err) |
| 353 |
} |
| 354 |
if want, have := 42, doc.Retweets; want != have { |
| 355 |
t.Fatalf("expected updated tweet to have Retweets = %v; got %v", want, have) |
| 356 |
} |
| 357 |
|
| 358 |
// Succeeded actions |
| 359 |
succeeded := bulkResponse.Succeeded() |
| 360 |
if succeeded == nil { |
| 361 |
t.Fatal("expected succeeded to be != nil; got nil") |
| 362 |
} |
| 363 |
if len(succeeded) != 4 { |
| 364 |
t.Fatalf("expected len(succeeded) == %d; got %d", 4, len(succeeded)) |
| 365 |
} |
| 366 |
|
| 367 |
// ById |
| 368 |
id1Results := bulkResponse.ById("1") |
| 369 |
if id1Results == nil { |
| 370 |
t.Fatal("expected id1Results to be != nil; got nil") |
| 371 |
} |
| 372 |
if len(id1Results) != 2 { |
| 373 |
t.Fatalf("expected len(id1Results) == %d; got %d", 2, len(id1Results)) |
| 374 |
} |
| 375 |
if id1Results[0].Id != "1" { |
| 376 |
t.Errorf("expected id1Results[0].Id == %s; got %s", "1", id1Results[0].Id) |
| 377 |
} |
| 378 |
if id1Results[0].Status != 201 { |
| 379 |
t.Errorf("expected id1Results[0].Status == %d; got %d", 201, id1Results[0].Status) |
| 380 |
} |
| 381 |
if id1Results[0].Version != 1 { |
| 382 |
t.Errorf("expected id1Results[0].Version == %d; got %d", 1, id1Results[0].Version) |
| 383 |
} |
| 384 |
if id1Results[1].Id != "1" { |
| 385 |
t.Errorf("expected id1Results[1].Id == %s; got %s", "1", id1Results[1].Id) |
| 386 |
} |
| 387 |
if id1Results[1].Status != 200 { |
| 388 |
t.Errorf("expected id1Results[1].Status == %d; got %d", 200, id1Results[1].Status) |
| 389 |
} |
| 390 |
if id1Results[1].Version != 2 { |
| 391 |
t.Errorf("expected id1Results[1].Version == %d; got %d", 2, id1Results[1].Version) |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
func TestFailedBulkRequests(t *testing.T) { |
| 396 |
js := `{ |
| 397 |
"took" : 2, |
| 398 |
"errors" : true, |
| 399 |
"items" : [ { |
| 400 |
"index" : { |
| 401 |
"_index" : "elastic-test", |
| 402 |
"_type" : "tweet", |
| 403 |
"_id" : "1", |
| 404 |
"_version" : 1, |
| 405 |
"status" : 201 |
| 406 |
} |
| 407 |
}, { |
| 408 |
"create" : { |
| 409 |
"_index" : "elastic-test", |
| 410 |
"_type" : "tweet", |
| 411 |
"_id" : "2", |
| 412 |
"_version" : 1, |
| 413 |
"status" : 423, |
| 414 |
"error" : { |
| 415 |
"type":"routing_missing_exception", |
| 416 |
"reason":"routing is required for [elastic-test2]/[comment]/[1]" |
| 417 |
} |
| 418 |
} |
| 419 |
}, { |
| 420 |
"delete" : { |
| 421 |
"_index" : "elastic-test", |
| 422 |
"_type" : "tweet", |
| 423 |
"_id" : "1", |
| 424 |
"_version" : 2, |
| 425 |
"status" : 404, |
| 426 |
"found" : false |
| 427 |
} |
| 428 |
}, { |
| 429 |
"update" : { |
| 430 |
"_index" : "elastic-test", |
| 431 |
"_type" : "tweet", |
| 432 |
"_id" : "2", |
| 433 |
"_version" : 2, |
| 434 |
"status" : 200 |
| 435 |
} |
| 436 |
} ] |
| 437 |
}` |
| 438 |
|
| 439 |
var resp BulkResponse |
| 440 |
err := json.Unmarshal([]byte(js), &resp) |
| 441 |
if err != nil { |
| 442 |
t.Fatal(err) |
| 443 |
} |
| 444 |
failed := resp.Failed() |
| 445 |
if len(failed) != 2 { |
| 446 |
t.Errorf("expected %d failed items; got: %d", 2, len(failed)) |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
func TestBulkEstimatedSizeInBytes(t *testing.T) { |
| 451 |
client := setupTestClientAndCreateIndex(t) |
| 452 |
|
| 453 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 454 |
tweet2 := tweet{User: "sandrae", Message: "Dancing all night long. Yeah."} |
| 455 |
|
| 456 |
index1Req := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(tweet1) |
| 457 |
index2Req := NewBulkIndexRequest().OpType("create").Index(testIndexName).Type("tweet").Id("2").Doc(tweet2) |
| 458 |
delete1Req := NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1") |
| 459 |
update2Req := NewBulkUpdateRequest().Index(testIndexName).Type("tweet").Id("2"). |
| 460 |
Doc(struct { |
| 461 |
Retweets int `json:"retweets"` |
| 462 |
}{ |
| 463 |
Retweets: 42, |
| 464 |
}) |
| 465 |
|
| 466 |
bulkRequest := client.Bulk() |
| 467 |
bulkRequest = bulkRequest.Add(index1Req) |
| 468 |
bulkRequest = bulkRequest.Add(index2Req) |
| 469 |
bulkRequest = bulkRequest.Add(delete1Req) |
| 470 |
bulkRequest = bulkRequest.Add(update2Req) |
| 471 |
|
| 472 |
if bulkRequest.NumberOfActions() != 4 { |
| 473 |
t.Errorf("expected bulkRequest.NumberOfActions %d; got %d", 4, bulkRequest.NumberOfActions()) |
| 474 |
} |
| 475 |
|
| 476 |
// The estimated size of the bulk request in bytes must be at least |
| 477 |
// the length of the body request. |
| 478 |
raw, err := bulkRequest.bodyAsString() |
| 479 |
if err != nil { |
| 480 |
t.Fatal(err) |
| 481 |
} |
| 482 |
rawlen := int64(len([]byte(raw))) |
| 483 |
|
| 484 |
if got, want := bulkRequest.EstimatedSizeInBytes(), rawlen; got < want { |
| 485 |
t.Errorf("expected an EstimatedSizeInBytes = %d; got: %v", want, got) |
| 486 |
} |
| 487 |
|
| 488 |
// Reset should also reset the calculated estimated byte size |
| 489 |
bulkRequest.Reset() |
| 490 |
|
| 491 |
if got, want := bulkRequest.EstimatedSizeInBytes(), int64(0); got != want { |
| 492 |
t.Errorf("expected an EstimatedSizeInBytes = %d; got: %v", want, got) |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
func TestBulkEstimateSizeInBytesLength(t *testing.T) { |
| 497 |
client := setupTestClientAndCreateIndex(t) |
| 498 |
s := client.Bulk() |
| 499 |
r := NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1") |
| 500 |
s = s.Add(r) |
| 501 |
if got, want := s.estimateSizeInBytes(r), int64(1+len(r.String())); got != want { |
| 502 |
t.Fatalf("expected %d; got: %d", want, got) |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
var benchmarkBulkEstimatedSizeInBytes int64 |
| 507 |
|
| 508 |
func BenchmarkBulkEstimatedSizeInBytesWith1Request(b *testing.B) { |
| 509 |
client := setupTestClientAndCreateIndex(b) |
| 510 |
s := client.Bulk() |
| 511 |
var result int64 |
| 512 |
for n := 0; n < b.N; n++ { |
| 513 |
s = s.Add(NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(struct{ A string }{"1"})) |
| 514 |
s = s.Add(NewBulkUpdateRequest().Index(testIndexName).Type("tweet").Id("1").Doc(struct{ A string }{"2"})) |
| 515 |
s = s.Add(NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1")) |
| 516 |
result = s.EstimatedSizeInBytes() |
| 517 |
s.Reset() |
| 518 |
} |
| 519 |
b.ReportAllocs() |
| 520 |
benchmarkBulkEstimatedSizeInBytes = result // ensure the compiler doesn't optimize |
| 521 |
} |
| 522 |
|
| 523 |
func BenchmarkBulkEstimatedSizeInBytesWith100Requests(b *testing.B) { |
| 524 |
client := setupTestClientAndCreateIndex(b) |
| 525 |
s := client.Bulk() |
| 526 |
var result int64 |
| 527 |
for n := 0; n < b.N; n++ { |
| 528 |
for i := 0; i < 100; i++ { |
| 529 |
s = s.Add(NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(struct{ A string }{"1"})) |
| 530 |
s = s.Add(NewBulkUpdateRequest().Index(testIndexName).Type("tweet").Id("1").Doc(struct{ A string }{"2"})) |
| 531 |
s = s.Add(NewBulkDeleteRequest().Index(testIndexName).Type("tweet").Id("1")) |
| 532 |
} |
| 533 |
result = s.EstimatedSizeInBytes() |
| 534 |
s.Reset() |
| 535 |
} |
| 536 |
b.ReportAllocs() |
| 537 |
benchmarkBulkEstimatedSizeInBytes = result // ensure the compiler doesn't optimize |
| 538 |
} |
| 539 |
|
| 540 |
func TestBulkContentType(t *testing.T) { |
| 541 |
var header http.Header |
| 542 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 543 |
header = r.Header |
| 544 |
fmt.Fprintln(w, `{}`) |
| 545 |
})) |
| 546 |
defer ts.Close() |
| 547 |
|
| 548 |
client, err := NewSimpleClient(SetURL(ts.URL)) |
| 549 |
if err != nil { |
| 550 |
t.Fatal(err) |
| 551 |
} |
| 552 |
indexReq := NewBulkIndexRequest().Index(testIndexName).Type("tweet").Id("1").Doc(tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}) |
| 553 |
if _, err := client.Bulk().Add(indexReq).Do(context.Background()); err != nil { |
| 554 |
t.Fatal(err) |
| 555 |
} |
| 556 |
if header == nil { |
| 557 |
t.Fatalf("expected header, got %v", header) |
| 558 |
} |
| 559 |
if want, have := "application/x-ndjson", header.Get("Content-Type"); want != have { |
| 560 |
t.Fatalf("Content-Type: want %q, have %q", want, have) |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
// -- Benchmarks -- |
| 565 |
|
| 566 |
func BenchmarkBulkAllocs(b *testing.B) { |
| 567 |
b.Run("1000 docs with 64 byte", func(b *testing.B) { benchmarkBulkAllocs(b, 64, 1000) }) |
| 568 |
b.Run("1000 docs with 1 KiB", func(b *testing.B) { benchmarkBulkAllocs(b, 1024, 1000) }) |
| 569 |
b.Run("1000 docs with 4 KiB", func(b *testing.B) { benchmarkBulkAllocs(b, 4096, 1000) }) |
| 570 |
b.Run("1000 docs with 16 KiB", func(b *testing.B) { benchmarkBulkAllocs(b, 16*1024, 1000) }) |
| 571 |
b.Run("1000 docs with 64 KiB", func(b *testing.B) { benchmarkBulkAllocs(b, 64*1024, 1000) }) |
| 572 |
b.Run("1000 docs with 256 KiB", func(b *testing.B) { benchmarkBulkAllocs(b, 256*1024, 1000) }) |
| 573 |
b.Run("1000 docs with 1 MiB", func(b *testing.B) { benchmarkBulkAllocs(b, 1024*1024, 1000) }) |
| 574 |
} |
| 575 |
|
| 576 |
const ( |
| 577 |
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" |
| 578 |
) |
| 579 |
|
| 580 |
func benchmarkBulkAllocs(b *testing.B, size, num int) { |
| 581 |
buf := make([]byte, size) |
| 582 |
for i := range buf { |
| 583 |
buf[i] = charset[rand.Intn(len(charset))] |
| 584 |
} |
| 585 |
|
| 586 |
s := &BulkService{} |
| 587 |
n := 0 |
| 588 |
for { |
| 589 |
n++ |
| 590 |
s = s.Add(NewBulkIndexRequest().Index("test").Type("doc").Id("1").Doc(struct { |
| 591 |
S string `json:"s"` |
| 592 |
}{ |
| 593 |
S: string(buf), |
| 594 |
})) |
| 595 |
if n >= num { |
| 596 |
break |
| 597 |
} |
| 598 |
} |
| 599 |
for i := 0; i < b.N; i++ { |
| 600 |
s.bodyAsString() |
| 601 |
} |
| 602 |
b.ReportAllocs() |
| 603 |
} |