index_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 TestIndexLifecycle(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  13. // Add a document
  14. indexResult, err := client.Index().
  15. Index(testIndexName).
  16. Type("tweet").
  17. Id("1").
  18. BodyJson(&tweet1).
  19. Do(context.TODO())
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if indexResult == nil {
  24. t.Errorf("expected result to be != nil; got: %v", indexResult)
  25. }
  26. // Exists
  27. exists, err := client.Exists().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO())
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if !exists {
  32. t.Errorf("expected exists %v; got %v", true, exists)
  33. }
  34. // Get document
  35. getResult, err := client.Get().
  36. Index(testIndexName).
  37. Type("tweet").
  38. Id("1").
  39. Do(context.TODO())
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if getResult.Index != testIndexName {
  44. t.Errorf("expected GetResult.Index %q; got %q", testIndexName, getResult.Index)
  45. }
  46. if getResult.Type != "tweet" {
  47. t.Errorf("expected GetResult.Type %q; got %q", "tweet", getResult.Type)
  48. }
  49. if getResult.Id != "1" {
  50. t.Errorf("expected GetResult.Id %q; got %q", "1", getResult.Id)
  51. }
  52. if getResult.Source == nil {
  53. t.Errorf("expected GetResult.Source to be != nil; got nil")
  54. }
  55. // Decode the Source field
  56. var tweetGot tweet
  57. err = json.Unmarshal(*getResult.Source, &tweetGot)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if tweetGot.User != tweet1.User {
  62. t.Errorf("expected Tweet.User to be %q; got %q", tweet1.User, tweetGot.User)
  63. }
  64. if tweetGot.Message != tweet1.Message {
  65. t.Errorf("expected Tweet.Message to be %q; got %q", tweet1.Message, tweetGot.Message)
  66. }
  67. // Delete document again
  68. deleteResult, err := client.Delete().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO())
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. if deleteResult == nil {
  73. t.Errorf("expected result to be != nil; got: %v", deleteResult)
  74. }
  75. // Exists
  76. exists, err = client.Exists().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO())
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if exists {
  81. t.Errorf("expected exists %v; got %v", false, exists)
  82. }
  83. }
  84. func TestIndexLifecycleWithAutomaticIDGeneration(t *testing.T) {
  85. client := setupTestClientAndCreateIndex(t)
  86. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  87. // Add a document
  88. indexResult, err := client.Index().
  89. Index(testIndexName).
  90. Type("tweet").
  91. BodyJson(&tweet1).
  92. Do(context.TODO())
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. if indexResult == nil {
  97. t.Errorf("expected result to be != nil; got: %v", indexResult)
  98. }
  99. if indexResult.Id == "" {
  100. t.Fatalf("expected Es to generate an automatic ID, got: %v", indexResult.Id)
  101. }
  102. id := indexResult.Id
  103. // Exists
  104. exists, err := client.Exists().Index(testIndexName).Type("tweet").Id(id).Do(context.TODO())
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. if !exists {
  109. t.Errorf("expected exists %v; got %v", true, exists)
  110. }
  111. // Get document
  112. getResult, err := client.Get().
  113. Index(testIndexName).
  114. Type("tweet").
  115. Id(id).
  116. Do(context.TODO())
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. if getResult.Index != testIndexName {
  121. t.Errorf("expected GetResult.Index %q; got %q", testIndexName, getResult.Index)
  122. }
  123. if getResult.Type != "tweet" {
  124. t.Errorf("expected GetResult.Type %q; got %q", "tweet", getResult.Type)
  125. }
  126. if getResult.Id != id {
  127. t.Errorf("expected GetResult.Id %q; got %q", id, getResult.Id)
  128. }
  129. if getResult.Source == nil {
  130. t.Errorf("expected GetResult.Source to be != nil; got nil")
  131. }
  132. // Decode the Source field
  133. var tweetGot tweet
  134. err = json.Unmarshal(*getResult.Source, &tweetGot)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. if tweetGot.User != tweet1.User {
  139. t.Errorf("expected Tweet.User to be %q; got %q", tweet1.User, tweetGot.User)
  140. }
  141. if tweetGot.Message != tweet1.Message {
  142. t.Errorf("expected Tweet.Message to be %q; got %q", tweet1.Message, tweetGot.Message)
  143. }
  144. // Delete document again
  145. deleteResult, err := client.Delete().Index(testIndexName).Type("tweet").Id(id).Do(context.TODO())
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. if deleteResult == nil {
  150. t.Errorf("expected result to be != nil; got: %v", deleteResult)
  151. }
  152. // Exists
  153. exists, err = client.Exists().Index(testIndexName).Type("tweet").Id(id).Do(context.TODO())
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. if exists {
  158. t.Errorf("expected exists %v; got %v", false, exists)
  159. }
  160. }
  161. func TestIndexValidate(t *testing.T) {
  162. client := setupTestClient(t)
  163. tweet := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  164. // No index name -> fail with error
  165. res, err := NewIndexService(client).Type("tweet").Id("1").BodyJson(&tweet).Do(context.TODO())
  166. if err == nil {
  167. t.Fatalf("expected Index to fail without index name")
  168. }
  169. if res != nil {
  170. t.Fatalf("expected result to be == nil; got: %v", res)
  171. }
  172. // No index name -> fail with error
  173. res, err = NewIndexService(client).Index(testIndexName).Id("1").BodyJson(&tweet).Do(context.TODO())
  174. if err == nil {
  175. t.Fatalf("expected Index to fail without type")
  176. }
  177. if res != nil {
  178. t.Fatalf("expected result to be == nil; got: %v", res)
  179. }
  180. }
  181. func TestIndexCreateExistsOpenCloseDelete(t *testing.T) {
  182. // TODO: Find out how to make these test robust
  183. t.Skip("test fails regularly with 409 (Conflict): " +
  184. "IndexPrimaryShardNotAllocatedException[[elastic-test] " +
  185. "primary not allocated post api... skipping")
  186. client := setupTestClient(t)
  187. // Create index
  188. createIndex, err := client.CreateIndex(testIndexName).Body(testMapping).Do(context.TODO())
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. if createIndex == nil {
  193. t.Fatalf("expected response; got: %v", createIndex)
  194. }
  195. if !createIndex.Acknowledged {
  196. t.Errorf("expected ack for creating index; got: %v", createIndex.Acknowledged)
  197. }
  198. // Exists
  199. indexExists, err := client.IndexExists(testIndexName).Do(context.TODO())
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. if !indexExists {
  204. t.Fatalf("expected index exists=%v; got %v", true, indexExists)
  205. }
  206. // Flush
  207. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. // Close index
  212. closeIndex, err := client.CloseIndex(testIndexName).Do(context.TODO())
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. if closeIndex == nil {
  217. t.Fatalf("expected response; got: %v", closeIndex)
  218. }
  219. if !closeIndex.Acknowledged {
  220. t.Errorf("expected ack for closing index; got: %v", closeIndex.Acknowledged)
  221. }
  222. // Open index
  223. openIndex, err := client.OpenIndex(testIndexName).Do(context.TODO())
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. if openIndex == nil {
  228. t.Fatalf("expected response; got: %v", openIndex)
  229. }
  230. if !openIndex.Acknowledged {
  231. t.Errorf("expected ack for opening index; got: %v", openIndex.Acknowledged)
  232. }
  233. // Flush
  234. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. // Delete index
  239. deleteIndex, err := client.DeleteIndex(testIndexName).Do(context.TODO())
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. if deleteIndex == nil {
  244. t.Fatalf("expected response; got: %v", deleteIndex)
  245. }
  246. if !deleteIndex.Acknowledged {
  247. t.Errorf("expected ack for deleting index; got %v", deleteIndex.Acknowledged)
  248. }
  249. }