Debian Codegraph @

search_test.go

raw
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
5 package elastic
6
7 import (
8 "context"
9 "encoding/json"
10 "reflect"
11 "testing"
12 "time"
13 )
14
15 func TestSearchMatchAll(t *testing.T) {
16 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
17 client := setupTestClientAndCreateIndexAndAddDocs(t)
18
19 // Match all should return all documents
20 searchResult, err := client.Search().
21 Index(testIndexName).
22 Query(NewMatchAllQuery()).
23 Size(100).
24 Pretty(true).
25 Do(context.TODO())
26 if err != nil {
27 t.Fatal(err)
28 }
29 if searchResult.Hits == nil {
30 t.Errorf("expected SearchResult.Hits != nil; got nil")
31 }
32 if got, want := searchResult.Hits.TotalHits, int64(12); got != want {
33 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
34 }
35 if got, want := len(searchResult.Hits.Hits), 12; got != want {
36 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
37 }
38
39 for _, hit := range searchResult.Hits.Hits {
40 if hit.Index != testIndexName {
41 t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
42 }
43 item := make(map[string]interface{})
44 err := json.Unmarshal(*hit.Source, &item)
45 if err != nil {
46 t.Fatal(err)
47 }
48 }
49 }
50
51 func TestSearchMatchAllWithRequestCacheDisabled(t *testing.T) {
52 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
53 client := setupTestClientAndCreateIndexAndAddDocs(t)
54
55 // Match all should return all documents, with request cache disabled
56 searchResult, err := client.Search().
57 Index(testIndexName).
58 Query(NewMatchAllQuery()).
59 Size(100).
60 Pretty(true).
61 RequestCache(false).
62 Do(context.TODO())
63 if err != nil {
64 t.Fatal(err)
65 }
66 if searchResult.Hits == nil {
67 t.Errorf("expected SearchResult.Hits != nil; got nil")
68 }
69 if got, want := searchResult.Hits.TotalHits, int64(12); got != want {
70 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
71 }
72 if got, want := len(searchResult.Hits.Hits), 12; got != want {
73 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
74 }
75 }
76
77 func BenchmarkSearchMatchAll(b *testing.B) {
78 client := setupTestClientAndCreateIndexAndAddDocs(b)
79
80 for n := 0; n < b.N; n++ {
81 // Match all should return all documents
82 all := NewMatchAllQuery()
83 searchResult, err := client.Search().Index(testIndexName).Query(all).Do(context.TODO())
84 if err != nil {
85 b.Fatal(err)
86 }
87 if searchResult.Hits == nil {
88 b.Errorf("expected SearchResult.Hits != nil; got nil")
89 }
90 if searchResult.Hits.TotalHits == 0 {
91 b.Errorf("expected SearchResult.Hits.TotalHits > %d; got %d", 0, searchResult.Hits.TotalHits)
92 }
93 }
94 }
95
96 func TestSearchResultTotalHits(t *testing.T) {
97 client := setupTestClientAndCreateIndexAndAddDocs(t)
98
99 count, err := client.Count(testIndexName).Do(context.TODO())
100 if err != nil {
101 t.Fatal(err)
102 }
103
104 all := NewMatchAllQuery()
105 searchResult, err := client.Search().Index(testIndexName).Query(all).Do(context.TODO())
106 if err != nil {
107 t.Fatal(err)
108 }
109
110 got := searchResult.TotalHits()
111 if got != count {
112 t.Fatalf("expected %d hits; got: %d", count, got)
113 }
114
115 // No hits
116 searchResult = &SearchResult{}
117 got = searchResult.TotalHits()
118 if got != 0 {
119 t.Errorf("expected %d hits; got: %d", 0, got)
120 }
121 }
122
123 func TestSearchResultWithProfiling(t *testing.T) {
124 client := setupTestClientAndCreateIndexAndAddDocs(t)
125
126 all := NewMatchAllQuery()
127 searchResult, err := client.Search().Index(testIndexName).Query(all).Profile(true).Do(context.TODO())
128 if err != nil {
129 t.Fatal(err)
130 }
131
132 if searchResult.Profile == nil {
133 t.Fatal("Profiled MatchAll query did not return profiling data with results")
134 }
135 }
136
137 func TestSearchResultEach(t *testing.T) {
138 client := setupTestClientAndCreateIndexAndAddDocs(t)
139
140 all := NewMatchAllQuery()
141 searchResult, err := client.Search().Index(testIndexName).Query(all).Do(context.TODO())
142 if err != nil {
143 t.Fatal(err)
144 }
145
146 // Iterate over non-ptr type
147 var aTweet tweet
148 count := 0
149 for _, item := range searchResult.Each(reflect.TypeOf(aTweet)) {
150 count++
151 _, ok := item.(tweet)
152 if !ok {
153 t.Fatalf("expected hit to be serialized as tweet; got: %v", reflect.ValueOf(item))
154 }
155 }
156 if count == 0 {
157 t.Errorf("expected to find some hits; got: %d", count)
158 }
159
160 // Iterate over ptr-type
161 count = 0
162 var aTweetPtr *tweet
163 for _, item := range searchResult.Each(reflect.TypeOf(aTweetPtr)) {
164 count++
165 tw, ok := item.(*tweet)
166 if !ok {
167 t.Fatalf("expected hit to be serialized as tweet; got: %v", reflect.ValueOf(item))
168 }
169 if tw == nil {
170 t.Fatal("expected hit to not be nil")
171 }
172 }
173 if count == 0 {
174 t.Errorf("expected to find some hits; got: %d", count)
175 }
176
177 // Does not iterate when no hits are found
178 searchResult = &SearchResult{Hits: nil}
179 count = 0
180 for _, item := range searchResult.Each(reflect.TypeOf(aTweet)) {
181 count++
182 _ = item
183 }
184 if count != 0 {
185 t.Errorf("expected to not find any hits; got: %d", count)
186 }
187 searchResult = &SearchResult{Hits: &SearchHits{Hits: make([]*SearchHit, 0)}}
188 count = 0
189 for _, item := range searchResult.Each(reflect.TypeOf(aTweet)) {
190 count++
191 _ = item
192 }
193 if count != 0 {
194 t.Errorf("expected to not find any hits; got: %d", count)
195 }
196 }
197
198 func TestSearchResultEachNoSource(t *testing.T) {
199 client := setupTestClientAndCreateIndexAndAddDocsNoSource(t)
200
201 all := NewMatchAllQuery()
202 searchResult, err := client.Search().Index(testIndexName).Type("tweet-nosource").Query(all).Do(context.TODO())
203 if err != nil {
204 t.Fatal(err)
205 }
206
207 // Iterate over non-ptr type
208 var aTweet tweet
209 count := 0
210 for _, item := range searchResult.Each(reflect.TypeOf(aTweet)) {
211 count++
212 tw, ok := item.(tweet)
213 if !ok {
214 t.Fatalf("expected hit to be serialized as tweet; got: %v", reflect.ValueOf(item))
215 }
216
217 if tw.User != "" {
218 t.Fatalf("expected no _source hit to be empty tweet; got: %v", reflect.ValueOf(item))
219 }
220 }
221 if count != 2 {
222 t.Errorf("expected to find 2 hits; got: %d", count)
223 }
224
225 // Iterate over ptr-type
226 count = 0
227 var aTweetPtr *tweet
228 for _, item := range searchResult.Each(reflect.TypeOf(aTweetPtr)) {
229 count++
230 tw, ok := item.(*tweet)
231 if !ok {
232 t.Fatalf("expected hit to be serialized as tweet; got: %v", reflect.ValueOf(item))
233 }
234 if tw != nil {
235 t.Fatal("expected hit to be nil")
236 }
237 }
238 if count != 2 {
239 t.Errorf("expected to find 2 hits; got: %d", count)
240 }
241 }
242
243 func TestSearchSorting(t *testing.T) {
244 client := setupTestClientAndCreateIndex(t)
245
246 tweet1 := tweet{
247 User: "olivere", Retweets: 108,
248 Message: "Welcome to Golang and Elasticsearch.",
249 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
250 }
251 tweet2 := tweet{
252 User: "olivere", Retweets: 0,
253 Message: "Another unrelated topic.",
254 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
255 }
256 tweet3 := tweet{
257 User: "sandrae", Retweets: 12,
258 Message: "Cycling is fun.",
259 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
260 }
261
262 // Add all documents
263 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
264 if err != nil {
265 t.Fatal(err)
266 }
267
268 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
269 if err != nil {
270 t.Fatal(err)
271 }
272
273 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
274 if err != nil {
275 t.Fatal(err)
276 }
277
278 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
279 if err != nil {
280 t.Fatal(err)
281 }
282
283 // Match all should return all documents
284 all := NewMatchAllQuery()
285 searchResult, err := client.Search().
286 Index(testIndexName).
287 Query(all).
288 Sort("created", false).
289 Timeout("1s").
290 Do(context.TODO())
291 if err != nil {
292 t.Fatal(err)
293 }
294 if searchResult.Hits == nil {
295 t.Errorf("expected SearchResult.Hits != nil; got nil")
296 }
297 if searchResult.Hits.TotalHits != 3 {
298 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
299 }
300 if len(searchResult.Hits.Hits) != 3 {
301 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
302 }
303
304 for _, hit := range searchResult.Hits.Hits {
305 if hit.Index != testIndexName {
306 t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
307 }
308 item := make(map[string]interface{})
309 err := json.Unmarshal(*hit.Source, &item)
310 if err != nil {
311 t.Fatal(err)
312 }
313 }
314 }
315
316 func TestSearchSortingBySorters(t *testing.T) {
317 client := setupTestClientAndCreateIndex(t)
318
319 tweet1 := tweet{
320 User: "olivere", Retweets: 108,
321 Message: "Welcome to Golang and Elasticsearch.",
322 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
323 }
324 tweet2 := tweet{
325 User: "olivere", Retweets: 0,
326 Message: "Another unrelated topic.",
327 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
328 }
329 tweet3 := tweet{
330 User: "sandrae", Retweets: 12,
331 Message: "Cycling is fun.",
332 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
333 }
334
335 // Add all documents
336 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
337 if err != nil {
338 t.Fatal(err)
339 }
340
341 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
342 if err != nil {
343 t.Fatal(err)
344 }
345
346 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
347 if err != nil {
348 t.Fatal(err)
349 }
350
351 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
352 if err != nil {
353 t.Fatal(err)
354 }
355
356 // Match all should return all documents
357 all := NewMatchAllQuery()
358 searchResult, err := client.Search().
359 Index(testIndexName).
360 Query(all).
361 SortBy(NewFieldSort("created").Desc(), NewScoreSort()).
362 Timeout("1s").
363 Do(context.TODO())
364 if err != nil {
365 t.Fatal(err)
366 }
367 if searchResult.Hits == nil {
368 t.Errorf("expected SearchResult.Hits != nil; got nil")
369 }
370 if searchResult.Hits.TotalHits != 3 {
371 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
372 }
373 if len(searchResult.Hits.Hits) != 3 {
374 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
375 }
376
377 for _, hit := range searchResult.Hits.Hits {
378 if hit.Index != testIndexName {
379 t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
380 }
381 item := make(map[string]interface{})
382 err := json.Unmarshal(*hit.Source, &item)
383 if err != nil {
384 t.Fatal(err)
385 }
386 }
387 }
388
389 func TestSearchSpecificFields(t *testing.T) {
390 // client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0)))
391 client := setupTestClientAndCreateIndex(t)
392
393 tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
394 tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
395 tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
396
397 // Add all documents
398 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
399 if err != nil {
400 t.Fatal(err)
401 }
402
403 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
404 if err != nil {
405 t.Fatal(err)
406 }
407
408 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
409 if err != nil {
410 t.Fatal(err)
411 }
412
413 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
414 if err != nil {
415 t.Fatal(err)
416 }
417
418 // Match all should return all documents
419 all := NewMatchAllQuery()
420 searchResult, err := client.Search().
421 Index(testIndexName).
422 Query(all).
423 StoredFields("message").
424 Do(context.TODO())
425 if err != nil {
426 t.Fatal(err)
427 }
428 if searchResult.Hits == nil {
429 t.Errorf("expected SearchResult.Hits != nil; got nil")
430 }
431 if searchResult.Hits.TotalHits != 3 {
432 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
433 }
434 if len(searchResult.Hits.Hits) != 3 {
435 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
436 }
437
438 for _, hit := range searchResult.Hits.Hits {
439 if hit.Index != testIndexName {
440 t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
441 }
442 if hit.Source != nil {
443 t.Fatalf("expected SearchResult.Hits.Hit.Source to be nil; got: %v", hit.Source)
444 }
445 if hit.Fields == nil {
446 t.Fatal("expected SearchResult.Hits.Hit.Fields to be != nil")
447 }
448 field, found := hit.Fields["message"]
449 if !found {
450 t.Errorf("expected SearchResult.Hits.Hit.Fields[%s] to be found", "message")
451 }
452 fields, ok := field.([]interface{})
453 if !ok {
454 t.Errorf("expected []interface{}; got: %v", reflect.TypeOf(fields))
455 }
456 if len(fields) != 1 {
457 t.Errorf("expected a field with 1 entry; got: %d", len(fields))
458 }
459 message, ok := fields[0].(string)
460 if !ok {
461 t.Errorf("expected a string; got: %v", reflect.TypeOf(fields[0]))
462 }
463 if message == "" {
464 t.Errorf("expected a message; got: %q", message)
465 }
466 }
467 }
468
469 func TestSearchExplain(t *testing.T) {
470 client := setupTestClientAndCreateIndex(t)
471 // client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", 0)))
472
473 tweet1 := tweet{
474 User: "olivere", Retweets: 108,
475 Message: "Welcome to Golang and Elasticsearch.",
476 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
477 }
478 tweet2 := tweet{
479 User: "olivere", Retweets: 0,
480 Message: "Another unrelated topic.",
481 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
482 }
483 tweet3 := tweet{
484 User: "sandrae", Retweets: 12,
485 Message: "Cycling is fun.",
486 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
487 }
488
489 // Add all documents
490 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
491 if err != nil {
492 t.Fatal(err)
493 }
494
495 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
496 if err != nil {
497 t.Fatal(err)
498 }
499
500 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
501 if err != nil {
502 t.Fatal(err)
503 }
504
505 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
506 if err != nil {
507 t.Fatal(err)
508 }
509
510 // Match all should return all documents
511 all := NewMatchAllQuery()
512 searchResult, err := client.Search().
513 Index(testIndexName).
514 Query(all).
515 Explain(true).
516 Timeout("1s").
517 // Pretty(true).
518 Do(context.TODO())
519 if err != nil {
520 t.Fatal(err)
521 }
522 if searchResult.Hits == nil {
523 t.Errorf("expected SearchResult.Hits != nil; got nil")
524 }
525 if searchResult.Hits.TotalHits != 3 {
526 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
527 }
528 if len(searchResult.Hits.Hits) != 3 {
529 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
530 }
531
532 for _, hit := range searchResult.Hits.Hits {
533 if hit.Index != testIndexName {
534 t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
535 }
536 if hit.Explanation == nil {
537 t.Fatal("expected search explanation")
538 }
539 if hit.Explanation.Value <= 0.0 {
540 t.Errorf("expected explanation value to be > 0.0; got: %v", hit.Explanation.Value)
541 }
542 if hit.Explanation.Description == "" {
543 t.Errorf("expected explanation description != %q; got: %q", "", hit.Explanation.Description)
544 }
545 }
546 }
547
548 func TestSearchSource(t *testing.T) {
549 client := setupTestClientAndCreateIndex(t)
550
551 tweet1 := tweet{
552 User: "olivere", Retweets: 108,
553 Message: "Welcome to Golang and Elasticsearch.",
554 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
555 }
556 tweet2 := tweet{
557 User: "olivere", Retweets: 0,
558 Message: "Another unrelated topic.",
559 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
560 }
561 tweet3 := tweet{
562 User: "sandrae", Retweets: 12,
563 Message: "Cycling is fun.",
564 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
565 }
566
567 // Add all documents
568 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
569 if err != nil {
570 t.Fatal(err)
571 }
572
573 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
574 if err != nil {
575 t.Fatal(err)
576 }
577
578 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
579 if err != nil {
580 t.Fatal(err)
581 }
582
583 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
584 if err != nil {
585 t.Fatal(err)
586 }
587
588 // Set up the request JSON manually to pass to the search service via Source()
589 source := map[string]interface{}{
590 "query": map[string]interface{}{
591 "match_all": map[string]interface{}{},
592 },
593 }
594
595 searchResult, err := client.Search().
596 Index(testIndexName).
597 Source(source). // sets the JSON request
598 Do(context.TODO())
599 if err != nil {
600 t.Fatal(err)
601 }
602 if searchResult.Hits == nil {
603 t.Errorf("expected SearchResult.Hits != nil; got nil")
604 }
605 if searchResult.Hits.TotalHits != 3 {
606 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
607 }
608 }
609
610 func TestSearchSourceWithString(t *testing.T) {
611 client := setupTestClientAndCreateIndex(t)
612
613 tweet1 := tweet{
614 User: "olivere", Retweets: 108,
615 Message: "Welcome to Golang and Elasticsearch.",
616 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
617 }
618 tweet2 := tweet{
619 User: "olivere", Retweets: 0,
620 Message: "Another unrelated topic.",
621 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
622 }
623 tweet3 := tweet{
624 User: "sandrae", Retweets: 12,
625 Message: "Cycling is fun.",
626 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
627 }
628
629 // Add all documents
630 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
631 if err != nil {
632 t.Fatal(err)
633 }
634
635 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
636 if err != nil {
637 t.Fatal(err)
638 }
639
640 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
641 if err != nil {
642 t.Fatal(err)
643 }
644
645 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
646 if err != nil {
647 t.Fatal(err)
648 }
649
650 searchResult, err := client.Search().
651 Index(testIndexName).
652 Source(`{"query":{"match_all":{}}}`). // sets the JSON request
653 Do(context.TODO())
654 if err != nil {
655 t.Fatal(err)
656 }
657 if searchResult.Hits == nil {
658 t.Errorf("expected SearchResult.Hits != nil; got nil")
659 }
660 if searchResult.Hits.TotalHits != 3 {
661 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
662 }
663 }
664
665 func TestSearchRawString(t *testing.T) {
666 // client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0)))
667 client := setupTestClientAndCreateIndex(t)
668
669 tweet1 := tweet{
670 User: "olivere", Retweets: 108,
671 Message: "Welcome to Golang and Elasticsearch.",
672 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
673 }
674 tweet2 := tweet{
675 User: "olivere", Retweets: 0,
676 Message: "Another unrelated topic.",
677 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
678 }
679 tweet3 := tweet{
680 User: "sandrae", Retweets: 12,
681 Message: "Cycling is fun.",
682 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
683 }
684
685 // Add all documents
686 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
687 if err != nil {
688 t.Fatal(err)
689 }
690
691 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
692 if err != nil {
693 t.Fatal(err)
694 }
695
696 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
697 if err != nil {
698 t.Fatal(err)
699 }
700
701 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
702 if err != nil {
703 t.Fatal(err)
704 }
705
706 query := RawStringQuery(`{"match_all":{}}`)
707 searchResult, err := client.Search().
708 Index(testIndexName).
709 Query(query).
710 Do(context.TODO())
711 if err != nil {
712 t.Fatal(err)
713 }
714 if searchResult.Hits == nil {
715 t.Errorf("expected SearchResult.Hits != nil; got nil")
716 }
717 if searchResult.Hits.TotalHits != 3 {
718 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
719 }
720 }
721
722 func TestSearchSearchSource(t *testing.T) {
723 client := setupTestClientAndCreateIndex(t)
724
725 tweet1 := tweet{
726 User: "olivere", Retweets: 108,
727 Message: "Welcome to Golang and Elasticsearch.",
728 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
729 }
730 tweet2 := tweet{
731 User: "olivere", Retweets: 0,
732 Message: "Another unrelated topic.",
733 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
734 }
735 tweet3 := tweet{
736 User: "sandrae", Retweets: 12,
737 Message: "Cycling is fun.",
738 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
739 }
740
741 // Add all documents
742 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
743 if err != nil {
744 t.Fatal(err)
745 }
746
747 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
748 if err != nil {
749 t.Fatal(err)
750 }
751
752 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
753 if err != nil {
754 t.Fatal(err)
755 }
756
757 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
758 if err != nil {
759 t.Fatal(err)
760 }
761
762 // Set up the search source manually and pass it to the search service via SearchSource()
763 ss := NewSearchSource().Query(NewMatchAllQuery()).From(0).Size(2)
764
765 // One can use ss.Source() to get to the raw interface{} that will be used
766 // as the search request JSON by the SearchService.
767
768 searchResult, err := client.Search().
769 Index(testIndexName).
770 SearchSource(ss). // sets the SearchSource
771 Do(context.TODO())
772 if err != nil {
773 t.Fatal(err)
774 }
775 if searchResult.Hits == nil {
776 t.Errorf("expected SearchResult.Hits != nil; got nil")
777 }
778 if searchResult.Hits.TotalHits != 3 {
779 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
780 }
781 if len(searchResult.Hits.Hits) != 2 {
782 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 2, len(searchResult.Hits.Hits))
783 }
784 }
785
786 func TestSearchInnerHitsOnHasChild(t *testing.T) {
787 client := setupTestClientAndCreateIndex(t)
788
789 // Check for valid ES version
790 esversion, err := client.ElasticsearchVersion(DefaultURL)
791 if err != nil {
792 t.Fatal(err)
793 }
794 if esversion < "1.5.0" {
795 t.Skip("InnerHits feature is only available for Elasticsearch 1.5+")
796 return
797 }
798
799 tweet1 := tweet{
800 User: "olivere", Retweets: 108,
801 Message: "Welcome to Golang and Elasticsearch.",
802 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
803 }
804 tweet2 := tweet{
805 User: "olivere", Retweets: 0,
806 Message: "Another unrelated topic.",
807 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
808 }
809 comment2a := comment{User: "sandrae", Comment: "What does that even mean?"}
810 tweet3 := tweet{
811 User: "sandrae", Retweets: 12,
812 Message: "Cycling is fun.",
813 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
814 }
815 comment3a := comment{User: "nico", Comment: "You bet."}
816 comment3b := comment{User: "olivere", Comment: "It sure is."}
817
818 // Add all documents
819 _, err = client.Index().Index(testIndexName).Type("tweet").Id("t1").BodyJson(&tweet1).Do(context.TODO())
820 if err != nil {
821 t.Fatal(err)
822 }
823 _, err = client.Index().Index(testIndexName).Type("tweet").Id("t2").BodyJson(&tweet2).Do(context.TODO())
824 if err != nil {
825 t.Fatal(err)
826 }
827 _, err = client.Index().Index(testIndexName).Type("comment").Id("c2a").Parent("t2").BodyJson(&comment2a).Do(context.TODO())
828 if err != nil {
829 t.Fatal(err)
830 }
831 _, err = client.Index().Index(testIndexName).Type("tweet").Id("t3").BodyJson(&tweet3).Do(context.TODO())
832 if err != nil {
833 t.Fatal(err)
834 }
835 _, err = client.Index().Index(testIndexName).Type("comment").Id("c3a").Parent("t3").BodyJson(&comment3a).Do(context.TODO())
836 if err != nil {
837 t.Fatal(err)
838 }
839 _, err = client.Index().Index(testIndexName).Type("comment").Id("c3b").Parent("t3").BodyJson(&comment3b).Do(context.TODO())
840 if err != nil {
841 t.Fatal(err)
842 }
843
844 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
845 if err != nil {
846 t.Fatal(err)
847 }
848
849 bq := NewBoolQuery()
850 bq = bq.Must(NewMatchAllQuery())
851 bq = bq.Filter(NewHasChildQuery("comment", NewMatchAllQuery()).
852 InnerHit(NewInnerHit().Name("comments")))
853
854 searchResult, err := client.Search().
855 Index(testIndexName).
856 Query(bq).
857 Pretty(true).
858 Do(context.TODO())
859 if err != nil {
860 t.Fatal(err)
861 }
862 if searchResult.Hits == nil {
863 t.Errorf("expected SearchResult.Hits != nil; got nil")
864 }
865 if searchResult.Hits.TotalHits != 2 {
866 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 2, searchResult.Hits.TotalHits)
867 }
868 if len(searchResult.Hits.Hits) != 2 {
869 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 2, len(searchResult.Hits.Hits))
870 }
871
872 hit := searchResult.Hits.Hits[0]
873 if hit.Id != "t2" {
874 t.Fatalf("expected tweet %q; got: %q", "t2", hit.Id)
875 }
876 if hit.InnerHits == nil {
877 t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
878 }
879 if len(hit.InnerHits) != 1 {
880 t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
881 }
882 innerHits, found := hit.InnerHits["comments"]
883 if !found {
884 t.Fatalf("expected inner hits for name %q", "comments")
885 }
886 if innerHits == nil || innerHits.Hits == nil {
887 t.Fatal("expected inner hits != nil")
888 }
889 if len(innerHits.Hits.Hits) != 1 {
890 t.Fatalf("expected %d inner hits; got: %d", 1, len(innerHits.Hits.Hits))
891 }
892 if innerHits.Hits.Hits[0].Id != "c2a" {
893 t.Fatalf("expected inner hit with id %q; got: %q", "c2a", innerHits.Hits.Hits[0].Id)
894 }
895
896 hit = searchResult.Hits.Hits[1]
897 if hit.Id != "t3" {
898 t.Fatalf("expected tweet %q; got: %q", "t3", hit.Id)
899 }
900 if hit.InnerHits == nil {
901 t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
902 }
903 if len(hit.InnerHits) != 1 {
904 t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
905 }
906 innerHits, found = hit.InnerHits["comments"]
907 if !found {
908 t.Fatalf("expected inner hits for name %q", "comments")
909 }
910 if innerHits == nil || innerHits.Hits == nil {
911 t.Fatal("expected inner hits != nil")
912 }
913 if len(innerHits.Hits.Hits) != 2 {
914 t.Fatalf("expected %d inner hits; got: %d", 2, len(innerHits.Hits.Hits))
915 }
916 if innerHits.Hits.Hits[0].Id != "c3a" {
917 t.Fatalf("expected inner hit with id %q; got: %q", "c3a", innerHits.Hits.Hits[0].Id)
918 }
919 if innerHits.Hits.Hits[1].Id != "c3b" {
920 t.Fatalf("expected inner hit with id %q; got: %q", "c3b", innerHits.Hits.Hits[1].Id)
921 }
922 }
923
924 func TestSearchInnerHitsOnHasParent(t *testing.T) {
925 client := setupTestClientAndCreateIndex(t)
926
927 // Check for valid ES version
928 esversion, err := client.ElasticsearchVersion(DefaultURL)
929 if err != nil {
930 t.Fatal(err)
931 }
932 if esversion < "1.5.0" {
933 t.Skip("InnerHits feature is only available for Elasticsearch 1.5+")
934 return
935 }
936
937 tweet1 := tweet{
938 User: "olivere", Retweets: 108,
939 Message: "Welcome to Golang and Elasticsearch.",
940 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
941 }
942 tweet2 := tweet{
943 User: "olivere", Retweets: 0,
944 Message: "Another unrelated topic.",
945 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
946 }
947 comment2a := comment{User: "sandrae", Comment: "What does that even mean?"}
948 tweet3 := tweet{
949 User: "sandrae", Retweets: 12,
950 Message: "Cycling is fun.",
951 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
952 }
953 comment3a := comment{User: "nico", Comment: "You bet."}
954 comment3b := comment{User: "olivere", Comment: "It sure is."}
955
956 // Add all documents
957 _, err = client.Index().Index(testIndexName).Type("tweet").Id("t1").BodyJson(&tweet1).Do(context.TODO())
958 if err != nil {
959 t.Fatal(err)
960 }
961 _, err = client.Index().Index(testIndexName).Type("tweet").Id("t2").BodyJson(&tweet2).Do(context.TODO())
962 if err != nil {
963 t.Fatal(err)
964 }
965 _, err = client.Index().Index(testIndexName).Type("comment").Id("c2a").Parent("t2").BodyJson(&comment2a).Do(context.TODO())
966 if err != nil {
967 t.Fatal(err)
968 }
969 _, err = client.Index().Index(testIndexName).Type("tweet").Id("t3").BodyJson(&tweet3).Do(context.TODO())
970 if err != nil {
971 t.Fatal(err)
972 }
973 _, err = client.Index().Index(testIndexName).Type("comment").Id("c3a").Parent("t3").BodyJson(&comment3a).Do(context.TODO())
974 if err != nil {
975 t.Fatal(err)
976 }
977 _, err = client.Index().Index(testIndexName).Type("comment").Id("c3b").Parent("t3").BodyJson(&comment3b).Do(context.TODO())
978 if err != nil {
979 t.Fatal(err)
980 }
981
982 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
983 if err != nil {
984 t.Fatal(err)
985 }
986
987 bq := NewBoolQuery()
988 bq = bq.Must(NewMatchAllQuery())
989 bq = bq.Filter(NewHasParentQuery("tweet", NewMatchAllQuery()).
990 InnerHit(NewInnerHit().Name("tweets")))
991
992 searchResult, err := client.Search().
993 Index(testIndexName).
994 Query(bq).
995 Pretty(true).
996 Do(context.TODO())
997 if err != nil {
998 t.Fatal(err)
999 }
1000 if searchResult.Hits == nil {
1001 t.Errorf("expected SearchResult.Hits != nil; got nil")
1002 }
1003 if searchResult.Hits.TotalHits != 3 {
1004 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
1005 }
1006 if len(searchResult.Hits.Hits) != 3 {
1007 t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
1008 }
1009
1010 hit := searchResult.Hits.Hits[0]
1011 if hit.Id != "c2a" {
1012 t.Fatalf("expected tweet %q; got: %q", "c2a", hit.Id)
1013 }
1014 if hit.InnerHits == nil {
1015 t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
1016 }
1017 if len(hit.InnerHits) != 1 {
1018 t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
1019 }
1020 innerHits, found := hit.InnerHits["tweets"]
1021 if !found {
1022 t.Fatalf("expected inner hits for name %q", "tweets")
1023 }
1024 if innerHits == nil || innerHits.Hits == nil {
1025 t.Fatal("expected inner hits != nil")
1026 }
1027 if len(innerHits.Hits.Hits) != 1 {
1028 t.Fatalf("expected %d inner hits; got: %d", 1, len(innerHits.Hits.Hits))
1029 }
1030 if innerHits.Hits.Hits[0].Id != "t2" {
1031 t.Fatalf("expected inner hit with id %q; got: %q", "t2", innerHits.Hits.Hits[0].Id)
1032 }
1033
1034 hit = searchResult.Hits.Hits[1]
1035 if hit.Id != "c3a" {
1036 t.Fatalf("expected tweet %q; got: %q", "c3a", hit.Id)
1037 }
1038 if hit.InnerHits == nil {
1039 t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
1040 }
1041 if len(hit.InnerHits) != 1 {
1042 t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
1043 }
1044 innerHits, found = hit.InnerHits["tweets"]
1045 if !found {
1046 t.Fatalf("expected inner hits for name %q", "tweets")
1047 }
1048 if innerHits == nil || innerHits.Hits == nil {
1049 t.Fatal("expected inner hits != nil")
1050 }
1051 if len(innerHits.Hits.Hits) != 1 {
1052 t.Fatalf("expected %d inner hits; got: %d", 1, len(innerHits.Hits.Hits))
1053 }
1054 if innerHits.Hits.Hits[0].Id != "t3" {
1055 t.Fatalf("expected inner hit with id %q; got: %q", "t3", innerHits.Hits.Hits[0].Id)
1056 }
1057
1058 hit = searchResult.Hits.Hits[2]
1059 if hit.Id != "c3b" {
1060 t.Fatalf("expected tweet %q; got: %q", "c3b", hit.Id)
1061 }
1062 if hit.InnerHits == nil {
1063 t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
1064 }
1065 if len(hit.InnerHits) != 1 {
1066 t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
1067 }
1068 innerHits, found = hit.InnerHits["tweets"]
1069 if !found {
1070 t.Fatalf("expected inner hits for name %q", "tweets")
1071 }
1072 if innerHits == nil || innerHits.Hits == nil {
1073 t.Fatal("expected inner hits != nil")
1074 }
1075 if len(innerHits.Hits.Hits) != 1 {
1076 t.Fatalf("expected %d inner hits; got: %d", 1, len(innerHits.Hits.Hits))
1077 }
1078 if innerHits.Hits.Hits[0].Id != "t3" {
1079 t.Fatalf("expected inner hit with id %q; got: %q", "t3", innerHits.Hits.Hits[0].Id)
1080 }
1081 }
1082
1083 func TestSearchBuildURL(t *testing.T) {
1084 client := setupTestClient(t)
1085
1086 tests := []struct {
1087 Indices []string
1088 Types []string
1089 Expected string
1090 }{
1091 {
1092 []string{},
1093 []string{},
1094 "/_search",
1095 },
1096 {
1097 []string{"index1"},
1098 []string{},
1099 "/index1/_search",
1100 },
1101 {
1102 []string{"index1", "index2"},
1103 []string{},
1104 "/index1%2Cindex2/_search",
1105 },
1106 {
1107 []string{},
1108 []string{"type1"},
1109 "/_all/type1/_search",
1110 },
1111 {
1112 []string{"index1"},
1113 []string{"type1"},
1114 "/index1/type1/_search",
1115 },
1116 {
1117 []string{"index1", "index2"},
1118 []string{"type1", "type2"},
1119 "/index1%2Cindex2/type1%2Ctype2/_search",
1120 },
1121 {
1122 []string{},
1123 []string{"type1", "type2"},
1124 "/_all/type1%2Ctype2/_search",
1125 },
1126 }
1127
1128 for i, test := range tests {
1129 path, _, err := client.Search().Index(test.Indices...).Type(test.Types...).buildURL()
1130 if err != nil {
1131 t.Errorf("case #%d: %v", i+1, err)
1132 continue
1133 }
1134 if path != test.Expected {
1135 t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
1136 }
1137 }
1138 }
1139
1140 func TestSearchFilterPath(t *testing.T) {
1141 // client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
1142 client := setupTestClientAndCreateIndexAndAddDocs(t)
1143
1144 // Match all should return all documents
1145 all := NewMatchAllQuery()
1146 searchResult, err := client.Search().
1147 Index(testIndexName).
1148 Type("tweet").
1149 Query(all).
1150 FilterPath(
1151 "took",
1152 "hits.hits._id",
1153 "hits.hits._source.user",
1154 "hits.hits._source.message",
1155 ).
1156 Timeout("1s").
1157 Pretty(true).
1158 Do(context.TODO())
1159 if err != nil {
1160 t.Fatal(err)
1161 }
1162 if searchResult.Hits == nil {
1163 t.Fatalf("expected SearchResult.Hits != nil; got nil")
1164 }
1165 // 0 because it was filtered out
1166 if want, got := int64(0), searchResult.Hits.TotalHits; want != got {
1167 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
1168 }
1169 if want, got := 3, len(searchResult.Hits.Hits); want != got {
1170 t.Fatalf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
1171 }
1172
1173 for _, hit := range searchResult.Hits.Hits {
1174 if want, got := "", hit.Index; want != got {
1175 t.Fatalf("expected index %q, got %q", want, got)
1176 }
1177 item := make(map[string]interface{})
1178 err := json.Unmarshal(*hit.Source, &item)
1179 if err != nil {
1180 t.Fatal(err)
1181 }
1182 // user field
1183 v, found := item["user"]
1184 if !found {
1185 t.Fatalf("expected SearchResult.Hits.Hit[%q] to be found", "user")
1186 }
1187 if v == "" {
1188 t.Fatalf("expected user field, got %v (%T)", v, v)
1189 }
1190 // No retweets field
1191 v, found = item["retweets"]
1192 if found {
1193 t.Fatalf("expected SearchResult.Hits.Hit[%q] to not be found, got %v", "retweets", v)
1194 }
1195 if v == "" {
1196 t.Fatalf("expected user field, got %v (%T)", v, v)
1197 }
1198 }
1199 }
1200
1201 func TestSearchAfter(t *testing.T) {
1202 // client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0)))
1203 client := setupTestClientAndCreateIndex(t)
1204
1205 tweet1 := tweet{
1206 User: "olivere", Retweets: 108,
1207 Message: "Welcome to Golang and Elasticsearch.",
1208 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
1209 }
1210 tweet2 := tweet{
1211 User: "olivere", Retweets: 0,
1212 Message: "Another unrelated topic.",
1213 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
1214 }
1215 tweet3 := tweet{
1216 User: "sandrae", Retweets: 12,
1217 Message: "Cycling is fun.",
1218 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
1219 }
1220
1221 // Add all documents
1222 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
1223 if err != nil {
1224 t.Fatal(err)
1225 }
1226
1227 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
1228 if err != nil {
1229 t.Fatal(err)
1230 }
1231
1232 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
1233 if err != nil {
1234 t.Fatal(err)
1235 }
1236
1237 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
1238 if err != nil {
1239 t.Fatal(err)
1240 }
1241
1242 searchResult, err := client.Search().
1243 Index(testIndexName).
1244 Query(NewMatchAllQuery()).
1245 SearchAfter("olivere").
1246 Sort("user", true).
1247 Do(context.TODO())
1248 if err != nil {
1249 t.Fatal(err)
1250 }
1251 if searchResult.Hits == nil {
1252 t.Errorf("expected SearchResult.Hits != nil; got nil")
1253 }
1254 if searchResult.Hits.TotalHits != 3 {
1255 t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
1256 }
1257 if want, got := 1, len(searchResult.Hits.Hits); want != got {
1258 t.Fatalf("expected len(SearchResult.Hits.Hits) = %d; got: %d", want, got)
1259 }
1260 hit := searchResult.Hits.Hits[0]
1261 if want, got := "3", hit.Id; want != got {
1262 t.Fatalf("expected tweet %q; got: %q", want, got)
1263 }
1264 }
1265
1266 func TestSearchResultWithFieldCollapsing(t *testing.T) {
1267 client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
1268
1269 searchResult, err := client.Search().
1270 Index(testIndexName).
1271 Type("tweet").
1272 Query(NewMatchAllQuery()).
1273 Collapse(NewCollapseBuilder("user")).
1274 Pretty(true).
1275 Do(context.TODO())
1276 if err != nil {
1277 t.Fatal(err)
1278 }
1279
1280 if searchResult.Hits == nil {
1281 t.Fatalf("expected SearchResult.Hits != nil; got nil")
1282 }
1283 if got := searchResult.Hits.TotalHits; got == 0 {
1284 t.Fatalf("expected SearchResult.Hits.TotalHits > 0; got %d", got)
1285 }
1286
1287 for _, hit := range searchResult.Hits.Hits {
1288 if hit.Index != testIndexName {
1289 t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
1290 }
1291 item := make(map[string]interface{})
1292 err := json.Unmarshal(*hit.Source, &item)
1293 if err != nil {
1294 t.Fatal(err)
1295 }
1296 if len(hit.Fields) == 0 {
1297 t.Fatal("expected fields in SearchResult")
1298 }
1299 usersVal, ok := hit.Fields["user"]
1300 if !ok {
1301 t.Fatalf("expected %q field in fields of SearchResult", "user")
1302 }
1303 users, ok := usersVal.([]interface{})
1304 if !ok {
1305 t.Fatalf("expected slice of strings in field of SearchResult, got %T", usersVal)
1306 }
1307 if len(users) != 1 {
1308 t.Fatalf("expected 1 entry in users slice, got %d", len(users))
1309 }
1310 }
1311 }
1312
1313 func TestSearchResultWithFieldCollapsingAndInnerHits(t *testing.T) {
1314 client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
1315
1316 searchResult, err := client.Search().
1317 Index(testIndexName).
1318 Type("tweet").
1319 Query(NewMatchAllQuery()).
1320 Collapse(
1321 NewCollapseBuilder("user").
1322 InnerHit(
1323 NewInnerHit().Name("last_tweets").Size(5).Sort("created", true),
1324 ).
1325 MaxConcurrentGroupRequests(4)).
1326 Pretty(true).
1327 Do(context.TODO())
1328 if err != nil {
1329 t.Fatal(err)
1330 }
1331
1332 if searchResult.Hits == nil {
1333 t.Fatalf("expected SearchResult.Hits != nil; got nil")
1334 }
1335 if got := searchResult.Hits.TotalHits; got == 0 {
1336 t.Fatalf("expected SearchResult.Hits.TotalHits > 0; got %d", got)
1337 }
1338
1339 for _, hit := range searchResult.Hits.Hits {
1340 if hit.Index != testIndexName {
1341 t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
1342 }
1343 item := make(map[string]interface{})
1344 err := json.Unmarshal(*hit.Source, &item)
1345 if err != nil {
1346 t.Fatal(err)
1347 }
1348 if len(hit.Fields) == 0 {
1349 t.Fatal("expected fields in SearchResult")
1350 }
1351 usersVal, ok := hit.Fields["user"]
1352 if !ok {
1353 t.Fatalf("expected %q field in fields of SearchResult", "user")
1354 }
1355 users, ok := usersVal.([]interface{})
1356 if !ok {
1357 t.Fatalf("expected slice of strings in field of SearchResult, got %T", usersVal)
1358 }
1359 if len(users) != 1 {
1360 t.Fatalf("expected 1 entry in users slice, got %d", len(users))
1361 }
1362 lastTweets, ok := hit.InnerHits["last_tweets"]
1363 if !ok {
1364 t.Fatalf("expected inner_hits named %q in SearchResult", "last_tweets")
1365 }
1366 if lastTweets == nil {
1367 t.Fatal("expected inner_hits in SearchResult")
1368 }
1369 }
1370 }