scroll_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. "io"
  9. _ "net/http"
  10. "testing"
  11. )
  12. func TestScroll(t *testing.T) {
  13. // client := setupTestClientAndCreateIndexAndLog(t)
  14. client := setupTestClientAndCreateIndex(t)
  15. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  16. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  17. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  18. // Add all documents
  19. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. // Should return all documents. Just don't call Do yet!
  36. svc := client.Scroll(testIndexName).Size(1)
  37. pages := 0
  38. docs := 0
  39. for {
  40. res, err := svc.Do(context.TODO())
  41. if err == io.EOF {
  42. break
  43. }
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. if res == nil {
  48. t.Fatal("expected results != nil; got nil")
  49. }
  50. if res.Hits == nil {
  51. t.Fatal("expected results.Hits != nil; got nil")
  52. }
  53. if want, have := int64(3), res.Hits.TotalHits; want != have {
  54. t.Fatalf("expected results.Hits.TotalHits = %d; got %d", want, have)
  55. }
  56. if want, have := 1, len(res.Hits.Hits); want != have {
  57. t.Fatalf("expected len(results.Hits.Hits) = %d; got %d", want, have)
  58. }
  59. pages++
  60. for _, hit := range res.Hits.Hits {
  61. if hit.Index != testIndexName {
  62. t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  63. }
  64. item := make(map[string]interface{})
  65. err := json.Unmarshal(*hit.Source, &item)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. docs++
  70. }
  71. if len(res.ScrollId) == 0 {
  72. t.Fatalf("expected scrollId in results; got %q", res.ScrollId)
  73. }
  74. }
  75. if want, have := 3, pages; want != have {
  76. t.Fatalf("expected to retrieve %d pages; got %d", want, have)
  77. }
  78. if want, have := 3, docs; want != have {
  79. t.Fatalf("expected to retrieve %d hits; got %d", want, have)
  80. }
  81. err = svc.Clear(context.TODO())
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. _, err = svc.Do(context.TODO())
  86. if err == nil {
  87. t.Fatal("expected to fail")
  88. }
  89. }
  90. func TestScrollWithQueryAndSort(t *testing.T) {
  91. client := setupTestClientAndCreateIndex(t)
  92. // client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  93. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  94. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  95. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  96. // Add all documents
  97. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. // Create a scroll service that returns tweets from user olivere
  114. // and returns them sorted by "message", in reverse order.
  115. //
  116. // Just don't call Do yet!
  117. svc := client.Scroll(testIndexName).
  118. Query(NewTermQuery("user", "olivere")).
  119. Sort("message", false).
  120. Size(1)
  121. docs := 0
  122. pages := 0
  123. for {
  124. res, err := svc.Do(context.TODO())
  125. if err == io.EOF {
  126. break
  127. }
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if res == nil {
  135. t.Fatal("expected results != nil; got nil")
  136. }
  137. if res.Hits == nil {
  138. t.Fatal("expected results.Hits != nil; got nil")
  139. }
  140. if want, have := int64(2), res.Hits.TotalHits; want != have {
  141. t.Fatalf("expected results.Hits.TotalHits = %d; got %d", want, have)
  142. }
  143. if want, have := 1, len(res.Hits.Hits); want != have {
  144. t.Fatalf("expected len(results.Hits.Hits) = %d; got %d", want, have)
  145. }
  146. pages++
  147. for _, hit := range res.Hits.Hits {
  148. if hit.Index != testIndexName {
  149. t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  150. }
  151. item := make(map[string]interface{})
  152. err := json.Unmarshal(*hit.Source, &item)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. docs++
  157. }
  158. }
  159. if want, have := 2, pages; want != have {
  160. t.Fatalf("expected to retrieve %d pages; got %d", want, have)
  161. }
  162. if want, have := 2, docs; want != have {
  163. t.Fatalf("expected to retrieve %d hits; got %d", want, have)
  164. }
  165. }
  166. func TestScrollWithBody(t *testing.T) {
  167. // client := setupTestClientAndCreateIndexAndLog(t)
  168. client := setupTestClientAndCreateIndex(t)
  169. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch.", Retweets: 4}
  170. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic.", Retweets: 10}
  171. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun.", Retweets: 3}
  172. // Add all documents
  173. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. // Test with simple strings and a map
  190. var tests = []struct {
  191. Body interface{}
  192. ExpectedTotalHits int64
  193. ExpectedDocs int
  194. ExpectedPages int
  195. }{
  196. {
  197. Body: `{"query":{"match_all":{}}}`,
  198. ExpectedTotalHits: 3,
  199. ExpectedDocs: 3,
  200. ExpectedPages: 3,
  201. },
  202. {
  203. Body: `{"query":{"term":{"user":"olivere"}},"sort":["_doc"]}`,
  204. ExpectedTotalHits: 2,
  205. ExpectedDocs: 2,
  206. ExpectedPages: 2,
  207. },
  208. {
  209. Body: `{"query":{"term":{"user":"olivere"}},"sort":[{"retweets":"desc"}]}`,
  210. ExpectedTotalHits: 2,
  211. ExpectedDocs: 2,
  212. ExpectedPages: 2,
  213. },
  214. {
  215. Body: map[string]interface{}{
  216. "query": map[string]interface{}{
  217. "term": map[string]interface{}{
  218. "user": "olivere",
  219. },
  220. },
  221. "sort": []interface{}{"_doc"},
  222. },
  223. ExpectedTotalHits: 2,
  224. ExpectedDocs: 2,
  225. ExpectedPages: 2,
  226. },
  227. }
  228. for i, tt := range tests {
  229. // Should return all documents. Just don't call Do yet!
  230. svc := client.Scroll(testIndexName).Size(1).Body(tt.Body)
  231. pages := 0
  232. docs := 0
  233. for {
  234. res, err := svc.Do(context.TODO())
  235. if err == io.EOF {
  236. break
  237. }
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. if res == nil {
  242. t.Fatalf("#%d: expected results != nil; got nil", i)
  243. }
  244. if res.Hits == nil {
  245. t.Fatalf("#%d: expected results.Hits != nil; got nil", i)
  246. }
  247. if want, have := tt.ExpectedTotalHits, res.Hits.TotalHits; want != have {
  248. t.Fatalf("#%d: expected results.Hits.TotalHits = %d; got %d", i, want, have)
  249. }
  250. if want, have := 1, len(res.Hits.Hits); want != have {
  251. t.Fatalf("#%d: expected len(results.Hits.Hits) = %d; got %d", i, want, have)
  252. }
  253. pages++
  254. for _, hit := range res.Hits.Hits {
  255. if hit.Index != testIndexName {
  256. t.Fatalf("#%d: expected SearchResult.Hits.Hit.Index = %q; got %q", i, testIndexName, hit.Index)
  257. }
  258. item := make(map[string]interface{})
  259. err := json.Unmarshal(*hit.Source, &item)
  260. if err != nil {
  261. t.Fatalf("#%d: %v", i, err)
  262. }
  263. docs++
  264. }
  265. if len(res.ScrollId) == 0 {
  266. t.Fatalf("#%d: expected scrollId in results; got %q", i, res.ScrollId)
  267. }
  268. }
  269. if want, have := tt.ExpectedPages, pages; want != have {
  270. t.Fatalf("#%d: expected to retrieve %d pages; got %d", i, want, have)
  271. }
  272. if want, have := tt.ExpectedDocs, docs; want != have {
  273. t.Fatalf("#%d: expected to retrieve %d hits; got %d", i, want, have)
  274. }
  275. err = svc.Clear(context.TODO())
  276. if err != nil {
  277. t.Fatalf("#%d: failed to clear scroll context: %v", i, err)
  278. }
  279. _, err = svc.Do(context.TODO())
  280. if err == nil {
  281. t.Fatalf("#%d: expected to fail", i)
  282. }
  283. }
  284. }
  285. func TestScrollWithSlice(t *testing.T) {
  286. client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
  287. // Should return all documents. Just don't call Do yet!
  288. sliceQuery := NewSliceQuery().Id(0).Max(2)
  289. svc := client.Scroll(testIndexName).Slice(sliceQuery).Size(1)
  290. pages := 0
  291. docs := 0
  292. for {
  293. res, err := svc.Do(context.TODO())
  294. if err == io.EOF {
  295. break
  296. }
  297. if err != nil {
  298. t.Fatal(err)
  299. }
  300. if res == nil {
  301. t.Fatal("expected results != nil; got nil")
  302. }
  303. if res.Hits == nil {
  304. t.Fatal("expected results.Hits != nil; got nil")
  305. }
  306. pages++
  307. for _, hit := range res.Hits.Hits {
  308. if hit.Index != testIndexName {
  309. t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  310. }
  311. item := make(map[string]interface{})
  312. err := json.Unmarshal(*hit.Source, &item)
  313. if err != nil {
  314. t.Fatal(err)
  315. }
  316. docs++
  317. }
  318. if len(res.ScrollId) == 0 {
  319. t.Fatalf("expected scrollId in results; got %q", res.ScrollId)
  320. }
  321. }
  322. if pages == 0 {
  323. t.Fatal("expected to retrieve some pages")
  324. }
  325. if docs == 0 {
  326. t.Fatal("expected to retrieve some hits")
  327. }
  328. if err := svc.Clear(context.TODO()); err != nil {
  329. t.Fatal(err)
  330. }
  331. if _, err := svc.Do(context.TODO()); err == nil {
  332. t.Fatal("expected to fail")
  333. }
  334. }
  335. func TestScrollWithFilterPath(t *testing.T) {
  336. // client := setupTestClientAndCreateIndexAndLog(t)
  337. client := setupTestClientAndCreateIndex(t)
  338. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  339. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  340. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  341. // Add all documents
  342. _, err := client.Index().Index(testIndexName).Type("doc").Id("1").BodyJson(&tweet1).Do(context.TODO())
  343. if err != nil {
  344. t.Fatal(err)
  345. }
  346. _, err = client.Index().Index(testIndexName).Type("doc").Id("2").BodyJson(&tweet2).Do(context.TODO())
  347. if err != nil {
  348. t.Fatal(err)
  349. }
  350. _, err = client.Index().Index(testIndexName).Type("doc").Id("3").BodyJson(&tweet3).Do(context.TODO())
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. // Should return all documents. Just don't call Do yet!
  359. // Notice that we don't have to add "_scroll_id" to the FilterPath here:
  360. // It's been added automatically by the ScrollService.
  361. svc := client.Scroll(testIndexName).Size(1).
  362. FilterPath("hits.total", "hits.hits._index", "hits.hits._id", "hits.hits._source")
  363. pages := 0
  364. docs := 0
  365. for {
  366. res, err := svc.Do(context.TODO())
  367. if err == io.EOF {
  368. break
  369. }
  370. if err != nil {
  371. t.Fatal(err)
  372. }
  373. if res == nil {
  374. t.Fatal("expected results != nil; got nil")
  375. }
  376. if res.Hits == nil {
  377. t.Fatal("expected results.Hits != nil; got nil")
  378. }
  379. if want, have := int64(3), res.Hits.TotalHits; want != have {
  380. t.Fatalf("expected results.Hits.TotalHits = %d; got %d", want, have)
  381. }
  382. if want, have := 1, len(res.Hits.Hits); want != have {
  383. t.Fatalf("expected len(results.Hits.Hits) = %d; got %d", want, have)
  384. }
  385. pages++
  386. for _, hit := range res.Hits.Hits {
  387. if hit.Index != testIndexName {
  388. t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  389. }
  390. item := make(map[string]interface{})
  391. err := json.Unmarshal(*hit.Source, &item)
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. docs++
  396. }
  397. if len(res.ScrollId) == 0 {
  398. t.Fatalf("expected scrollId in results; got %q", res.ScrollId)
  399. }
  400. }
  401. if want, have := 3, pages; want != have {
  402. t.Fatalf("expected to retrieve %d pages; got %d", want, have)
  403. }
  404. if want, have := 3, docs; want != have {
  405. t.Fatalf("expected to retrieve %d hits; got %d", want, have)
  406. }
  407. err = svc.Clear(context.TODO())
  408. if err != nil {
  409. t.Fatal(err)
  410. }
  411. _, err = svc.Do(context.TODO())
  412. if err == nil {
  413. t.Fatal("expected to fail")
  414. }
  415. }