-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsearch_aggs_bucket_terms_integration_test.go
102 lines (92 loc) · 3.41 KB
/
search_aggs_bucket_terms_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"encoding/json"
"testing"
)
func TestAggsBucketTermsIntegration(t *testing.T) {
client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
resp, err := client.Search().
Index(testIndexName).
Query(NewMatchAllQuery()).
Aggregation("retweets", NewTermsAggregation().Field("retweets")).
Pretty(true).
Do(context.Background())
if err != nil {
t.Fatal(err)
}
if resp.Hits == nil {
t.Errorf("expected Hits != nil")
}
if want, have := int64(3), resp.TotalHits(); want != have {
t.Errorf("TotalHits(): want %d, have %d", want, have)
}
aggs := resp.Aggregations
if aggs == nil {
t.Fatalf("expected Aggregations != nil")
}
agg, found := aggs.Terms("retweets")
if !found {
t.Fatal("expected to find aggregation by name")
}
if agg == nil {
t.Fatalf("expected != nil")
}
if want, have := 3, len(agg.Buckets); want != have {
t.Fatalf("len(Buckets): want %d, have %d", want, have)
}
// Element #0
if want, have := float64(0), agg.Buckets[0].Key; want != have {
t.Errorf("Buckets[0].Key: want %v, have %v", want, have)
}
if want, have := json.Number("0"), agg.Buckets[0].KeyNumber; want != have {
t.Errorf("agg.Buckets[0].KeyNumber: want %q, have %q", want, have)
}
if have, err := agg.Buckets[0].KeyNumber.Int64(); err != nil {
t.Errorf("Buckets[0].KeyNumber.Int64(): %v", err)
} else if want := int64(0); want != have {
t.Errorf("Buckets[0].KeyNumber.Int64(): want %d, have %d", want, have)
}
if want, have := "0", agg.Buckets[0].KeyNumber.String(); want != have {
t.Errorf("Buckets[0].KeyNumber.String(): want %q, have %q", want, have)
}
if want, have := int64(1), agg.Buckets[0].DocCount; want != have {
t.Errorf("Buckets[0].DocCount: want %d, have %d", want, have)
}
// Element #1
if want, have := float64(12), agg.Buckets[1].Key; want != have {
t.Errorf("Buckets[1].Key: want %v, have %v", want, have)
}
if have, err := agg.Buckets[1].KeyNumber.Int64(); err != nil {
t.Errorf("Buckets[1].KeyNumber.Int64(): %v", err)
} else if want := int64(12); want != have {
t.Errorf("Buckets[1].KeyNumber.Int64(): want %d, have %d", want, have)
}
if want, have := "12", agg.Buckets[1].KeyNumber.String(); want != have {
t.Errorf("Buckets[1].KeyNumber.String(): want %q, have %q", want, have)
}
if want, have := int64(1), agg.Buckets[1].DocCount; want != have {
t.Errorf("Buckets[1].DocCount: want %d, have %d", want, have)
}
// Element #2
if want, have := float64(108), agg.Buckets[2].Key; want != have {
t.Errorf("Buckets[2].Key: want %v, have %v", want, have)
}
if want, have := json.Number("108"), agg.Buckets[2].KeyNumber; want != have {
t.Errorf("Buckets[2].KeyNumber: want %q, have %q", want, have)
}
if have, err := agg.Buckets[2].KeyNumber.Int64(); err != nil {
t.Errorf("Buckets[2].KeyNumber.Int64(): %v", err)
} else if want := int64(108); want != have {
t.Errorf("Buckets[2].KeyNumber.Int64(): want %d, have %d", want, have)
}
if want, have := "108", agg.Buckets[2].KeyNumber.String(); want != have {
t.Errorf("Buckets[2].KeyNumber.String(): want %q, have %q", want, have)
}
if want, have := int64(1), agg.Buckets[2].DocCount; want != have {
t.Errorf("Buckets[2].DocCount: want %d, have %d", want, have)
}
}