msearch_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. _ "net/http"
  9. "testing"
  10. )
  11. func TestMultiSearch(t *testing.T) {
  12. client := setupTestClientAndCreateIndex(t)
  13. tweet1 := tweet{
  14. User: "olivere",
  15. Message: "Welcome to Golang and Elasticsearch.",
  16. Tags: []string{"golang", "elasticsearch"},
  17. }
  18. tweet2 := tweet{
  19. User: "olivere",
  20. Message: "Another unrelated topic.",
  21. Tags: []string{"golang"},
  22. }
  23. tweet3 := tweet{
  24. User: "sandrae",
  25. Message: "Cycling is fun.",
  26. Tags: []string{"sports", "cycling"},
  27. }
  28. // Add all documents
  29. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. // Spawn two search queries with one roundtrip
  46. q1 := NewMatchAllQuery()
  47. q2 := NewTermQuery("tags", "golang")
  48. sreq1 := NewSearchRequest().Index(testIndexName, testIndexName2).
  49. Source(NewSearchSource().Query(q1).Size(10))
  50. sreq2 := NewSearchRequest().Index(testIndexName).Type("tweet").
  51. Source(NewSearchSource().Query(q2))
  52. searchResult, err := client.MultiSearch().
  53. Add(sreq1, sreq2).
  54. Do(context.TODO())
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if searchResult.Responses == nil {
  59. t.Fatal("expected responses != nil; got nil")
  60. }
  61. if len(searchResult.Responses) != 2 {
  62. t.Fatalf("expected 2 responses; got %d", len(searchResult.Responses))
  63. }
  64. sres := searchResult.Responses[0]
  65. if sres.Hits == nil {
  66. t.Errorf("expected Hits != nil; got nil")
  67. }
  68. if sres.Hits.TotalHits != 3 {
  69. t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits)
  70. }
  71. if len(sres.Hits.Hits) != 3 {
  72. t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits))
  73. }
  74. for _, hit := range sres.Hits.Hits {
  75. if hit.Index != testIndexName {
  76. t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  77. }
  78. item := make(map[string]interface{})
  79. err := json.Unmarshal(*hit.Source, &item)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. }
  84. sres = searchResult.Responses[1]
  85. if sres.Hits == nil {
  86. t.Errorf("expected Hits != nil; got nil")
  87. }
  88. if sres.Hits.TotalHits != 2 {
  89. t.Errorf("expected Hits.TotalHits = %d; got %d", 2, sres.Hits.TotalHits)
  90. }
  91. if len(sres.Hits.Hits) != 2 {
  92. t.Errorf("expected len(Hits.Hits) = %d; got %d", 2, len(sres.Hits.Hits))
  93. }
  94. for _, hit := range sres.Hits.Hits {
  95. if hit.Index != testIndexName {
  96. t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  97. }
  98. item := make(map[string]interface{})
  99. err := json.Unmarshal(*hit.Source, &item)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. }
  104. }
  105. func TestMultiSearchWithStrings(t *testing.T) {
  106. client := setupTestClientAndCreateIndex(t)
  107. // client := setupTestClientAndCreateIndexAndLog(t)
  108. tweet1 := tweet{
  109. User: "olivere",
  110. Message: "Welcome to Golang and Elasticsearch.",
  111. Tags: []string{"golang", "elasticsearch"},
  112. }
  113. tweet2 := tweet{
  114. User: "olivere",
  115. Message: "Another unrelated topic.",
  116. Tags: []string{"golang"},
  117. }
  118. tweet3 := tweet{
  119. User: "sandrae",
  120. Message: "Cycling is fun.",
  121. Tags: []string{"sports", "cycling"},
  122. }
  123. // Add all documents
  124. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. // Spawn two search queries with one roundtrip
  141. sreq1 := NewSearchRequest().Index(testIndexName, testIndexName2).
  142. Source(`{"query":{"match_all":{}}}`)
  143. sreq2 := NewSearchRequest().Index(testIndexName).Type("tweet").
  144. Source(`{"query":{"term":{"tags":"golang"}}}`)
  145. searchResult, err := client.MultiSearch().
  146. Add(sreq1, sreq2).
  147. Do(context.TODO())
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. if searchResult.Responses == nil {
  152. t.Fatal("expected responses != nil; got nil")
  153. }
  154. if len(searchResult.Responses) != 2 {
  155. t.Fatalf("expected 2 responses; got %d", len(searchResult.Responses))
  156. }
  157. sres := searchResult.Responses[0]
  158. if sres.Hits == nil {
  159. t.Errorf("expected Hits != nil; got nil")
  160. }
  161. if sres.Hits.TotalHits != 3 {
  162. t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits)
  163. }
  164. if len(sres.Hits.Hits) != 3 {
  165. t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits))
  166. }
  167. for _, hit := range sres.Hits.Hits {
  168. if hit.Index != testIndexName {
  169. t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  170. }
  171. item := make(map[string]interface{})
  172. err := json.Unmarshal(*hit.Source, &item)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. }
  177. sres = searchResult.Responses[1]
  178. if sres.Hits == nil {
  179. t.Errorf("expected Hits != nil; got nil")
  180. }
  181. if sres.Hits.TotalHits != 2 {
  182. t.Errorf("expected Hits.TotalHits = %d; got %d", 2, sres.Hits.TotalHits)
  183. }
  184. if len(sres.Hits.Hits) != 2 {
  185. t.Errorf("expected len(Hits.Hits) = %d; got %d", 2, len(sres.Hits.Hits))
  186. }
  187. for _, hit := range sres.Hits.Hits {
  188. if hit.Index != testIndexName {
  189. t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  190. }
  191. item := make(map[string]interface{})
  192. err := json.Unmarshal(*hit.Source, &item)
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. }
  197. }
  198. func TestMultiSearchWithOneRequest(t *testing.T) {
  199. client := setupTestClientAndCreateIndex(t)
  200. tweet1 := tweet{
  201. User: "olivere",
  202. Message: "Welcome to Golang and Elasticsearch.",
  203. Tags: []string{"golang", "elasticsearch"},
  204. }
  205. tweet2 := tweet{
  206. User: "olivere",
  207. Message: "Another unrelated topic.",
  208. Tags: []string{"golang"},
  209. }
  210. tweet3 := tweet{
  211. User: "sandrae",
  212. Message: "Cycling is fun.",
  213. Tags: []string{"sports", "cycling"},
  214. }
  215. // Add all documents
  216. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  217. if err != nil {
  218. t.Fatal(err)
  219. }
  220. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. // Spawn two search queries with one roundtrip
  233. query := NewMatchAllQuery()
  234. source := NewSearchSource().Query(query).Size(10)
  235. sreq := NewSearchRequest().Source(source)
  236. searchResult, err := client.MultiSearch().
  237. Index(testIndexName).
  238. Add(sreq).
  239. Do(context.TODO())
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. if searchResult.Responses == nil {
  244. t.Fatal("expected responses != nil; got nil")
  245. }
  246. if len(searchResult.Responses) != 1 {
  247. t.Fatalf("expected 1 responses; got %d", len(searchResult.Responses))
  248. }
  249. sres := searchResult.Responses[0]
  250. if sres.Hits == nil {
  251. t.Errorf("expected Hits != nil; got nil")
  252. }
  253. if sres.Hits.TotalHits != 3 {
  254. t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits)
  255. }
  256. if len(sres.Hits.Hits) != 3 {
  257. t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits))
  258. }
  259. for _, hit := range sres.Hits.Hits {
  260. if hit.Index != testIndexName {
  261. t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  262. }
  263. item := make(map[string]interface{})
  264. err := json.Unmarshal(*hit.Source, &item)
  265. if err != nil {
  266. t.Fatal(err)
  267. }
  268. }
  269. }