get_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 TestGet(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  13. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. // Get document 1
  18. res, err := client.Get().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO())
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. if res.Found != true {
  23. t.Errorf("expected Found = true; got %v", res.Found)
  24. }
  25. if res.Source == nil {
  26. t.Errorf("expected Source != nil; got %v", res.Source)
  27. }
  28. // Get non existent document 99
  29. res, err = client.Get().Index(testIndexName).Type("tweet").Id("99").Do(context.TODO())
  30. if err == nil {
  31. t.Fatalf("expected error; got: %v", err)
  32. }
  33. if !IsNotFound(err) {
  34. t.Errorf("expected NotFound error; got: %v", err)
  35. }
  36. if res != nil {
  37. t.Errorf("expected no response; got: %v", res)
  38. }
  39. }
  40. func TestGetWithSourceFiltering(t *testing.T) {
  41. client := setupTestClientAndCreateIndex(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  42. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  43. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. // Get document 1, without source
  48. res, err := client.Get().Index(testIndexName).Type("tweet").Id("1").FetchSource(false).Do(context.TODO())
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if res.Found != true {
  53. t.Errorf("expected Found = true; got %v", res.Found)
  54. }
  55. if res.Source != nil {
  56. t.Errorf("expected Source == nil; got %v", res.Source)
  57. }
  58. // Get document 1, exclude Message field
  59. fsc := NewFetchSourceContext(true).Exclude("message")
  60. res, err = client.Get().Index(testIndexName).Type("tweet").Id("1").FetchSourceContext(fsc).Do(context.TODO())
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if res.Found != true {
  65. t.Errorf("expected Found = true; got %v", res.Found)
  66. }
  67. if res.Source == nil {
  68. t.Errorf("expected Source != nil; got %v", res.Source)
  69. }
  70. var tw tweet
  71. err = json.Unmarshal(*res.Source, &tw)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. if tw.User != "olivere" {
  76. t.Errorf("expected user %q; got: %q", "olivere", tw.User)
  77. }
  78. if tw.Message != "" {
  79. t.Errorf("expected message %q; got: %q", "", tw.Message)
  80. }
  81. }
  82. func TestGetWithFields(t *testing.T) {
  83. client := setupTestClientAndCreateIndex(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
  84. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  85. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. // Get document 1, specifying fields
  90. res, err := client.Get().Index(testIndexName).Type("tweet").Id("1").StoredFields("message").Do(context.TODO())
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. if res.Found != true {
  95. t.Errorf("expected Found = true; got: %v", res.Found)
  96. }
  97. // We must NOT have the "user" field
  98. _, ok := res.Fields["user"]
  99. if ok {
  100. t.Fatalf("expected no field %q in document", "user")
  101. }
  102. // We must have the "message" field
  103. messageField, ok := res.Fields["message"]
  104. if !ok {
  105. t.Fatalf("expected field %q in document", "message")
  106. }
  107. // Depending on the version of elasticsearch the message field will be returned
  108. // as a string or a slice of strings. This test works in both cases.
  109. messageString, ok := messageField.(string)
  110. if !ok {
  111. messageArray, ok := messageField.([]interface{})
  112. if !ok {
  113. t.Fatalf("expected field %q to be a string or a slice of strings; got: %T", "message", messageField)
  114. } else {
  115. messageString, ok = messageArray[0].(string)
  116. if !ok {
  117. t.Fatalf("expected field %q to be a string or a slice of strings; got: %T", "message", messageField)
  118. }
  119. }
  120. }
  121. if messageString != tweet1.Message {
  122. t.Errorf("expected message %q; got: %q", tweet1.Message, messageString)
  123. }
  124. }
  125. func TestGetValidate(t *testing.T) {
  126. // Mitigate against http://stackoverflow.com/questions/27491738/elasticsearch-go-index-failures-no-feature-for-name
  127. client := setupTestClientAndCreateIndex(t)
  128. if _, err := client.Get().Do(context.TODO()); err == nil {
  129. t.Fatal("expected Get to fail")
  130. }
  131. if _, err := client.Get().Index(testIndexName).Do(context.TODO()); err == nil {
  132. t.Fatal("expected Get to fail")
  133. }
  134. if _, err := client.Get().Type("tweet").Do(context.TODO()); err == nil {
  135. t.Fatal("expected Get to fail")
  136. }
  137. if _, err := client.Get().Id("1").Do(context.TODO()); err == nil {
  138. t.Fatal("expected Get to fail")
  139. }
  140. if _, err := client.Get().Index(testIndexName).Type("tweet").Do(context.TODO()); err == nil {
  141. t.Fatal("expected Get to fail")
  142. }
  143. if _, err := client.Get().Type("tweet").Id("1").Do(context.TODO()); err == nil {
  144. t.Fatal("expected Get to fail")
  145. }
  146. }