mget_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "testing"
  9. )
  10. func TestMultiGet(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  13. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  14. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  15. // Add some documents
  16. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. // Count documents
  33. count, err := client.Count(testIndexName).Do(context.TODO())
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if count != 3 {
  38. t.Errorf("expected Count = %d; got %d", 3, count)
  39. }
  40. // Get documents 1 and 3
  41. res, err := client.MultiGet().
  42. Add(NewMultiGetItem().Index(testIndexName).Type("tweet").Id("1")).
  43. Add(NewMultiGetItem().Index(testIndexName).Type("tweet").Id("3")).
  44. Do(context.TODO())
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if res == nil {
  49. t.Fatal("expected result to be != nil; got nil")
  50. }
  51. if res.Docs == nil {
  52. t.Fatal("expected result docs to be != nil; got nil")
  53. }
  54. if len(res.Docs) != 2 {
  55. t.Fatalf("expected to have 2 docs; got %d", len(res.Docs))
  56. }
  57. item := res.Docs[0]
  58. if item.Error != nil {
  59. t.Errorf("expected no error on item 0; got %v", item.Error)
  60. }
  61. if item.Source == nil {
  62. t.Errorf("expected Source != nil; got %v", item.Source)
  63. }
  64. var doc tweet
  65. if err := json.Unmarshal(*item.Source, &doc); err != nil {
  66. t.Fatalf("expected to unmarshal item Source; got %v", err)
  67. }
  68. if doc.Message != tweet1.Message {
  69. t.Errorf("expected Message of first tweet to be %q; got %q", tweet1.Message, doc.Message)
  70. }
  71. item = res.Docs[1]
  72. if item.Error != nil {
  73. t.Errorf("expected no error on item 1; got %v", item.Error)
  74. }
  75. if item.Source == nil {
  76. t.Errorf("expected Source != nil; got %v", item.Source)
  77. }
  78. if err := json.Unmarshal(*item.Source, &doc); err != nil {
  79. t.Fatalf("expected to unmarshal item Source; got %v", err)
  80. }
  81. if doc.Message != tweet3.Message {
  82. t.Errorf("expected Message of second tweet to be %q; got %q", tweet3.Message, doc.Message)
  83. }
  84. }