Debian Codegraph @

search_aggs_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 "strings"
11 "testing"
12 "time"
13 )
14
15 func TestAggs(t *testing.T) {
16 // client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
17 client := setupTestClientAndCreateIndex(t)
18
19 tweet1 := tweet{
20 User: "olivere",
21 Retweets: 108,
22 Message: "Welcome to Golang and Elasticsearch.",
23 Image: "http://golang.org/doc/gopher/gophercolor.png",
24 Tags: []string{"golang", "elasticsearch"},
25 Location: "48.1333,11.5667", // lat,lon
26 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
27 }
28 tweet2 := tweet{
29 User: "olivere",
30 Retweets: 0,
31 Message: "Another unrelated topic.",
32 Tags: []string{"golang"},
33 Location: "48.1189,11.4289", // lat,lon
34 Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
35 }
36 tweet3 := tweet{
37 User: "sandrae",
38 Retweets: 12,
39 Message: "Cycling is fun.",
40 Tags: []string{"sports", "cycling"},
41 Location: "47.7167,11.7167", // lat,lon
42 Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
43 }
44
45 // Add all documents
46 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
47 if err != nil {
48 t.Fatal(err)
49 }
50
51 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
52 if err != nil {
53 t.Fatal(err)
54 }
55
56 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
57 if err != nil {
58 t.Fatal(err)
59 }
60
61 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
62 if err != nil {
63 t.Fatal(err)
64 }
65
66 // Match all should return all documents
67 all := NewMatchAllQuery()
68
69 // Terms Aggregate by user name
70 globalAgg := NewGlobalAggregation()
71 usersAgg := NewTermsAggregation().Field("user").Size(10).OrderByCountDesc()
72 retweetsAgg := NewTermsAggregation().Field("retweets").Size(10)
73 avgRetweetsAgg := NewAvgAggregation().Field("retweets")
74 avgRetweetsWithMetaAgg := NewAvgAggregation().Field("retweetsMeta").Meta(map[string]interface{}{"meta": true})
75 minRetweetsAgg := NewMinAggregation().Field("retweets")
76 maxRetweetsAgg := NewMaxAggregation().Field("retweets")
77 sumRetweetsAgg := NewSumAggregation().Field("retweets")
78 statsRetweetsAgg := NewStatsAggregation().Field("retweets")
79 extstatsRetweetsAgg := NewExtendedStatsAggregation().Field("retweets")
80 valueCountRetweetsAgg := NewValueCountAggregation().Field("retweets")
81 percentilesRetweetsAgg := NewPercentilesAggregation().Field("retweets")
82 percentileRanksRetweetsAgg := NewPercentileRanksAggregation().Field("retweets").Values(25, 50, 75)
83 cardinalityAgg := NewCardinalityAggregation().Field("user")
84 significantTermsAgg := NewSignificantTermsAggregation().Field("message")
85 samplerAgg := NewSamplerAggregation().SubAggregation("tagged_with", NewTermsAggregation().Field("tags"))
86 retweetsRangeAgg := NewRangeAggregation().Field("retweets").Lt(10).Between(10, 100).Gt(100)
87 retweetsKeyedRangeAgg := NewRangeAggregation().Field("retweets").Keyed(true).Lt(10).Between(10, 100).Gt(100)
88 dateRangeAgg := NewDateRangeAggregation().Field("created").Lt("2012-01-01").Between("2012-01-01", "2013-01-01").Gt("2013-01-01")
89 missingTagsAgg := NewMissingAggregation().Field("tags")
90 retweetsHistoAgg := NewHistogramAggregation().Field("retweets").Interval(100)
91 dateHistoAgg := NewDateHistogramAggregation().Field("created").Interval("year")
92 dateHistoKeyedAgg := NewDateHistogramAggregation().Field("created").Interval("year").Keyed(true)
93 retweetsFilterAgg := NewFilterAggregation().Filter(
94 NewRangeQuery("created").Gte("2012-01-01").Lte("2012-12-31")).
95 SubAggregation("avgRetweetsSub", NewAvgAggregation().Field("retweets"))
96 queryFilterAgg := NewFilterAggregation().Filter(NewTermQuery("tags", "golang"))
97 topTagsHitsAgg := NewTopHitsAggregation().Sort("created", false).Size(5).FetchSource(true)
98 topTagsAgg := NewTermsAggregation().Field("tags").Size(3).SubAggregation("top_tag_hits", topTagsHitsAgg)
99 geoBoundsAgg := NewGeoBoundsAggregation().Field("location")
100 geoHashAgg := NewGeoHashGridAggregation().Field("location").Precision(5)
101 geoCentroidAgg := NewGeoCentroidAggregation().Field("location")
102
103 // Run query
104 builder := client.Search().Index(testIndexName).Query(all).Pretty(true)
105 builder = builder.Aggregation("global", globalAgg)
106 builder = builder.Aggregation("users", usersAgg)
107 builder = builder.Aggregation("retweets", retweetsAgg)
108 builder = builder.Aggregation("avgRetweets", avgRetweetsAgg)
109 builder = builder.Aggregation("avgRetweetsWithMeta", avgRetweetsWithMetaAgg)
110 builder = builder.Aggregation("minRetweets", minRetweetsAgg)
111 builder = builder.Aggregation("maxRetweets", maxRetweetsAgg)
112 builder = builder.Aggregation("sumRetweets", sumRetweetsAgg)
113 builder = builder.Aggregation("statsRetweets", statsRetweetsAgg)
114 builder = builder.Aggregation("extstatsRetweets", extstatsRetweetsAgg)
115 builder = builder.Aggregation("valueCountRetweets", valueCountRetweetsAgg)
116 builder = builder.Aggregation("percentilesRetweets", percentilesRetweetsAgg)
117 builder = builder.Aggregation("percentileRanksRetweets", percentileRanksRetweetsAgg)
118 builder = builder.Aggregation("usersCardinality", cardinalityAgg)
119 builder = builder.Aggregation("significantTerms", significantTermsAgg)
120 builder = builder.Aggregation("sample", samplerAgg)
121 builder = builder.Aggregation("retweetsRange", retweetsRangeAgg)
122 builder = builder.Aggregation("retweetsKeyedRange", retweetsKeyedRangeAgg)
123 builder = builder.Aggregation("dateRange", dateRangeAgg)
124 builder = builder.Aggregation("missingTags", missingTagsAgg)
125 builder = builder.Aggregation("retweetsHisto", retweetsHistoAgg)
126 builder = builder.Aggregation("dateHisto", dateHistoAgg)
127 builder = builder.Aggregation("dateHistoKeyed", dateHistoKeyedAgg)
128 builder = builder.Aggregation("retweetsFilter", retweetsFilterAgg)
129 builder = builder.Aggregation("queryFilter", queryFilterAgg)
130 builder = builder.Aggregation("top-tags", topTagsAgg)
131 builder = builder.Aggregation("viewport", geoBoundsAgg)
132 builder = builder.Aggregation("geohashed", geoHashAgg)
133 builder = builder.Aggregation("centroid", geoCentroidAgg)
134 // Unnamed filters
135 countByUserAgg := NewFiltersAggregation().
136 Filters(NewTermQuery("user", "olivere"), NewTermQuery("user", "sandrae"))
137 builder = builder.Aggregation("countByUser", countByUserAgg)
138 // Named filters
139 countByUserAgg2 := NewFiltersAggregation().
140 FilterWithName("olivere", NewTermQuery("user", "olivere")).
141 FilterWithName("sandrae", NewTermQuery("user", "sandrae"))
142 builder = builder.Aggregation("countByUser2", countByUserAgg2)
143 // AvgBucket
144 dateHisto := NewDateHistogramAggregation().Field("created").Interval("year")
145 dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
146 builder = builder.Aggregation("avgBucketDateHisto", dateHisto)
147 builder = builder.Aggregation("avgSumOfRetweets", NewAvgBucketAggregation().BucketsPath("avgBucketDateHisto>sumOfRetweets"))
148 // MinBucket
149 dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
150 dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
151 builder = builder.Aggregation("minBucketDateHisto", dateHisto)
152 builder = builder.Aggregation("minBucketSumOfRetweets", NewMinBucketAggregation().BucketsPath("minBucketDateHisto>sumOfRetweets"))
153 // MaxBucket
154 dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
155 dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
156 builder = builder.Aggregation("maxBucketDateHisto", dateHisto)
157 builder = builder.Aggregation("maxBucketSumOfRetweets", NewMaxBucketAggregation().BucketsPath("maxBucketDateHisto>sumOfRetweets"))
158 // SumBucket
159 dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
160 dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
161 builder = builder.Aggregation("sumBucketDateHisto", dateHisto)
162 builder = builder.Aggregation("sumBucketSumOfRetweets", NewSumBucketAggregation().BucketsPath("sumBucketDateHisto>sumOfRetweets"))
163 // MovAvg
164 dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
165 dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
166 dateHisto = dateHisto.SubAggregation("movingAvg", NewMovAvgAggregation().BucketsPath("sumOfRetweets"))
167 builder = builder.Aggregation("movingAvgDateHisto", dateHisto)
168
169 searchResult, err := builder.Do(context.TODO())
170 if err != nil {
171 t.Fatal(err)
172 }
173 if searchResult.Hits == nil {
174 t.Errorf("expected Hits != nil; got: nil")
175 }
176 if searchResult.Hits.TotalHits != 3 {
177 t.Errorf("expected Hits.TotalHits = %d; got: %d", 3, searchResult.Hits.TotalHits)
178 }
179 if len(searchResult.Hits.Hits) != 3 {
180 t.Errorf("expected len(Hits.Hits) = %d; got: %d", 3, len(searchResult.Hits.Hits))
181 }
182 agg := searchResult.Aggregations
183 if agg == nil {
184 t.Fatalf("expected Aggregations != nil; got: nil")
185 }
186
187 // Search for non-existent aggregate should return (nil, false)
188 unknownAgg, found := agg.Terms("no-such-aggregate")
189 if found {
190 t.Errorf("expected unknown aggregation to not be found; got: %v", found)
191 }
192 if unknownAgg != nil {
193 t.Errorf("expected unknown aggregation to return %v; got %v", nil, unknownAgg)
194 }
195
196 // Global
197 globalAggRes, found := agg.Global("global")
198 if !found {
199 t.Errorf("expected %v; got: %v", true, found)
200 }
201 if globalAggRes == nil {
202 t.Fatalf("expected != nil; got: nil")
203 }
204 if globalAggRes.DocCount != 3 {
205 t.Errorf("expected DocCount = %d; got: %d", 3, globalAggRes.DocCount)
206 }
207
208 // Search for existent aggregate (by name) should return (aggregate, true)
209 termsAggRes, found := agg.Terms("users")
210 if !found {
211 t.Errorf("expected %v; got: %v", true, found)
212 }
213 if termsAggRes == nil {
214 t.Fatalf("expected != nil; got: nil")
215 }
216 if len(termsAggRes.Buckets) != 2 {
217 t.Fatalf("expected %d; got: %d", 2, len(termsAggRes.Buckets))
218 }
219 if termsAggRes.Buckets[0].Key != "olivere" {
220 t.Errorf("expected %q; got: %q", "olivere", termsAggRes.Buckets[0].Key)
221 }
222 if termsAggRes.Buckets[0].DocCount != 2 {
223 t.Errorf("expected %d; got: %d", 2, termsAggRes.Buckets[0].DocCount)
224 }
225 if termsAggRes.Buckets[1].Key != "sandrae" {
226 t.Errorf("expected %q; got: %q", "sandrae", termsAggRes.Buckets[1].Key)
227 }
228 if termsAggRes.Buckets[1].DocCount != 1 {
229 t.Errorf("expected %d; got: %d", 1, termsAggRes.Buckets[1].DocCount)
230 }
231
232 // A terms aggregate with keys that are not strings
233 retweetsAggRes, found := agg.Terms("retweets")
234 if !found {
235 t.Errorf("expected %v; got: %v", true, found)
236 }
237 if retweetsAggRes == nil {
238 t.Fatalf("expected != nil; got: nil")
239 }
240 if len(retweetsAggRes.Buckets) != 3 {
241 t.Fatalf("expected %d; got: %d", 3, len(retweetsAggRes.Buckets))
242 }
243
244 if retweetsAggRes.Buckets[0].Key != float64(0) {
245 t.Errorf("expected %v; got: %v", float64(0), retweetsAggRes.Buckets[0].Key)
246 }
247 if got, err := retweetsAggRes.Buckets[0].KeyNumber.Int64(); err != nil {
248 t.Errorf("expected %d; got: %v", 0, retweetsAggRes.Buckets[0].Key)
249 } else if got != 0 {
250 t.Errorf("expected %d; got: %d", 0, got)
251 }
252 if retweetsAggRes.Buckets[0].KeyNumber != "0" {
253 t.Errorf("expected %q; got: %q", "0", retweetsAggRes.Buckets[0].KeyNumber)
254 }
255 if retweetsAggRes.Buckets[0].DocCount != 1 {
256 t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[0].DocCount)
257 }
258
259 if retweetsAggRes.Buckets[1].Key != float64(12) {
260 t.Errorf("expected %v; got: %v", float64(12), retweetsAggRes.Buckets[1].Key)
261 }
262 if got, err := retweetsAggRes.Buckets[1].KeyNumber.Int64(); err != nil {
263 t.Errorf("expected %d; got: %v", 0, retweetsAggRes.Buckets[1].KeyNumber)
264 } else if got != 12 {
265 t.Errorf("expected %d; got: %d", 12, got)
266 }
267 if retweetsAggRes.Buckets[1].KeyNumber != "12" {
268 t.Errorf("expected %q; got: %q", "12", retweetsAggRes.Buckets[1].KeyNumber)
269 }
270 if retweetsAggRes.Buckets[1].DocCount != 1 {
271 t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[1].DocCount)
272 }
273
274 if retweetsAggRes.Buckets[2].Key != float64(108) {
275 t.Errorf("expected %v; got: %v", float64(108), retweetsAggRes.Buckets[2].Key)
276 }
277 if got, err := retweetsAggRes.Buckets[2].KeyNumber.Int64(); err != nil {
278 t.Errorf("expected %d; got: %v", 108, retweetsAggRes.Buckets[2].KeyNumber)
279 } else if got != 108 {
280 t.Errorf("expected %d; got: %d", 108, got)
281 }
282 if retweetsAggRes.Buckets[2].KeyNumber != "108" {
283 t.Errorf("expected %q; got: %q", "108", retweetsAggRes.Buckets[2].KeyNumber)
284 }
285 if retweetsAggRes.Buckets[2].DocCount != 1 {
286 t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[2].DocCount)
287 }
288
289 // avgRetweets
290 avgAggRes, found := agg.Avg("avgRetweets")
291 if !found {
292 t.Errorf("expected %v; got: %v", true, found)
293 }
294 if avgAggRes == nil {
295 t.Fatalf("expected != nil; got: nil")
296 }
297 if avgAggRes.Value == nil {
298 t.Fatalf("expected != nil; got: %v", *avgAggRes.Value)
299 }
300 if *avgAggRes.Value != 40.0 {
301 t.Errorf("expected %v; got: %v", 40.0, *avgAggRes.Value)
302 }
303
304 // avgRetweetsWithMeta
305 avgMetaAggRes, found := agg.Avg("avgRetweetsWithMeta")
306 if !found {
307 t.Errorf("expected %v; got: %v", true, found)
308 }
309 if avgMetaAggRes == nil {
310 t.Fatalf("expected != nil; got: nil")
311 }
312 if avgMetaAggRes.Meta == nil {
313 t.Fatalf("expected != nil; got: %v", avgMetaAggRes.Meta)
314 }
315 metaDataValue, found := avgMetaAggRes.Meta["meta"]
316 if !found {
317 t.Fatalf("expected to return meta data key %q; got: %v", "meta", found)
318 }
319 if flag, ok := metaDataValue.(bool); !ok {
320 t.Fatalf("expected to return meta data key type %T; got: %T", true, metaDataValue)
321 } else if flag != true {
322 t.Fatalf("expected to return meta data key value %v; got: %v", true, flag)
323 }
324
325 // minRetweets
326 minAggRes, found := agg.Min("minRetweets")
327 if !found {
328 t.Errorf("expected %v; got: %v", true, found)
329 }
330 if minAggRes == nil {
331 t.Fatalf("expected != nil; got: nil")
332 }
333 if minAggRes.Value == nil {
334 t.Fatalf("expected != nil; got: %v", *minAggRes.Value)
335 }
336 if *minAggRes.Value != 0.0 {
337 t.Errorf("expected %v; got: %v", 0.0, *minAggRes.Value)
338 }
339
340 // maxRetweets
341 maxAggRes, found := agg.Max("maxRetweets")
342 if !found {
343 t.Errorf("expected %v; got: %v", true, found)
344 }
345 if maxAggRes == nil {
346 t.Fatalf("expected != nil; got: nil")
347 }
348 if maxAggRes.Value == nil {
349 t.Fatalf("expected != nil; got: %v", *maxAggRes.Value)
350 }
351 if *maxAggRes.Value != 108.0 {
352 t.Errorf("expected %v; got: %v", 108.0, *maxAggRes.Value)
353 }
354
355 // sumRetweets
356 sumAggRes, found := agg.Sum("sumRetweets")
357 if !found {
358 t.Errorf("expected %v; got: %v", true, found)
359 }
360 if sumAggRes == nil {
361 t.Fatalf("expected != nil; got: nil")
362 }
363 if sumAggRes.Value == nil {
364 t.Fatalf("expected != nil; got: %v", *sumAggRes.Value)
365 }
366 if *sumAggRes.Value != 120.0 {
367 t.Errorf("expected %v; got: %v", 120.0, *sumAggRes.Value)
368 }
369
370 // statsRetweets
371 statsAggRes, found := agg.Stats("statsRetweets")
372 if !found {
373 t.Errorf("expected %v; got: %v", true, found)
374 }
375 if statsAggRes == nil {
376 t.Fatalf("expected != nil; got: nil")
377 }
378 if statsAggRes.Count != 3 {
379 t.Errorf("expected %d; got: %d", 3, statsAggRes.Count)
380 }
381 if statsAggRes.Min == nil {
382 t.Fatalf("expected != nil; got: %v", *statsAggRes.Min)
383 }
384 if *statsAggRes.Min != 0.0 {
385 t.Errorf("expected %v; got: %v", 0.0, *statsAggRes.Min)
386 }
387 if statsAggRes.Max == nil {
388 t.Fatalf("expected != nil; got: %v", *statsAggRes.Max)
389 }
390 if *statsAggRes.Max != 108.0 {
391 t.Errorf("expected %v; got: %v", 108.0, *statsAggRes.Max)
392 }
393 if statsAggRes.Avg == nil {
394 t.Fatalf("expected != nil; got: %v", *statsAggRes.Avg)
395 }
396 if *statsAggRes.Avg != 40.0 {
397 t.Errorf("expected %v; got: %v", 40.0, *statsAggRes.Avg)
398 }
399 if statsAggRes.Sum == nil {
400 t.Fatalf("expected != nil; got: %v", *statsAggRes.Sum)
401 }
402 if *statsAggRes.Sum != 120.0 {
403 t.Errorf("expected %v; got: %v", 120.0, *statsAggRes.Sum)
404 }
405
406 // extstatsRetweets
407 extStatsAggRes, found := agg.ExtendedStats("extstatsRetweets")
408 if !found {
409 t.Errorf("expected %v; got: %v", true, found)
410 }
411 if extStatsAggRes == nil {
412 t.Fatalf("expected != nil; got: nil")
413 }
414 if extStatsAggRes.Count != 3 {
415 t.Errorf("expected %d; got: %d", 3, extStatsAggRes.Count)
416 }
417 if extStatsAggRes.Min == nil {
418 t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Min)
419 }
420 if *extStatsAggRes.Min != 0.0 {
421 t.Errorf("expected %v; got: %v", 0.0, *extStatsAggRes.Min)
422 }
423 if extStatsAggRes.Max == nil {
424 t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Max)
425 }
426 if *extStatsAggRes.Max != 108.0 {
427 t.Errorf("expected %v; got: %v", 108.0, *extStatsAggRes.Max)
428 }
429 if extStatsAggRes.Avg == nil {
430 t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Avg)
431 }
432 if *extStatsAggRes.Avg != 40.0 {
433 t.Errorf("expected %v; got: %v", 40.0, *extStatsAggRes.Avg)
434 }
435 if extStatsAggRes.Sum == nil {
436 t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Sum)
437 }
438 if *extStatsAggRes.Sum != 120.0 {
439 t.Errorf("expected %v; got: %v", 120.0, *extStatsAggRes.Sum)
440 }
441 if extStatsAggRes.SumOfSquares == nil {
442 t.Fatalf("expected != nil; got: %v", *extStatsAggRes.SumOfSquares)
443 }
444 if *extStatsAggRes.SumOfSquares != 11808.0 {
445 t.Errorf("expected %v; got: %v", 11808.0, *extStatsAggRes.SumOfSquares)
446 }
447 if extStatsAggRes.Variance == nil {
448 t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Variance)
449 }
450 if *extStatsAggRes.Variance != 2336.0 {
451 t.Errorf("expected %v; got: %v", 2336.0, *extStatsAggRes.Variance)
452 }
453 if extStatsAggRes.StdDeviation == nil {
454 t.Fatalf("expected != nil; got: %v", *extStatsAggRes.StdDeviation)
455 }
456 if *extStatsAggRes.StdDeviation != 48.33218389437829 {
457 t.Errorf("expected %v; got: %v", 48.33218389437829, *extStatsAggRes.StdDeviation)
458 }
459
460 // valueCountRetweets
461 valueCountAggRes, found := agg.ValueCount("valueCountRetweets")
462 if !found {
463 t.Errorf("expected %v; got: %v", true, found)
464 }
465 if valueCountAggRes == nil {
466 t.Fatalf("expected != nil; got: nil")
467 }
468 if valueCountAggRes.Value == nil {
469 t.Fatalf("expected != nil; got: %v", *valueCountAggRes.Value)
470 }
471 if *valueCountAggRes.Value != 3.0 {
472 t.Errorf("expected %v; got: %v", 3.0, *valueCountAggRes.Value)
473 }
474
475 // percentilesRetweets
476 percentilesAggRes, found := agg.Percentiles("percentilesRetweets")
477 if !found {
478 t.Errorf("expected %v; got: %v", true, found)
479 }
480 if percentilesAggRes == nil {
481 t.Fatalf("expected != nil; got: nil")
482 }
483 // ES 1.4.x returns 7: {"1.0":...}
484 // ES 1.5.0 returns 14: {"1.0":..., "1.0_as_string":...}
485 // So we're relaxing the test here.
486 if len(percentilesAggRes.Values) == 0 {
487 t.Errorf("expected at least %d value; got: %d\nValues are: %#v", 1, len(percentilesAggRes.Values), percentilesAggRes.Values)
488 }
489 if _, found := percentilesAggRes.Values["0.0"]; found {
490 t.Errorf("expected %v; got: %v", false, found)
491 }
492 if percentilesAggRes.Values["1.0"] != 0.24 {
493 t.Errorf("expected %v; got: %v", 0.24, percentilesAggRes.Values["1.0"])
494 }
495 if percentilesAggRes.Values["25.0"] != 6.0 {
496 t.Errorf("expected %v; got: %v", 6.0, percentilesAggRes.Values["25.0"])
497 }
498 if percentilesAggRes.Values["99.0"] != 106.08 {
499 t.Errorf("expected %v; got: %v", 106.08, percentilesAggRes.Values["99.0"])
500 }
501
502 // percentileRanksRetweets
503 percentileRanksAggRes, found := agg.PercentileRanks("percentileRanksRetweets")
504 if !found {
505 t.Errorf("expected %v; got: %v", true, found)
506 }
507 if percentileRanksAggRes == nil {
508 t.Fatalf("expected != nil; got: nil")
509 }
510 if len(percentileRanksAggRes.Values) == 0 {
511 t.Errorf("expected at least %d value; got %d\nValues are: %#v", 1, len(percentileRanksAggRes.Values), percentileRanksAggRes.Values)
512 }
513 if _, found := percentileRanksAggRes.Values["0.0"]; found {
514 t.Errorf("expected %v; got: %v", true, found)
515 }
516 if percentileRanksAggRes.Values["25.0"] != 21.180555555555557 {
517 t.Errorf("expected %v; got: %v", 21.180555555555557, percentileRanksAggRes.Values["25.0"])
518 }
519 if percentileRanksAggRes.Values["50.0"] != 29.86111111111111 {
520 t.Errorf("expected %v; got: %v", 29.86111111111111, percentileRanksAggRes.Values["50.0"])
521 }
522 if percentileRanksAggRes.Values["75.0"] != 38.54166666666667 {
523 t.Errorf("expected %v; got: %v", 38.54166666666667, percentileRanksAggRes.Values["75.0"])
524 }
525
526 // usersCardinality
527 cardAggRes, found := agg.Cardinality("usersCardinality")
528 if !found {
529 t.Errorf("expected %v; got: %v", true, found)
530 }
531 if cardAggRes == nil {
532 t.Fatalf("expected != nil; got: nil")
533 }
534 if cardAggRes.Value == nil {
535 t.Fatalf("expected != nil; got: %v", *cardAggRes.Value)
536 }
537 if *cardAggRes.Value != 2 {
538 t.Errorf("expected %v; got: %v", 2, *cardAggRes.Value)
539 }
540
541 // retweetsFilter
542 filterAggRes, found := agg.Filter("retweetsFilter")
543 if !found {
544 t.Errorf("expected %v; got: %v", true, found)
545 }
546 if filterAggRes == nil {
547 t.Fatalf("expected != nil; got: nil")
548 }
549 if filterAggRes.DocCount != 2 {
550 t.Fatalf("expected %v; got: %v", 2, filterAggRes.DocCount)
551 }
552
553 // Retrieve sub-aggregation
554 avgRetweetsAggRes, found := filterAggRes.Avg("avgRetweetsSub")
555 if !found {
556 t.Error("expected sub-aggregation \"avgRetweets\" to be found; got false")
557 }
558 if avgRetweetsAggRes == nil {
559 t.Fatal("expected sub-aggregation \"avgRetweets\"; got nil")
560 }
561 if avgRetweetsAggRes.Value == nil {
562 t.Fatalf("expected != nil; got: %v", avgRetweetsAggRes.Value)
563 }
564 if *avgRetweetsAggRes.Value != 54.0 {
565 t.Errorf("expected %v; got: %v", 54.0, *avgRetweetsAggRes.Value)
566 }
567
568 // queryFilter
569 queryFilterAggRes, found := agg.Filter("queryFilter")
570 if !found {
571 t.Errorf("expected %v; got: %v", true, found)
572 }
573 if queryFilterAggRes == nil {
574 t.Fatalf("expected != nil; got: nil")
575 }
576 if queryFilterAggRes.DocCount != 2 {
577 t.Fatalf("expected %v; got: %v", 2, queryFilterAggRes.DocCount)
578 }
579
580 // significantTerms
581 stAggRes, found := agg.SignificantTerms("significantTerms")
582 if !found {
583 t.Errorf("expected %v; got: %v", true, found)
584 }
585 if stAggRes == nil {
586 t.Fatalf("expected != nil; got: nil")
587 }
588 if stAggRes.DocCount != 3 {
589 t.Errorf("expected %v; got: %v", 3, stAggRes.DocCount)
590 }
591 if len(stAggRes.Buckets) != 0 {
592 t.Errorf("expected %v; got: %v", 0, len(stAggRes.Buckets))
593 }
594
595 // sampler
596 samplerAggRes, found := agg.Sampler("sample")
597 if !found {
598 t.Errorf("expected %v; got: %v", true, found)
599 }
600 if samplerAggRes == nil {
601 t.Fatalf("expected != nil; got: nil")
602 }
603 if samplerAggRes.DocCount != 3 {
604 t.Errorf("expected %v; got: %v", 3, samplerAggRes.DocCount)
605 }
606 sub, found := samplerAggRes.Aggregations["tagged_with"]
607 if !found {
608 t.Fatalf("expected sub aggregation %q", "tagged_with")
609 }
610 if sub == nil {
611 t.Fatalf("expected sub aggregation %q; got: %v", "tagged_with", sub)
612 }
613
614 // retweetsRange
615 rangeAggRes, found := agg.Range("retweetsRange")
616 if !found {
617 t.Errorf("expected %v; got: %v", true, found)
618 }
619 if rangeAggRes == nil {
620 t.Fatal("expected != nil; got: nil")
621 }
622 if len(rangeAggRes.Buckets) != 3 {
623 t.Fatalf("expected %d; got: %d", 3, len(rangeAggRes.Buckets))
624 }
625 if rangeAggRes.Buckets[0].DocCount != 1 {
626 t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[0].DocCount)
627 }
628 if rangeAggRes.Buckets[1].DocCount != 1 {
629 t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[1].DocCount)
630 }
631 if rangeAggRes.Buckets[2].DocCount != 1 {
632 t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[2].DocCount)
633 }
634
635 // retweetsKeyedRange
636 keyedRangeAggRes, found := agg.KeyedRange("retweetsKeyedRange")
637 if !found {
638 t.Errorf("expected %v; got: %v", true, found)
639 }
640 if keyedRangeAggRes == nil {
641 t.Fatal("expected != nil; got: nil")
642 }
643 if len(keyedRangeAggRes.Buckets) != 3 {
644 t.Fatalf("expected %d; got: %d", 3, len(keyedRangeAggRes.Buckets))
645 }
646 _, found = keyedRangeAggRes.Buckets["no-such-key"]
647 if found {
648 t.Fatalf("expected bucket to not be found; got: %v", found)
649 }
650 bucket, found := keyedRangeAggRes.Buckets["*-10.0"]
651 if !found {
652 t.Fatalf("expected bucket to be found; got: %v", found)
653 }
654 if bucket.DocCount != 1 {
655 t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
656 }
657 bucket, found = keyedRangeAggRes.Buckets["10.0-100.0"]
658 if !found {
659 t.Fatalf("expected bucket to be found; got: %v", found)
660 }
661 if bucket.DocCount != 1 {
662 t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
663 }
664 bucket, found = keyedRangeAggRes.Buckets["100.0-*"]
665 if !found {
666 t.Fatalf("expected bucket to be found; got: %v", found)
667 }
668 if bucket.DocCount != 1 {
669 t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
670 }
671
672 // dateRange
673 dateRangeRes, found := agg.DateRange("dateRange")
674 if !found {
675 t.Errorf("expected %v; got: %v", true, found)
676 }
677 if dateRangeRes == nil {
678 t.Fatal("expected != nil; got: nil")
679 }
680 if dateRangeRes.Buckets[0].DocCount != 1 {
681 t.Errorf("expected %d; got: %d", 1, dateRangeRes.Buckets[0].DocCount)
682 }
683 if dateRangeRes.Buckets[0].From != nil {
684 t.Fatal("expected From to be nil")
685 }
686 if dateRangeRes.Buckets[0].To == nil {
687 t.Fatal("expected To to be != nil")
688 }
689 if *dateRangeRes.Buckets[0].To != 1.325376e+12 {
690 t.Errorf("expected %v; got: %v", 1.325376e+12, *dateRangeRes.Buckets[0].To)
691 }
692 if dateRangeRes.Buckets[0].ToAsString != "2012-01-01T00:00:00.000Z" {
693 t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", dateRangeRes.Buckets[0].ToAsString)
694 }
695 if dateRangeRes.Buckets[1].DocCount != 2 {
696 t.Errorf("expected %d; got: %d", 2, dateRangeRes.Buckets[1].DocCount)
697 }
698 if dateRangeRes.Buckets[1].From == nil {
699 t.Fatal("expected From to be != nil")
700 }
701 if *dateRangeRes.Buckets[1].From != 1.325376e+12 {
702 t.Errorf("expected From = %v; got: %v", 1.325376e+12, *dateRangeRes.Buckets[1].From)
703 }
704 if dateRangeRes.Buckets[1].FromAsString != "2012-01-01T00:00:00.000Z" {
705 t.Errorf("expected FromAsString = %q; got: %q", "2012-01-01T00:00:00.000Z", dateRangeRes.Buckets[1].FromAsString)
706 }
707 if dateRangeRes.Buckets[1].To == nil {
708 t.Fatal("expected To to be != nil")
709 }
710 if *dateRangeRes.Buckets[1].To != 1.3569984e+12 {
711 t.Errorf("expected To = %v; got: %v", 1.3569984e+12, *dateRangeRes.Buckets[1].To)
712 }
713 if dateRangeRes.Buckets[1].ToAsString != "2013-01-01T00:00:00.000Z" {
714 t.Errorf("expected ToAsString = %q; got: %q", "2013-01-01T00:00:00.000Z", dateRangeRes.Buckets[1].ToAsString)
715 }
716 if dateRangeRes.Buckets[2].DocCount != 0 {
717 t.Errorf("expected %d; got: %d", 0, dateRangeRes.Buckets[2].DocCount)
718 }
719 if dateRangeRes.Buckets[2].To != nil {
720 t.Fatal("expected To to be nil")
721 }
722 if dateRangeRes.Buckets[2].From == nil {
723 t.Fatal("expected From to be != nil")
724 }
725 if *dateRangeRes.Buckets[2].From != 1.3569984e+12 {
726 t.Errorf("expected %v; got: %v", 1.3569984e+12, *dateRangeRes.Buckets[2].From)
727 }
728 if dateRangeRes.Buckets[2].FromAsString != "2013-01-01T00:00:00.000Z" {
729 t.Errorf("expected %q; got: %q", "2013-01-01T00:00:00.000Z", dateRangeRes.Buckets[2].FromAsString)
730 }
731
732 // missingTags
733 missingRes, found := agg.Missing("missingTags")
734 if !found {
735 t.Errorf("expected %v; got: %v", true, found)
736 }
737 if missingRes == nil {
738 t.Fatalf("expected != nil; got: nil")
739 }
740 if missingRes.DocCount != 0 {
741 t.Errorf("expected searchResult.Aggregations[\"missingTags\"].DocCount = %v; got %v", 0, missingRes.DocCount)
742 }
743
744 // retweetsHisto
745 histoRes, found := agg.Histogram("retweetsHisto")
746 if !found {
747 t.Errorf("expected %v; got: %v", true, found)
748 }
749 if histoRes == nil {
750 t.Fatalf("expected != nil; got: nil")
751 }
752 if len(histoRes.Buckets) != 2 {
753 t.Fatalf("expected %d; got: %d", 2, len(histoRes.Buckets))
754 }
755 if histoRes.Buckets[0].DocCount != 2 {
756 t.Errorf("expected %d; got: %d", 2, histoRes.Buckets[0].DocCount)
757 }
758 if histoRes.Buckets[0].Key != 0.0 {
759 t.Errorf("expected %v; got: %v", 0.0, histoRes.Buckets[0].Key)
760 }
761 if histoRes.Buckets[1].DocCount != 1 {
762 t.Errorf("expected %d; got: %d", 1, histoRes.Buckets[1].DocCount)
763 }
764 if histoRes.Buckets[1].Key != 100.0 {
765 t.Errorf("expected %v; got: %+v", 100.0, histoRes.Buckets[1].Key)
766 }
767
768 // dateHisto
769 dateHistoRes, found := agg.DateHistogram("dateHisto")
770 if !found {
771 t.Errorf("expected %v; got: %v", true, found)
772 }
773 if dateHistoRes == nil {
774 t.Fatalf("expected != nil; got: nil")
775 }
776 if len(dateHistoRes.Buckets) != 2 {
777 t.Fatalf("expected %d; got: %d", 2, len(dateHistoRes.Buckets))
778 }
779 if dateHistoRes.Buckets[0].DocCount != 1 {
780 t.Errorf("expected %d; got: %d", 1, dateHistoRes.Buckets[0].DocCount)
781 }
782 if dateHistoRes.Buckets[0].Key != 1.29384e+12 {
783 t.Errorf("expected %v; got: %v", 1.29384e+12, dateHistoRes.Buckets[0].Key)
784 }
785 if dateHistoRes.Buckets[0].KeyAsString == nil {
786 t.Fatalf("expected != nil; got: %v", dateHistoRes.Buckets[0].KeyAsString)
787 }
788 if *dateHistoRes.Buckets[0].KeyAsString != "2011-01-01T00:00:00.000Z" {
789 t.Errorf("expected %q; got: %q", "2011-01-01T00:00:00.000Z", *dateHistoRes.Buckets[0].KeyAsString)
790 }
791 if dateHistoRes.Buckets[1].DocCount != 2 {
792 t.Errorf("expected %d; got: %d", 2, dateHistoRes.Buckets[1].DocCount)
793 }
794 if dateHistoRes.Buckets[1].Key != 1.325376e+12 {
795 t.Errorf("expected %v; got: %v", 1.325376e+12, dateHistoRes.Buckets[1].Key)
796 }
797 if dateHistoRes.Buckets[1].KeyAsString == nil {
798 t.Fatalf("expected != nil; got: %v", dateHistoRes.Buckets[1].KeyAsString)
799 }
800 if *dateHistoRes.Buckets[1].KeyAsString != "2012-01-01T00:00:00.000Z" {
801 t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", *dateHistoRes.Buckets[1].KeyAsString)
802 }
803
804 // dateHistoKeyed
805 {
806 res, found := agg.KeyedDateHistogram("dateHistoKeyed")
807 if !found {
808 t.Errorf("expected %v; got: %v", true, found)
809 }
810 if res == nil {
811 t.Fatalf("expected != nil; got: nil")
812 }
813 if len(res.Buckets) != 2 {
814 t.Fatalf("expected %d; got: %d", 2, len(res.Buckets))
815 }
816
817 bucket, ok := res.Buckets["2011-01-01T00:00:00.000Z"]
818 if !ok || bucket == nil {
819 t.Fatalf("expected to have bucket with key %q", "2011-01-01T00:00:00.000Z")
820 }
821 if bucket.DocCount != 1 {
822 t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
823 }
824 if bucket.Key != 1.29384e+12 {
825 t.Errorf("expected %v; got: %v", 1.29384e+12, bucket.Key)
826 }
827 if bucket.KeyAsString == nil {
828 t.Fatalf("expected != nil; got: %v", bucket.KeyAsString)
829 }
830 if *bucket.KeyAsString != "2011-01-01T00:00:00.000Z" {
831 t.Errorf("expected %q; got: %q", "2011-01-01T00:00:00.000Z", *bucket.KeyAsString)
832 }
833
834 bucket, ok = res.Buckets["2012-01-01T00:00:00.000Z"]
835 if !ok || bucket == nil {
836 t.Fatalf("expected to have bucket with key %q", "2012-01-01T00:00:00.000Z")
837 }
838 if bucket.DocCount != 2 {
839 t.Errorf("expected %d; got: %d", 2, bucket.DocCount)
840 }
841 if bucket.Key != 1.325376e+12 {
842 t.Errorf("expected %v; got: %v", 1.325376e+12, bucket.Key)
843 }
844 if bucket.KeyAsString == nil {
845 t.Fatalf("expected != nil; got: %v", bucket.KeyAsString)
846 }
847 if *bucket.KeyAsString != "2012-01-01T00:00:00.000Z" {
848 t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", *bucket.KeyAsString)
849 }
850 }
851
852 // topHits
853 topTags, found := agg.Terms("top-tags")
854 if !found {
855 t.Errorf("expected %v; got: %v", true, found)
856 }
857 if topTags == nil {
858 t.Fatalf("expected != nil; got: nil")
859 }
860 if topTags.DocCountErrorUpperBound != 0 {
861 t.Errorf("expected %v; got: %v", 0, topTags.DocCountErrorUpperBound)
862 }
863 if topTags.SumOfOtherDocCount != 1 {
864 t.Errorf("expected %v; got: %v", 1, topTags.SumOfOtherDocCount)
865 }
866 if len(topTags.Buckets) != 3 {
867 t.Fatalf("expected %d; got: %d", 3, len(topTags.Buckets))
868 }
869 if topTags.Buckets[0].DocCount != 2 {
870 t.Errorf("expected %d; got: %d", 2, topTags.Buckets[0].DocCount)
871 }
872 if topTags.Buckets[0].Key != "golang" {
873 t.Errorf("expected %v; got: %v", "golang", topTags.Buckets[0].Key)
874 }
875 topHits, found := topTags.Buckets[0].TopHits("top_tag_hits")
876 if !found {
877 t.Errorf("expected %v; got: %v", true, found)
878 }
879 if topHits == nil {
880 t.Fatal("expected != nil; got: nil")
881 }
882 if topHits.Hits == nil {
883 t.Fatalf("expected != nil; got: nil")
884 }
885 if topHits.Hits.TotalHits != 2 {
886 t.Errorf("expected %d; got: %d", 2, topHits.Hits.TotalHits)
887 }
888 if topHits.Hits.Hits == nil {
889 t.Fatalf("expected != nil; got: nil")
890 }
891 if len(topHits.Hits.Hits) != 2 {
892 t.Fatalf("expected %d; got: %d", 2, len(topHits.Hits.Hits))
893 }
894 hit := topHits.Hits.Hits[0]
895 if !found {
896 t.Fatalf("expected %v; got: %v", true, found)
897 }
898 if hit == nil {
899 t.Fatal("expected != nil; got: nil")
900 }
901 var tw tweet
902 if err := json.Unmarshal(*hit.Source, &tw); err != nil {
903 t.Fatalf("expected no error; got: %v", err)
904 }
905 if tw.Message != "Welcome to Golang and Elasticsearch." {
906 t.Errorf("expected %q; got: %q", "Welcome to Golang and Elasticsearch.", tw.Message)
907 }
908 if topTags.Buckets[1].DocCount != 1 {
909 t.Errorf("expected %d; got: %d", 1, topTags.Buckets[1].DocCount)
910 }
911 if topTags.Buckets[1].Key != "cycling" {
912 t.Errorf("expected %v; got: %v", "cycling", topTags.Buckets[1].Key)
913 }
914 topHits, found = topTags.Buckets[1].TopHits("top_tag_hits")
915 if !found {
916 t.Errorf("expected %v; got: %v", true, found)
917 }
918 if topHits == nil {
919 t.Fatal("expected != nil; got: nil")
920 }
921 if topHits.Hits == nil {
922 t.Fatal("expected != nil; got nil")
923 }
924 if topHits.Hits.TotalHits != 1 {
925 t.Errorf("expected %d; got: %d", 1, topHits.Hits.TotalHits)
926 }
927 if topTags.Buckets[2].DocCount != 1 {
928 t.Errorf("expected %d; got: %d", 1, topTags.Buckets[2].DocCount)
929 }
930 if topTags.Buckets[2].Key != "elasticsearch" {
931 t.Errorf("expected %v; got: %v", "elasticsearch", topTags.Buckets[2].Key)
932 }
933 topHits, found = topTags.Buckets[2].TopHits("top_tag_hits")
934 if !found {
935 t.Errorf("expected %v; got: %v", true, found)
936 }
937 if topHits == nil {
938 t.Fatal("expected != nil; got: nil")
939 }
940 if topHits.Hits == nil {
941 t.Fatal("expected != nil; got: nil")
942 }
943 if topHits.Hits.TotalHits != 1 {
944 t.Errorf("expected %d; got: %d", 1, topHits.Hits.TotalHits)
945 }
946
947 // viewport via geo_bounds (1.3.0 has an error in that it doesn't output the aggregation name)
948 geoBoundsRes, found := agg.GeoBounds("viewport")
949 if !found {
950 t.Errorf("expected %v; got: %v", true, found)
951 }
952 if geoBoundsRes == nil {
953 t.Fatalf("expected != nil; got: nil")
954 }
955
956 // geohashed via geohash
957 geoHashRes, found := agg.GeoHash("geohashed")
958 if !found {
959 t.Errorf("expected %v; got: %v", true, found)
960 }
961 if geoHashRes == nil {
962 t.Fatalf("expected != nil; got: nil")
963 }
964
965 // geo_centroid
966 geoCentroidRes, found := agg.GeoCentroid("centroid")
967 if !found {
968 t.Errorf("expected %v; got: %v", true, found)
969 }
970 if geoCentroidRes == nil {
971 t.Fatalf("expected != nil; got: nil")
972 }
973
974 // Filters agg "countByUser" (unnamed)
975 countByUserAggRes, found := agg.Filters("countByUser")
976 if !found {
977 t.Errorf("expected %v; got: %v", true, found)
978 }
979 if countByUserAggRes == nil {
980 t.Fatalf("expected != nil; got: nil")
981 }
982 if len(countByUserAggRes.Buckets) != 2 {
983 t.Fatalf("expected %d; got: %d", 2, len(countByUserAggRes.Buckets))
984 }
985 if len(countByUserAggRes.NamedBuckets) != 0 {
986 t.Fatalf("expected %d; got: %d", 0, len(countByUserAggRes.NamedBuckets))
987 }
988 if countByUserAggRes.Buckets[0].DocCount != 2 {
989 t.Errorf("expected %d; got: %d", 2, countByUserAggRes.Buckets[0].DocCount)
990 }
991 if countByUserAggRes.Buckets[1].DocCount != 1 {
992 t.Errorf("expected %d; got: %d", 1, countByUserAggRes.Buckets[1].DocCount)
993 }
994
995 // Filters agg "countByUser2" (named)
996 countByUser2AggRes, found := agg.Filters("countByUser2")
997 if !found {
998 t.Errorf("expected %v; got: %v", true, found)
999 }
1000 if countByUser2AggRes == nil {
1001 t.Fatalf("expected != nil; got: nil")
1002 }
1003 if len(countByUser2AggRes.Buckets) != 0 {
1004 t.Fatalf("expected %d; got: %d", 0, len(countByUser2AggRes.Buckets))
1005 }
1006 if len(countByUser2AggRes.NamedBuckets) != 2 {
1007 t.Fatalf("expected %d; got: %d", 2, len(countByUser2AggRes.NamedBuckets))
1008 }
1009 b, found := countByUser2AggRes.NamedBuckets["olivere"]
1010 if !found {
1011 t.Fatalf("expected bucket %q; got: %v", "olivere", found)
1012 }
1013 if b == nil {
1014 t.Fatalf("expected bucket %q; got: %v", "olivere", b)
1015 }
1016 if b.DocCount != 2 {
1017 t.Errorf("expected %d; got: %d", 2, b.DocCount)
1018 }
1019 b, found = countByUser2AggRes.NamedBuckets["sandrae"]
1020 if !found {
1021 t.Fatalf("expected bucket %q; got: %v", "sandrae", found)
1022 }
1023 if b == nil {
1024 t.Fatalf("expected bucket %q; got: %v", "sandrae", b)
1025 }
1026 if b.DocCount != 1 {
1027 t.Errorf("expected %d; got: %d", 1, b.DocCount)
1028 }
1029 }
1030
1031 // TestAggsMarshal ensures that marshaling aggregations back into a string
1032 // does not yield base64 encoded data. See https://github.com/olivere/elastic/issues/51
1033 // and https://groups.google.com/forum/#!topic/Golang-Nuts/38ShOlhxAYY for details.
1034 func TestAggsMarshal(t *testing.T) {
1035 client := setupTestClientAndCreateIndex(t)
1036
1037 tweet1 := tweet{
1038 User: "olivere",
1039 Retweets: 108,
1040 Message: "Welcome to Golang and Elasticsearch.",
1041 Image: "http://golang.org/doc/gopher/gophercolor.png",
1042 Tags: []string{"golang", "elasticsearch"},
1043 Location: "48.1333,11.5667", // lat,lon
1044 Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
1045 }
1046
1047 // Add all documents
1048 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
1049 if err != nil {
1050 t.Fatal(err)
1051 }
1052 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
1053 if err != nil {
1054 t.Fatal(err)
1055 }
1056
1057 // Match all should return all documents
1058 all := NewMatchAllQuery()
1059 dhagg := NewDateHistogramAggregation().Field("created").Interval("year")
1060
1061 // Run query
1062 builder := client.Search().Index(testIndexName).Query(all)
1063 builder = builder.Aggregation("dhagg", dhagg)
1064 searchResult, err := builder.Do(context.TODO())
1065 if err != nil {
1066 t.Fatal(err)
1067 }
1068 if searchResult.TotalHits() != 1 {
1069 t.Errorf("expected Hits.TotalHits = %d; got: %d", 1, searchResult.TotalHits())
1070 }
1071 if _, found := searchResult.Aggregations["dhagg"]; !found {
1072 t.Fatalf("expected aggregation %q", "dhagg")
1073 }
1074 buf, err := json.Marshal(searchResult)
1075 if err != nil {
1076 t.Fatal(err)
1077 }
1078 s := string(buf)
1079 if i := strings.Index(s, `{"dhagg":{"buckets":[{"key_as_string":"2012-01-01`); i < 0 {
1080 t.Errorf("expected to serialize aggregation into string; got: %v", s)
1081 }
1082 }
1083
1084 func TestAggsMetricsMin(t *testing.T) {
1085 s := `{
1086 "min_price": {
1087 "value": 10
1088 }
1089 }`
1090
1091 aggs := new(Aggregations)
1092 err := json.Unmarshal([]byte(s), &aggs)
1093 if err != nil {
1094 t.Fatalf("expected no error decoding; got: %v", err)
1095 }
1096
1097 agg, found := aggs.Min("min_price")
1098 if !found {
1099 t.Fatalf("expected aggregation to be found; got: %v", found)
1100 }
1101 if agg == nil {
1102 t.Fatalf("expected aggregation != nil; got: %v", agg)
1103 }
1104 if agg.Value == nil {
1105 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
1106 }
1107 if *agg.Value != float64(10) {
1108 t.Fatalf("expected aggregation value = %v; got: %v", float64(10), *agg.Value)
1109 }
1110 }
1111
1112 func TestAggsMetricsMax(t *testing.T) {
1113 s := `{
1114 "max_price": {
1115 "value": 35
1116 }
1117 }`
1118
1119 aggs := new(Aggregations)
1120 err := json.Unmarshal([]byte(s), &aggs)
1121 if err != nil {
1122 t.Fatalf("expected no error decoding; got: %v", err)
1123 }
1124
1125 agg, found := aggs.Max("max_price")
1126 if !found {
1127 t.Fatalf("expected aggregation to be found; got: %v", found)
1128 }
1129 if agg == nil {
1130 t.Fatalf("expected aggregation != nil; got: %v", agg)
1131 }
1132 if agg.Value == nil {
1133 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
1134 }
1135 if *agg.Value != float64(35) {
1136 t.Fatalf("expected aggregation value = %v; got: %v", float64(35), *agg.Value)
1137 }
1138 }
1139
1140 func TestAggsMetricsSum(t *testing.T) {
1141 s := `{
1142 "intraday_return": {
1143 "value": 2.18
1144 }
1145 }`
1146
1147 aggs := new(Aggregations)
1148 err := json.Unmarshal([]byte(s), &aggs)
1149 if err != nil {
1150 t.Fatalf("expected no error decoding; got: %v", err)
1151 }
1152
1153 agg, found := aggs.Sum("intraday_return")
1154 if !found {
1155 t.Fatalf("expected aggregation to be found; got: %v", found)
1156 }
1157 if agg == nil {
1158 t.Fatalf("expected aggregation != nil; got: %v", agg)
1159 }
1160 if agg.Value == nil {
1161 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
1162 }
1163 if *agg.Value != float64(2.18) {
1164 t.Fatalf("expected aggregation value = %v; got: %v", float64(2.18), *agg.Value)
1165 }
1166 }
1167
1168 func TestAggsMetricsAvg(t *testing.T) {
1169 s := `{
1170 "avg_grade": {
1171 "value": 75
1172 }
1173 }`
1174
1175 aggs := new(Aggregations)
1176 err := json.Unmarshal([]byte(s), &aggs)
1177 if err != nil {
1178 t.Fatalf("expected no error decoding; got: %v", err)
1179 }
1180
1181 agg, found := aggs.Avg("avg_grade")
1182 if !found {
1183 t.Fatalf("expected aggregation to be found; got: %v", found)
1184 }
1185 if agg == nil {
1186 t.Fatalf("expected aggregation != nil; got: %v", agg)
1187 }
1188 if agg.Value == nil {
1189 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
1190 }
1191 if *agg.Value != float64(75) {
1192 t.Fatalf("expected aggregation value = %v; got: %v", float64(75), *agg.Value)
1193 }
1194 }
1195
1196 func TestAggsMetricsValueCount(t *testing.T) {
1197 s := `{
1198 "grades_count": {
1199 "value": 10
1200 }
1201 }`
1202
1203 aggs := new(Aggregations)
1204 err := json.Unmarshal([]byte(s), &aggs)
1205 if err != nil {
1206 t.Fatalf("expected no error decoding; got: %v", err)
1207 }
1208
1209 agg, found := aggs.ValueCount("grades_count")
1210 if !found {
1211 t.Fatalf("expected aggregation to be found; got: %v", found)
1212 }
1213 if agg == nil {
1214 t.Fatalf("expected aggregation != nil; got: %v", agg)
1215 }
1216 if agg.Value == nil {
1217 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
1218 }
1219 if *agg.Value != float64(10) {
1220 t.Fatalf("expected aggregation value = %v; got: %v", float64(10), *agg.Value)
1221 }
1222 }
1223
1224 func TestAggsMetricsCardinality(t *testing.T) {
1225 s := `{
1226 "author_count": {
1227 "value": 12
1228 }
1229 }`
1230
1231 aggs := new(Aggregations)
1232 err := json.Unmarshal([]byte(s), &aggs)
1233 if err != nil {
1234 t.Fatalf("expected no error decoding; got: %v", err)
1235 }
1236
1237 agg, found := aggs.Cardinality("author_count")
1238 if !found {
1239 t.Fatalf("expected aggregation to be found; got: %v", found)
1240 }
1241 if agg == nil {
1242 t.Fatalf("expected aggregation != nil; got: %v", agg)
1243 }
1244 if agg.Value == nil {
1245 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
1246 }
1247 if *agg.Value != float64(12) {
1248 t.Fatalf("expected aggregation value = %v; got: %v", float64(12), *agg.Value)
1249 }
1250 }
1251
1252 func TestAggsMetricsStats(t *testing.T) {
1253 s := `{
1254 "grades_stats": {
1255 "count": 6,
1256 "min": 60,
1257 "max": 98,
1258 "avg": 78.5,
1259 "sum": 471
1260 }
1261 }`
1262
1263 aggs := new(Aggregations)
1264 err := json.Unmarshal([]byte(s), &aggs)
1265 if err != nil {
1266 t.Fatalf("expected no error decoding; got: %v", err)
1267 }
1268
1269 agg, found := aggs.Stats("grades_stats")
1270 if !found {
1271 t.Fatalf("expected aggregation to be found; got: %v", found)
1272 }
1273 if agg == nil {
1274 t.Fatalf("expected aggregation != nil; got: %v", agg)
1275 }
1276 if agg.Count != int64(6) {
1277 t.Fatalf("expected aggregation Count = %v; got: %v", int64(6), agg.Count)
1278 }
1279 if agg.Min == nil {
1280 t.Fatalf("expected aggregation Min != nil; got: %v", agg.Min)
1281 }
1282 if *agg.Min != float64(60) {
1283 t.Fatalf("expected aggregation Min = %v; got: %v", float64(60), *agg.Min)
1284 }
1285 if agg.Max == nil {
1286 t.Fatalf("expected aggregation Max != nil; got: %v", agg.Max)
1287 }
1288 if *agg.Max != float64(98) {
1289 t.Fatalf("expected aggregation Max = %v; got: %v", float64(98), *agg.Max)
1290 }
1291 if agg.Avg == nil {
1292 t.Fatalf("expected aggregation Avg != nil; got: %v", agg.Avg)
1293 }
1294 if *agg.Avg != float64(78.5) {
1295 t.Fatalf("expected aggregation Avg = %v; got: %v", float64(78.5), *agg.Avg)
1296 }
1297 if agg.Sum == nil {
1298 t.Fatalf("expected aggregation Sum != nil; got: %v", agg.Sum)
1299 }
1300 if *agg.Sum != float64(471) {
1301 t.Fatalf("expected aggregation Sum = %v; got: %v", float64(471), *agg.Sum)
1302 }
1303 }
1304
1305 func TestAggsMetricsExtendedStats(t *testing.T) {
1306 s := `{
1307 "grades_stats": {
1308 "count": 6,
1309 "min": 72,
1310 "max": 117.6,
1311 "avg": 94.2,
1312 "sum": 565.2,
1313 "sum_of_squares": 54551.51999999999,
1314 "variance": 218.2799999999976,
1315 "std_deviation": 14.774302013969987
1316 }
1317 }`
1318
1319 aggs := new(Aggregations)
1320 err := json.Unmarshal([]byte(s), &aggs)
1321 if err != nil {
1322 t.Fatalf("expected no error decoding; got: %v", err)
1323 }
1324
1325 agg, found := aggs.ExtendedStats("grades_stats")
1326 if !found {
1327 t.Fatalf("expected aggregation to be found; got: %v", found)
1328 }
1329 if agg == nil {
1330 t.Fatalf("expected aggregation != nil; got: %v", agg)
1331 }
1332 if agg.Count != int64(6) {
1333 t.Fatalf("expected aggregation Count = %v; got: %v", int64(6), agg.Count)
1334 }
1335 if agg.Min == nil {
1336 t.Fatalf("expected aggregation Min != nil; got: %v", agg.Min)
1337 }
1338 if *agg.Min != float64(72) {
1339 t.Fatalf("expected aggregation Min = %v; got: %v", float64(72), *agg.Min)
1340 }
1341 if agg.Max == nil {
1342 t.Fatalf("expected aggregation Max != nil; got: %v", agg.Max)
1343 }
1344 if *agg.Max != float64(117.6) {
1345 t.Fatalf("expected aggregation Max = %v; got: %v", float64(117.6), *agg.Max)
1346 }
1347 if agg.Avg == nil {
1348 t.Fatalf("expected aggregation Avg != nil; got: %v", agg.Avg)
1349 }
1350 if *agg.Avg != float64(94.2) {
1351 t.Fatalf("expected aggregation Avg = %v; got: %v", float64(94.2), *agg.Avg)
1352 }
1353 if agg.Sum == nil {
1354 t.Fatalf("expected aggregation Sum != nil; got: %v", agg.Sum)
1355 }
1356 if *agg.Sum != float64(565.2) {
1357 t.Fatalf("expected aggregation Sum = %v; got: %v", float64(565.2), *agg.Sum)
1358 }
1359 if agg.SumOfSquares == nil {
1360 t.Fatalf("expected aggregation sum_of_squares != nil; got: %v", agg.SumOfSquares)
1361 }
1362 if *agg.SumOfSquares != float64(54551.51999999999) {
1363 t.Fatalf("expected aggregation sum_of_squares = %v; got: %v", float64(54551.51999999999), *agg.SumOfSquares)
1364 }
1365 if agg.Variance == nil {
1366 t.Fatalf("expected aggregation Variance != nil; got: %v", agg.Variance)
1367 }
1368 if *agg.Variance != float64(218.2799999999976) {
1369 t.Fatalf("expected aggregation Variance = %v; got: %v", float64(218.2799999999976), *agg.Variance)
1370 }
1371 if agg.StdDeviation == nil {
1372 t.Fatalf("expected aggregation StdDeviation != nil; got: %v", agg.StdDeviation)
1373 }
1374 if *agg.StdDeviation != float64(14.774302013969987) {
1375 t.Fatalf("expected aggregation StdDeviation = %v; got: %v", float64(14.774302013969987), *agg.StdDeviation)
1376 }
1377 }
1378
1379 func TestAggsMatrixStats(t *testing.T) {
1380 s := `{
1381 "matrixstats": {
1382 "fields": [{
1383 "name": "income",
1384 "count": 50,
1385 "mean": 51985.1,
1386 "variance": 7.383377037755103E7,
1387 "skewness": 0.5595114003506483,
1388 "kurtosis": 2.5692365287787124,
1389 "covariance": {
1390 "income": 7.383377037755103E7,
1391 "poverty": -21093.65836734694
1392 },
1393 "correlation": {
1394 "income": 1.0,
1395 "poverty": -0.8352655256272504
1396 }
1397 }, {
1398 "name": "poverty",
1399 "count": 51,
1400 "mean": 12.732000000000001,
1401 "variance": 8.637730612244896,
1402 "skewness": 0.4516049811903419,
1403 "kurtosis": 2.8615929677997767,
1404 "covariance": {
1405 "income": -21093.65836734694,
1406 "poverty": 8.637730612244896
1407 },
1408 "correlation": {
1409 "income": -0.8352655256272504,
1410 "poverty": 1.0
1411 }
1412 }]
1413 }
1414 }`
1415
1416 aggs := new(Aggregations)
1417 err := json.Unmarshal([]byte(s), &aggs)
1418 if err != nil {
1419 t.Fatalf("expected no error decoding; got: %v", err)
1420 }
1421
1422 agg, found := aggs.MatrixStats("matrixstats")
1423 if !found {
1424 t.Fatalf("expected aggregation to be found; got: %v", found)
1425 }
1426 if agg == nil {
1427 t.Fatalf("expected aggregation != nil; got: %v", agg)
1428 }
1429 if want, got := 2, len(agg.Fields); want != got {
1430 t.Fatalf("expected aggregaton len(Fields) = %v; got: %v", want, got)
1431 }
1432 field := agg.Fields[0]
1433 if want, got := "income", field.Name; want != got {
1434 t.Fatalf("expected aggregation field name == %q; got: %q", want, got)
1435 }
1436 if want, got := int64(50), field.Count; want != got {
1437 t.Fatalf("expected aggregation field count == %v; got: %v", want, got)
1438 }
1439 if want, got := 51985.1, field.Mean; want != got {
1440 t.Fatalf("expected aggregation field mean == %v; got: %v", want, got)
1441 }
1442 if want, got := 7.383377037755103e7, field.Variance; want != got {
1443 t.Fatalf("expected aggregation field variance == %v; got: %v", want, got)
1444 }
1445 if want, got := 0.5595114003506483, field.Skewness; want != got {
1446 t.Fatalf("expected aggregation field skewness == %v; got: %v", want, got)
1447 }
1448 if want, got := 2.5692365287787124, field.Kurtosis; want != got {
1449 t.Fatalf("expected aggregation field kurtosis == %v; got: %v", want, got)
1450 }
1451 if field.Covariance == nil {
1452 t.Fatalf("expected aggregation field covariance != nil; got: %v", nil)
1453 }
1454 if want, got := 7.383377037755103e7, field.Covariance["income"]; want != got {
1455 t.Fatalf("expected aggregation field covariance == %v; got: %v", want, got)
1456 }
1457 if want, got := -21093.65836734694, field.Covariance["poverty"]; want != got {
1458 t.Fatalf("expected aggregation field covariance == %v; got: %v", want, got)
1459 }
1460 if field.Correlation == nil {
1461 t.Fatalf("expected aggregation field correlation != nil; got: %v", nil)
1462 }
1463 if want, got := 1.0, field.Correlation["income"]; want != got {
1464 t.Fatalf("expected aggregation field correlation == %v; got: %v", want, got)
1465 }
1466 if want, got := -0.8352655256272504, field.Correlation["poverty"]; want != got {
1467 t.Fatalf("expected aggregation field correlation == %v; got: %v", want, got)
1468 }
1469 field = agg.Fields[1]
1470 if want, got := "poverty", field.Name; want != got {
1471 t.Fatalf("expected aggregation field name == %q; got: %q", want, got)
1472 }
1473 if want, got := int64(51), field.Count; want != got {
1474 t.Fatalf("expected aggregation field count == %v; got: %v", want, got)
1475 }
1476 }
1477
1478 func TestAggsMetricsPercentiles(t *testing.T) {
1479 s := `{
1480 "load_time_outlier": {
1481 "values" : {
1482 "1.0": 15,
1483 "5.0": 20,
1484 "25.0": 23,
1485 "50.0": 25,
1486 "75.0": 29,
1487 "95.0": 60,
1488 "99.0": 150
1489 }
1490 }
1491 }`
1492
1493 aggs := new(Aggregations)
1494 err := json.Unmarshal([]byte(s), &aggs)
1495 if err != nil {
1496 t.Fatalf("expected no error decoding; got: %v", err)
1497 }
1498
1499 agg, found := aggs.Percentiles("load_time_outlier")
1500 if !found {
1501 t.Fatalf("expected aggregation to be found; got: %v", found)
1502 }
1503 if agg == nil {
1504 t.Fatalf("expected aggregation != nil; got: %v", agg)
1505 }
1506 if agg.Values == nil {
1507 t.Fatalf("expected aggregation Values != nil; got: %v", agg.Values)
1508 }
1509 if len(agg.Values) != 7 {
1510 t.Fatalf("expected %d aggregation Values; got: %d", 7, len(agg.Values))
1511 }
1512 if agg.Values["1.0"] != float64(15) {
1513 t.Errorf("expected aggregation value for \"1.0\" = %v; got: %v", float64(15), agg.Values["1.0"])
1514 }
1515 if agg.Values["5.0"] != float64(20) {
1516 t.Errorf("expected aggregation value for \"5.0\" = %v; got: %v", float64(20), agg.Values["5.0"])
1517 }
1518 if agg.Values["25.0"] != float64(23) {
1519 t.Errorf("expected aggregation value for \"25.0\" = %v; got: %v", float64(23), agg.Values["25.0"])
1520 }
1521 if agg.Values["50.0"] != float64(25) {
1522 t.Errorf("expected aggregation value for \"50.0\" = %v; got: %v", float64(25), agg.Values["50.0"])
1523 }
1524 if agg.Values["75.0"] != float64(29) {
1525 t.Errorf("expected aggregation value for \"75.0\" = %v; got: %v", float64(29), agg.Values["75.0"])
1526 }
1527 if agg.Values["95.0"] != float64(60) {
1528 t.Errorf("expected aggregation value for \"95.0\" = %v; got: %v", float64(60), agg.Values["95.0"])
1529 }
1530 if agg.Values["99.0"] != float64(150) {
1531 t.Errorf("expected aggregation value for \"99.0\" = %v; got: %v", float64(150), agg.Values["99.0"])
1532 }
1533 }
1534
1535 func TestAggsMetricsPercentileRanks(t *testing.T) {
1536 s := `{
1537 "load_time_outlier": {
1538 "values" : {
1539 "15": 92,
1540 "30": 100
1541 }
1542 }
1543 }`
1544
1545 aggs := new(Aggregations)
1546 err := json.Unmarshal([]byte(s), &aggs)
1547 if err != nil {
1548 t.Fatalf("expected no error decoding; got: %v", err)
1549 }
1550
1551 agg, found := aggs.PercentileRanks("load_time_outlier")
1552 if !found {
1553 t.Fatalf("expected aggregation to be found; got: %v", found)
1554 }
1555 if agg == nil {
1556 t.Fatalf("expected aggregation != nil; got: %v", agg)
1557 }
1558 if agg.Values == nil {
1559 t.Fatalf("expected aggregation Values != nil; got: %v", agg.Values)
1560 }
1561 if len(agg.Values) != 2 {
1562 t.Fatalf("expected %d aggregation Values; got: %d", 7, len(agg.Values))
1563 }
1564 if agg.Values["15"] != float64(92) {
1565 t.Errorf("expected aggregation value for \"15\" = %v; got: %v", float64(92), agg.Values["15"])
1566 }
1567 if agg.Values["30"] != float64(100) {
1568 t.Errorf("expected aggregation value for \"30\" = %v; got: %v", float64(100), agg.Values["30"])
1569 }
1570 }
1571
1572 func TestAggsMetricsTopHits(t *testing.T) {
1573 s := `{
1574 "top-tags": {
1575 "buckets": [
1576 {
1577 "key": "windows-7",
1578 "doc_count": 25365,
1579 "top_tags_hits": {
1580 "hits": {
1581 "total": 25365,
1582 "max_score": 1,
1583 "hits": [
1584 {
1585 "_index": "stack",
1586 "_type": "question",
1587 "_id": "602679",
1588 "_score": 1,
1589 "_source": {
1590 "title": "Windows port opening"
1591 },
1592 "sort": [
1593 1370143231177
1594 ]
1595 }
1596 ]
1597 }
1598 }
1599 },
1600 {
1601 "key": "linux",
1602 "doc_count": 18342,
1603 "top_tags_hits": {
1604 "hits": {
1605 "total": 18342,
1606 "max_score": 1,
1607 "hits": [
1608 {
1609 "_index": "stack",
1610 "_type": "question",
1611 "_id": "602672",
1612 "_score": 1,
1613 "_source": {
1614 "title": "Ubuntu RFID Screensaver lock-unlock"
1615 },
1616 "sort": [
1617 1370143379747
1618 ]
1619 }
1620 ]
1621 }
1622 }
1623 },
1624 {
1625 "key": "windows",
1626 "doc_count": 18119,
1627 "top_tags_hits": {
1628 "hits": {
1629 "total": 18119,
1630 "max_score": 1,
1631 "hits": [
1632 {
1633 "_index": "stack",
1634 "_type": "question",
1635 "_id": "602678",
1636 "_score": 1,
1637 "_source": {
1638 "title": "If I change my computers date / time, what could be affected?"
1639 },
1640 "sort": [
1641 1370142868283
1642 ]
1643 }
1644 ]
1645 }
1646 }
1647 }
1648 ]
1649 }
1650 }`
1651
1652 aggs := new(Aggregations)
1653 err := json.Unmarshal([]byte(s), &aggs)
1654 if err != nil {
1655 t.Fatalf("expected no error decoding; got: %v", err)
1656 }
1657
1658 agg, found := aggs.Terms("top-tags")
1659 if !found {
1660 t.Fatalf("expected aggregation to be found; got: %v", found)
1661 }
1662 if agg == nil {
1663 t.Fatalf("expected aggregation != nil; got: %v", agg)
1664 }
1665 if agg.Buckets == nil {
1666 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
1667 }
1668 if len(agg.Buckets) != 3 {
1669 t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
1670 }
1671 if agg.Buckets[0].Key != "windows-7" {
1672 t.Errorf("expected bucket key = %q; got: %q", "windows-7", agg.Buckets[0].Key)
1673 }
1674 if agg.Buckets[1].Key != "linux" {
1675 t.Errorf("expected bucket key = %q; got: %q", "linux", agg.Buckets[1].Key)
1676 }
1677 if agg.Buckets[2].Key != "windows" {
1678 t.Errorf("expected bucket key = %q; got: %q", "windows", agg.Buckets[2].Key)
1679 }
1680
1681 // Sub-aggregation of top-hits
1682 subAgg, found := agg.Buckets[0].TopHits("top_tags_hits")
1683 if !found {
1684 t.Fatalf("expected sub aggregation to be found; got: %v", found)
1685 }
1686 if subAgg == nil {
1687 t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
1688 }
1689 if subAgg.Hits == nil {
1690 t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
1691 }
1692 if subAgg.Hits.TotalHits != 25365 {
1693 t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 25365, subAgg.Hits.TotalHits)
1694 }
1695 if subAgg.Hits.MaxScore == nil {
1696 t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
1697 }
1698 if *subAgg.Hits.MaxScore != float64(1.0) {
1699 t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
1700 }
1701
1702 subAgg, found = agg.Buckets[1].TopHits("top_tags_hits")
1703 if !found {
1704 t.Fatalf("expected sub aggregation to be found; got: %v", found)
1705 }
1706 if subAgg == nil {
1707 t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
1708 }
1709 if subAgg.Hits == nil {
1710 t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
1711 }
1712 if subAgg.Hits.TotalHits != 18342 {
1713 t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 18342, subAgg.Hits.TotalHits)
1714 }
1715 if subAgg.Hits.MaxScore == nil {
1716 t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
1717 }
1718 if *subAgg.Hits.MaxScore != float64(1.0) {
1719 t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
1720 }
1721
1722 subAgg, found = agg.Buckets[2].TopHits("top_tags_hits")
1723 if !found {
1724 t.Fatalf("expected sub aggregation to be found; got: %v", found)
1725 }
1726 if subAgg == nil {
1727 t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
1728 }
1729 if subAgg.Hits == nil {
1730 t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
1731 }
1732 if subAgg.Hits.TotalHits != 18119 {
1733 t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 18119, subAgg.Hits.TotalHits)
1734 }
1735 if subAgg.Hits.MaxScore == nil {
1736 t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
1737 }
1738 if *subAgg.Hits.MaxScore != float64(1.0) {
1739 t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
1740 }
1741 }
1742
1743 func TestAggsBucketGlobal(t *testing.T) {
1744 s := `{
1745 "all_products" : {
1746 "doc_count" : 100,
1747 "avg_price" : {
1748 "value" : 56.3
1749 }
1750 }
1751 }`
1752
1753 aggs := new(Aggregations)
1754 err := json.Unmarshal([]byte(s), &aggs)
1755 if err != nil {
1756 t.Fatalf("expected no error decoding; got: %v", err)
1757 }
1758
1759 agg, found := aggs.Global("all_products")
1760 if !found {
1761 t.Fatalf("expected aggregation to be found; got: %v", found)
1762 }
1763 if agg == nil {
1764 t.Fatalf("expected aggregation != nil; got: %v", agg)
1765 }
1766 if agg.DocCount != 100 {
1767 t.Fatalf("expected aggregation DocCount = %d; got: %d", 100, agg.DocCount)
1768 }
1769
1770 // Sub-aggregation
1771 subAgg, found := agg.Avg("avg_price")
1772 if !found {
1773 t.Fatalf("expected sub-aggregation to be found; got: %v", found)
1774 }
1775 if subAgg == nil {
1776 t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
1777 }
1778 if subAgg.Value == nil {
1779 t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
1780 }
1781 if *subAgg.Value != float64(56.3) {
1782 t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(56.3), *subAgg.Value)
1783 }
1784 }
1785
1786 func TestAggsBucketFilter(t *testing.T) {
1787 s := `{
1788 "in_stock_products" : {
1789 "doc_count" : 100,
1790 "avg_price" : { "value" : 56.3 }
1791 }
1792 }`
1793
1794 aggs := new(Aggregations)
1795 err := json.Unmarshal([]byte(s), &aggs)
1796 if err != nil {
1797 t.Fatalf("expected no error decoding; got: %v", err)
1798 }
1799
1800 agg, found := aggs.Filter("in_stock_products")
1801 if !found {
1802 t.Fatalf("expected aggregation to be found; got: %v", found)
1803 }
1804 if agg == nil {
1805 t.Fatalf("expected aggregation != nil; got: %v", agg)
1806 }
1807 if agg.DocCount != 100 {
1808 t.Fatalf("expected aggregation DocCount = %d; got: %d", 100, agg.DocCount)
1809 }
1810
1811 // Sub-aggregation
1812 subAgg, found := agg.Avg("avg_price")
1813 if !found {
1814 t.Fatalf("expected sub-aggregation to be found; got: %v", found)
1815 }
1816 if subAgg == nil {
1817 t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
1818 }
1819 if subAgg.Value == nil {
1820 t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
1821 }
1822 if *subAgg.Value != float64(56.3) {
1823 t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(56.3), *subAgg.Value)
1824 }
1825 }
1826
1827 func TestAggsBucketFiltersWithBuckets(t *testing.T) {
1828 s := `{
1829 "messages" : {
1830 "buckets" : [
1831 {
1832 "doc_count" : 34,
1833 "monthly" : {
1834 "buckets" : []
1835 }
1836 },
1837 {
1838 "doc_count" : 439,
1839 "monthly" : {
1840 "buckets" : []
1841 }
1842 }
1843 ]
1844 }
1845 }`
1846
1847 aggs := new(Aggregations)
1848 err := json.Unmarshal([]byte(s), &aggs)
1849 if err != nil {
1850 t.Fatalf("expected no error decoding; got: %v", err)
1851 }
1852
1853 agg, found := aggs.Filters("messages")
1854 if !found {
1855 t.Fatalf("expected aggregation to be found; got: %v", found)
1856 }
1857 if agg == nil {
1858 t.Fatalf("expected aggregation != nil; got: %v", agg)
1859 }
1860 if agg.Buckets == nil {
1861 t.Fatalf("expected aggregation buckets != %v; got: %v", nil, agg.Buckets)
1862 }
1863 if len(agg.Buckets) != 2 {
1864 t.Fatalf("expected %d buckets; got: %d", 2, len(agg.Buckets))
1865 }
1866
1867 if agg.Buckets[0].DocCount != 34 {
1868 t.Fatalf("expected DocCount = %d; got: %d", 34, agg.Buckets[0].DocCount)
1869 }
1870 subAgg, found := agg.Buckets[0].Histogram("monthly")
1871 if !found {
1872 t.Fatalf("expected sub aggregation to be found; got: %v", found)
1873 }
1874 if subAgg == nil {
1875 t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
1876 }
1877
1878 if agg.Buckets[1].DocCount != 439 {
1879 t.Fatalf("expected DocCount = %d; got: %d", 439, agg.Buckets[1].DocCount)
1880 }
1881 subAgg, found = agg.Buckets[1].Histogram("monthly")
1882 if !found {
1883 t.Fatalf("expected sub aggregation to be found; got: %v", found)
1884 }
1885 if subAgg == nil {
1886 t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
1887 }
1888 }
1889
1890 func TestAggsBucketFiltersWithNamedBuckets(t *testing.T) {
1891 s := `{
1892 "messages" : {
1893 "buckets" : {
1894 "errors" : {
1895 "doc_count" : 34,
1896 "monthly" : {
1897 "buckets" : []
1898 }
1899 },
1900 "warnings" : {
1901 "doc_count" : 439,
1902 "monthly" : {
1903 "buckets" : []
1904 }
1905 }
1906 }
1907 }
1908 }`
1909
1910 aggs := new(Aggregations)
1911 err := json.Unmarshal([]byte(s), &aggs)
1912 if err != nil {
1913 t.Fatalf("expected no error decoding; got: %v", err)
1914 }
1915
1916 agg, found := aggs.Filters("messages")
1917 if !found {
1918 t.Fatalf("expected aggregation to be found; got: %v", found)
1919 }
1920 if agg == nil {
1921 t.Fatalf("expected aggregation != nil; got: %v", agg)
1922 }
1923 if agg.NamedBuckets == nil {
1924 t.Fatalf("expected aggregation buckets != %v; got: %v", nil, agg.NamedBuckets)
1925 }
1926 if len(agg.NamedBuckets) != 2 {
1927 t.Fatalf("expected %d buckets; got: %d", 2, len(agg.NamedBuckets))
1928 }
1929
1930 if agg.NamedBuckets["errors"].DocCount != 34 {
1931 t.Fatalf("expected DocCount = %d; got: %d", 34, agg.NamedBuckets["errors"].DocCount)
1932 }
1933 subAgg, found := agg.NamedBuckets["errors"].Histogram("monthly")
1934 if !found {
1935 t.Fatalf("expected sub aggregation to be found; got: %v", found)
1936 }
1937 if subAgg == nil {
1938 t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
1939 }
1940
1941 if agg.NamedBuckets["warnings"].DocCount != 439 {
1942 t.Fatalf("expected DocCount = %d; got: %d", 439, agg.NamedBuckets["warnings"].DocCount)
1943 }
1944 subAgg, found = agg.NamedBuckets["warnings"].Histogram("monthly")
1945 if !found {
1946 t.Fatalf("expected sub aggregation to be found; got: %v", found)
1947 }
1948 if subAgg == nil {
1949 t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
1950 }
1951 }
1952
1953 func TestAggsBucketMissing(t *testing.T) {
1954 s := `{
1955 "products_without_a_price" : {
1956 "doc_count" : 10
1957 }
1958 }`
1959
1960 aggs := new(Aggregations)
1961 err := json.Unmarshal([]byte(s), &aggs)
1962 if err != nil {
1963 t.Fatalf("expected no error decoding; got: %v", err)
1964 }
1965
1966 agg, found := aggs.Missing("products_without_a_price")
1967 if !found {
1968 t.Fatalf("expected aggregation to be found; got: %v", found)
1969 }
1970 if agg == nil {
1971 t.Fatalf("expected aggregation != nil; got: %v", agg)
1972 }
1973 if agg.DocCount != 10 {
1974 t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
1975 }
1976 }
1977
1978 func TestAggsBucketNested(t *testing.T) {
1979 s := `{
1980 "resellers": {
1981 "min_price": {
1982 "value" : 350
1983 }
1984 }
1985 }`
1986
1987 aggs := new(Aggregations)
1988 err := json.Unmarshal([]byte(s), &aggs)
1989 if err != nil {
1990 t.Fatalf("expected no error decoding; got: %v", err)
1991 }
1992
1993 agg, found := aggs.Nested("resellers")
1994 if !found {
1995 t.Fatalf("expected aggregation to be found; got: %v", found)
1996 }
1997 if agg == nil {
1998 t.Fatalf("expected aggregation != nil; got: %v", agg)
1999 }
2000 if agg.DocCount != 0 {
2001 t.Fatalf("expected aggregation DocCount = %d; got: %d", 0, agg.DocCount)
2002 }
2003
2004 // Sub-aggregation
2005 subAgg, found := agg.Avg("min_price")
2006 if !found {
2007 t.Fatalf("expected sub-aggregation to be found; got: %v", found)
2008 }
2009 if subAgg == nil {
2010 t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
2011 }
2012 if subAgg.Value == nil {
2013 t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
2014 }
2015 if *subAgg.Value != float64(350) {
2016 t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(350), *subAgg.Value)
2017 }
2018 }
2019
2020 func TestAggsBucketReverseNested(t *testing.T) {
2021 s := `{
2022 "comment_to_issue": {
2023 "doc_count" : 10
2024 }
2025 }`
2026
2027 aggs := new(Aggregations)
2028 err := json.Unmarshal([]byte(s), &aggs)
2029 if err != nil {
2030 t.Fatalf("expected no error decoding; got: %v", err)
2031 }
2032
2033 agg, found := aggs.ReverseNested("comment_to_issue")
2034 if !found {
2035 t.Fatalf("expected aggregation to be found; got: %v", found)
2036 }
2037 if agg == nil {
2038 t.Fatalf("expected aggregation != nil; got: %v", agg)
2039 }
2040 if agg.DocCount != 10 {
2041 t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
2042 }
2043 }
2044
2045 func TestAggsBucketChildren(t *testing.T) {
2046 s := `{
2047 "to-answers": {
2048 "doc_count" : 10
2049 }
2050 }`
2051
2052 aggs := new(Aggregations)
2053 err := json.Unmarshal([]byte(s), &aggs)
2054 if err != nil {
2055 t.Fatalf("expected no error decoding; got: %v", err)
2056 }
2057
2058 agg, found := aggs.Children("to-answers")
2059 if !found {
2060 t.Fatalf("expected aggregation to be found; got: %v", found)
2061 }
2062 if agg == nil {
2063 t.Fatalf("expected aggregation != nil; got: %v", agg)
2064 }
2065 if agg.DocCount != 10 {
2066 t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
2067 }
2068 }
2069
2070 func TestAggsBucketTerms(t *testing.T) {
2071 s := `{
2072 "users" : {
2073 "doc_count_error_upper_bound" : 1,
2074 "sum_other_doc_count" : 2,
2075 "buckets" : [ {
2076 "key" : "olivere",
2077 "doc_count" : 2
2078 }, {
2079 "key" : "sandrae",
2080 "doc_count" : 1
2081 } ]
2082 }
2083 }`
2084
2085 aggs := new(Aggregations)
2086 err := json.Unmarshal([]byte(s), &aggs)
2087 if err != nil {
2088 t.Fatalf("expected no error decoding; got: %v", err)
2089 }
2090
2091 agg, found := aggs.Terms("users")
2092 if !found {
2093 t.Fatalf("expected aggregation to be found; got: %v", found)
2094 }
2095 if agg == nil {
2096 t.Fatalf("expected aggregation != nil; got: %v", agg)
2097 }
2098 if agg.Buckets == nil {
2099 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2100 }
2101 if len(agg.Buckets) != 2 {
2102 t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
2103 }
2104 if agg.Buckets[0].Key != "olivere" {
2105 t.Errorf("expected key %q; got: %q", "olivere", agg.Buckets[0].Key)
2106 }
2107 if agg.Buckets[0].DocCount != 2 {
2108 t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
2109 }
2110 if agg.Buckets[1].Key != "sandrae" {
2111 t.Errorf("expected key %q; got: %q", "sandrae", agg.Buckets[1].Key)
2112 }
2113 if agg.Buckets[1].DocCount != 1 {
2114 t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
2115 }
2116 }
2117
2118 func TestAggsBucketTermsWithNumericKeys(t *testing.T) {
2119 s := `{
2120 "users" : {
2121 "doc_count_error_upper_bound" : 1,
2122 "sum_other_doc_count" : 2,
2123 "buckets" : [ {
2124 "key" : 17,
2125 "doc_count" : 2
2126 }, {
2127 "key" : 21,
2128 "doc_count" : 1
2129 } ]
2130 }
2131 }`
2132
2133 aggs := new(Aggregations)
2134 err := json.Unmarshal([]byte(s), &aggs)
2135 if err != nil {
2136 t.Fatalf("expected no error decoding; got: %v", err)
2137 }
2138
2139 agg, found := aggs.Terms("users")
2140 if !found {
2141 t.Fatalf("expected aggregation to be found; got: %v", found)
2142 }
2143 if agg == nil {
2144 t.Fatalf("expected aggregation != nil; got: %v", agg)
2145 }
2146 if agg.Buckets == nil {
2147 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2148 }
2149 if len(agg.Buckets) != 2 {
2150 t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
2151 }
2152 if agg.Buckets[0].Key != float64(17) {
2153 t.Errorf("expected key %v; got: %v", 17, agg.Buckets[0].Key)
2154 }
2155 if got, err := agg.Buckets[0].KeyNumber.Int64(); err != nil {
2156 t.Errorf("expected to convert key to int64; got: %v", err)
2157 } else if got != 17 {
2158 t.Errorf("expected key %v; got: %v", 17, agg.Buckets[0].Key)
2159 }
2160 if agg.Buckets[0].DocCount != 2 {
2161 t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
2162 }
2163 if agg.Buckets[1].Key != float64(21) {
2164 t.Errorf("expected key %v; got: %v", 21, agg.Buckets[1].Key)
2165 }
2166 if got, err := agg.Buckets[1].KeyNumber.Int64(); err != nil {
2167 t.Errorf("expected to convert key to int64; got: %v", err)
2168 } else if got != 21 {
2169 t.Errorf("expected key %v; got: %v", 21, agg.Buckets[1].Key)
2170 }
2171 if agg.Buckets[1].DocCount != 1 {
2172 t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
2173 }
2174 }
2175
2176 func TestAggsBucketTermsWithBoolKeys(t *testing.T) {
2177 s := `{
2178 "users" : {
2179 "doc_count_error_upper_bound" : 1,
2180 "sum_other_doc_count" : 2,
2181 "buckets" : [ {
2182 "key" : true,
2183 "doc_count" : 2
2184 }, {
2185 "key" : false,
2186 "doc_count" : 1
2187 } ]
2188 }
2189 }`
2190
2191 aggs := new(Aggregations)
2192 err := json.Unmarshal([]byte(s), &aggs)
2193 if err != nil {
2194 t.Fatalf("expected no error decoding; got: %v", err)
2195 }
2196
2197 agg, found := aggs.Terms("users")
2198 if !found {
2199 t.Fatalf("expected aggregation to be found; got: %v", found)
2200 }
2201 if agg == nil {
2202 t.Fatalf("expected aggregation != nil; got: %v", agg)
2203 }
2204 if agg.Buckets == nil {
2205 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2206 }
2207 if len(agg.Buckets) != 2 {
2208 t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
2209 }
2210 if agg.Buckets[0].Key != true {
2211 t.Errorf("expected key %v; got: %v", true, agg.Buckets[0].Key)
2212 }
2213 if agg.Buckets[0].DocCount != 2 {
2214 t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
2215 }
2216 if agg.Buckets[1].Key != false {
2217 t.Errorf("expected key %v; got: %v", false, agg.Buckets[1].Key)
2218 }
2219 if agg.Buckets[1].DocCount != 1 {
2220 t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
2221 }
2222 }
2223
2224 func TestAggsBucketSignificantTerms(t *testing.T) {
2225 s := `{
2226 "significantCrimeTypes" : {
2227 "doc_count": 47347,
2228 "buckets" : [
2229 {
2230 "key": "Bicycle theft",
2231 "doc_count": 3640,
2232 "score": 0.371235374214817,
2233 "bg_count": 66799
2234 }
2235 ]
2236 }
2237 }`
2238
2239 aggs := new(Aggregations)
2240 err := json.Unmarshal([]byte(s), &aggs)
2241 if err != nil {
2242 t.Fatalf("expected no error decoding; got: %v", err)
2243 }
2244
2245 agg, found := aggs.SignificantTerms("significantCrimeTypes")
2246 if !found {
2247 t.Fatalf("expected aggregation to be found; got: %v", found)
2248 }
2249 if agg == nil {
2250 t.Fatalf("expected aggregation != nil; got: %v", agg)
2251 }
2252 if agg.DocCount != 47347 {
2253 t.Fatalf("expected aggregation DocCount != %d; got: %d", 47347, agg.DocCount)
2254 }
2255 if agg.Buckets == nil {
2256 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2257 }
2258 if len(agg.Buckets) != 1 {
2259 t.Errorf("expected %d bucket entries; got: %d", 1, len(agg.Buckets))
2260 }
2261 if agg.Buckets[0].Key != "Bicycle theft" {
2262 t.Errorf("expected key = %q; got: %q", "Bicycle theft", agg.Buckets[0].Key)
2263 }
2264 if agg.Buckets[0].DocCount != 3640 {
2265 t.Errorf("expected doc count = %d; got: %d", 3640, agg.Buckets[0].DocCount)
2266 }
2267 if agg.Buckets[0].Score != float64(0.371235374214817) {
2268 t.Errorf("expected score = %v; got: %v", float64(0.371235374214817), agg.Buckets[0].Score)
2269 }
2270 if agg.Buckets[0].BgCount != 66799 {
2271 t.Errorf("expected BgCount = %d; got: %d", 66799, agg.Buckets[0].BgCount)
2272 }
2273 }
2274
2275 func TestAggsBucketSampler(t *testing.T) {
2276 s := `{
2277 "sample" : {
2278 "doc_count": 1000,
2279 "keywords": {
2280 "doc_count": 1000,
2281 "buckets" : [
2282 {
2283 "key": "bend",
2284 "doc_count": 58,
2285 "score": 37.982536582524276,
2286 "bg_count": 103
2287 }
2288 ]
2289 }
2290 }
2291 }`
2292
2293 aggs := new(Aggregations)
2294 err := json.Unmarshal([]byte(s), &aggs)
2295 if err != nil {
2296 t.Fatalf("expected no error decoding; got: %v", err)
2297 }
2298
2299 agg, found := aggs.Sampler("sample")
2300 if !found {
2301 t.Fatalf("expected aggregation to be found; got: %v", found)
2302 }
2303 if agg == nil {
2304 t.Fatalf("expected aggregation != nil; got: %v", agg)
2305 }
2306 if agg.DocCount != 1000 {
2307 t.Fatalf("expected aggregation DocCount != %d; got: %d", 1000, agg.DocCount)
2308 }
2309 sub, found := agg.Aggregations["keywords"]
2310 if !found {
2311 t.Fatalf("expected sub aggregation %q", "keywords")
2312 }
2313 if sub == nil {
2314 t.Fatalf("expected sub aggregation %q; got: %v", "keywords", sub)
2315 }
2316 }
2317
2318 func TestAggsBucketRange(t *testing.T) {
2319 s := `{
2320 "price_ranges" : {
2321 "buckets": [
2322 {
2323 "to": 50,
2324 "doc_count": 2
2325 },
2326 {
2327 "from": 50,
2328 "to": 100,
2329 "doc_count": 4
2330 },
2331 {
2332 "from": 100,
2333 "doc_count": 4
2334 }
2335 ]
2336 }
2337 }`
2338
2339 aggs := new(Aggregations)
2340 err := json.Unmarshal([]byte(s), &aggs)
2341 if err != nil {
2342 t.Fatalf("expected no error decoding; got: %v", err)
2343 }
2344
2345 agg, found := aggs.Range("price_ranges")
2346 if !found {
2347 t.Fatalf("expected aggregation to be found; got: %v", found)
2348 }
2349 if agg == nil {
2350 t.Fatalf("expected aggregation != nil; got: %v", agg)
2351 }
2352 if agg.Buckets == nil {
2353 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2354 }
2355 if len(agg.Buckets) != 3 {
2356 t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
2357 }
2358 if agg.Buckets[0].From != nil {
2359 t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
2360 }
2361 if agg.Buckets[0].To == nil {
2362 t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
2363 }
2364 if *agg.Buckets[0].To != float64(50) {
2365 t.Errorf("expected To = %v; got: %v", float64(50), *agg.Buckets[0].To)
2366 }
2367 if agg.Buckets[0].DocCount != 2 {
2368 t.Errorf("expected DocCount = %d; got: %d", 2, agg.Buckets[0].DocCount)
2369 }
2370 if agg.Buckets[1].From == nil {
2371 t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
2372 }
2373 if *agg.Buckets[1].From != float64(50) {
2374 t.Errorf("expected From = %v; got: %v", float64(50), *agg.Buckets[1].From)
2375 }
2376 if agg.Buckets[1].To == nil {
2377 t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[1].To)
2378 }
2379 if *agg.Buckets[1].To != float64(100) {
2380 t.Errorf("expected To = %v; got: %v", float64(100), *agg.Buckets[1].To)
2381 }
2382 if agg.Buckets[1].DocCount != 4 {
2383 t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[1].DocCount)
2384 }
2385 if agg.Buckets[2].From == nil {
2386 t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[2].From)
2387 }
2388 if *agg.Buckets[2].From != float64(100) {
2389 t.Errorf("expected From = %v; got: %v", float64(100), *agg.Buckets[2].From)
2390 }
2391 if agg.Buckets[2].To != nil {
2392 t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[2].To)
2393 }
2394 if agg.Buckets[2].DocCount != 4 {
2395 t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[2].DocCount)
2396 }
2397 }
2398
2399 func TestAggsBucketDateRange(t *testing.T) {
2400 s := `{
2401 "range": {
2402 "buckets": [
2403 {
2404 "to": 1.3437792E+12,
2405 "to_as_string": "08-2012",
2406 "doc_count": 7
2407 },
2408 {
2409 "from": 1.3437792E+12,
2410 "from_as_string": "08-2012",
2411 "doc_count": 2
2412 }
2413 ]
2414 }
2415 }`
2416
2417 aggs := new(Aggregations)
2418 err := json.Unmarshal([]byte(s), &aggs)
2419 if err != nil {
2420 t.Fatalf("expected no error decoding; got: %v", err)
2421 }
2422
2423 agg, found := aggs.DateRange("range")
2424 if !found {
2425 t.Fatalf("expected aggregation to be found; got: %v", found)
2426 }
2427 if agg == nil {
2428 t.Fatalf("expected aggregation != nil; got: %v", agg)
2429 }
2430 if agg.Buckets == nil {
2431 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2432 }
2433 if len(agg.Buckets) != 2 {
2434 t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
2435 }
2436 if agg.Buckets[0].From != nil {
2437 t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
2438 }
2439 if agg.Buckets[0].To == nil {
2440 t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
2441 }
2442 if *agg.Buckets[0].To != float64(1.3437792E+12) {
2443 t.Errorf("expected To = %v; got: %v", float64(1.3437792E+12), *agg.Buckets[0].To)
2444 }
2445 if agg.Buckets[0].ToAsString != "08-2012" {
2446 t.Errorf("expected ToAsString = %q; got: %q", "08-2012", agg.Buckets[0].ToAsString)
2447 }
2448 if agg.Buckets[0].DocCount != 7 {
2449 t.Errorf("expected DocCount = %d; got: %d", 7, agg.Buckets[0].DocCount)
2450 }
2451 if agg.Buckets[1].From == nil {
2452 t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
2453 }
2454 if *agg.Buckets[1].From != float64(1.3437792E+12) {
2455 t.Errorf("expected From = %v; got: %v", float64(1.3437792E+12), *agg.Buckets[1].From)
2456 }
2457 if agg.Buckets[1].FromAsString != "08-2012" {
2458 t.Errorf("expected FromAsString = %q; got: %q", "08-2012", agg.Buckets[1].FromAsString)
2459 }
2460 if agg.Buckets[1].To != nil {
2461 t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[1].To)
2462 }
2463 if agg.Buckets[1].DocCount != 2 {
2464 t.Errorf("expected DocCount = %d; got: %d", 2, agg.Buckets[1].DocCount)
2465 }
2466 }
2467
2468 func TestAggsBucketIPRange(t *testing.T) {
2469 s := `{
2470 "ip_ranges": {
2471 "buckets" : [
2472 {
2473 "to": 167772165,
2474 "to_as_string": "10.0.0.5",
2475 "doc_count": 4
2476 },
2477 {
2478 "from": 167772165,
2479 "from_as_string": "10.0.0.5",
2480 "doc_count": 6
2481 }
2482 ]
2483 }
2484 }`
2485
2486 aggs := new(Aggregations)
2487 err := json.Unmarshal([]byte(s), &aggs)
2488 if err != nil {
2489 t.Fatalf("expected no error decoding; got: %v", err)
2490 }
2491
2492 agg, found := aggs.IPRange("ip_ranges")
2493 if !found {
2494 t.Fatalf("expected aggregation to be found; got: %v", found)
2495 }
2496 if agg == nil {
2497 t.Fatalf("expected aggregation != nil; got: %v", agg)
2498 }
2499 if agg.Buckets == nil {
2500 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2501 }
2502 if len(agg.Buckets) != 2 {
2503 t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
2504 }
2505 if agg.Buckets[0].From != nil {
2506 t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
2507 }
2508 if agg.Buckets[0].To == nil {
2509 t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
2510 }
2511 if *agg.Buckets[0].To != float64(167772165) {
2512 t.Errorf("expected To = %v; got: %v", float64(167772165), *agg.Buckets[0].To)
2513 }
2514 if agg.Buckets[0].ToAsString != "10.0.0.5" {
2515 t.Errorf("expected ToAsString = %q; got: %q", "10.0.0.5", agg.Buckets[0].ToAsString)
2516 }
2517 if agg.Buckets[0].DocCount != 4 {
2518 t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[0].DocCount)
2519 }
2520 if agg.Buckets[1].From == nil {
2521 t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
2522 }
2523 if *agg.Buckets[1].From != float64(167772165) {
2524 t.Errorf("expected From = %v; got: %v", float64(167772165), *agg.Buckets[1].From)
2525 }
2526 if agg.Buckets[1].FromAsString != "10.0.0.5" {
2527 t.Errorf("expected FromAsString = %q; got: %q", "10.0.0.5", agg.Buckets[1].FromAsString)
2528 }
2529 if agg.Buckets[1].To != nil {
2530 t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[1].To)
2531 }
2532 if agg.Buckets[1].DocCount != 6 {
2533 t.Errorf("expected DocCount = %d; got: %d", 6, agg.Buckets[1].DocCount)
2534 }
2535 }
2536
2537 func TestAggsBucketHistogram(t *testing.T) {
2538 s := `{
2539 "prices" : {
2540 "buckets": [
2541 {
2542 "key": 0,
2543 "doc_count": 2
2544 },
2545 {
2546 "key": 50,
2547 "doc_count": 4
2548 },
2549 {
2550 "key": 150,
2551 "doc_count": 3
2552 }
2553 ]
2554 }
2555 }`
2556
2557 aggs := new(Aggregations)
2558 err := json.Unmarshal([]byte(s), &aggs)
2559 if err != nil {
2560 t.Fatalf("expected no error decoding; got: %v", err)
2561 }
2562
2563 agg, found := aggs.Histogram("prices")
2564 if !found {
2565 t.Fatalf("expected aggregation to be found; got: %v", found)
2566 }
2567 if agg == nil {
2568 t.Fatalf("expected aggregation != nil; got: %v", agg)
2569 }
2570 if agg.Buckets == nil {
2571 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2572 }
2573 if len(agg.Buckets) != 3 {
2574 t.Errorf("expected %d buckets; got: %d", 3, len(agg.Buckets))
2575 }
2576 if agg.Buckets[0].Key != 0 {
2577 t.Errorf("expected key = %v; got: %v", 0, agg.Buckets[0].Key)
2578 }
2579 if agg.Buckets[0].KeyAsString != nil {
2580 t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[0].KeyAsString)
2581 }
2582 if agg.Buckets[0].DocCount != 2 {
2583 t.Errorf("expected doc count = %d; got: %d", 2, agg.Buckets[0].DocCount)
2584 }
2585 if agg.Buckets[1].Key != 50 {
2586 t.Errorf("expected key = %v; got: %v", 50, agg.Buckets[1].Key)
2587 }
2588 if agg.Buckets[1].KeyAsString != nil {
2589 t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[1].KeyAsString)
2590 }
2591 if agg.Buckets[1].DocCount != 4 {
2592 t.Errorf("expected doc count = %d; got: %d", 4, agg.Buckets[1].DocCount)
2593 }
2594 if agg.Buckets[2].Key != 150 {
2595 t.Errorf("expected key = %v; got: %v", 150, agg.Buckets[2].Key)
2596 }
2597 if agg.Buckets[2].KeyAsString != nil {
2598 t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[2].KeyAsString)
2599 }
2600 if agg.Buckets[2].DocCount != 3 {
2601 t.Errorf("expected doc count = %d; got: %d", 3, agg.Buckets[2].DocCount)
2602 }
2603 }
2604
2605 func TestAggsBucketDateHistogram(t *testing.T) {
2606 s := `{
2607 "articles_over_time": {
2608 "buckets": [
2609 {
2610 "key_as_string": "2013-02-02",
2611 "key": 1328140800000,
2612 "doc_count": 1
2613 },
2614 {
2615 "key_as_string": "2013-03-02",
2616 "key": 1330646400000,
2617 "doc_count": 2
2618 }
2619 ]
2620 }
2621 }`
2622
2623 aggs := new(Aggregations)
2624 err := json.Unmarshal([]byte(s), &aggs)
2625 if err != nil {
2626 t.Fatalf("expected no error decoding; got: %v", err)
2627 }
2628
2629 agg, found := aggs.DateHistogram("articles_over_time")
2630 if !found {
2631 t.Fatalf("expected aggregation to be found; got: %v", found)
2632 }
2633 if agg == nil {
2634 t.Fatalf("expected aggregation != nil; got: %v", agg)
2635 }
2636 if agg.Buckets == nil {
2637 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2638 }
2639 if len(agg.Buckets) != 2 {
2640 t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
2641 }
2642 if agg.Buckets[0].Key != 1328140800000 {
2643 t.Errorf("expected key %v; got: %v", 1328140800000, agg.Buckets[0].Key)
2644 }
2645 if agg.Buckets[0].KeyAsString == nil {
2646 t.Fatalf("expected key_as_string != nil; got: %v", agg.Buckets[0].KeyAsString)
2647 }
2648 if *agg.Buckets[0].KeyAsString != "2013-02-02" {
2649 t.Errorf("expected key_as_string %q; got: %q", "2013-02-02", *agg.Buckets[0].KeyAsString)
2650 }
2651 if agg.Buckets[0].DocCount != 1 {
2652 t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[0].DocCount)
2653 }
2654 if agg.Buckets[1].Key != 1330646400000 {
2655 t.Errorf("expected key %v; got: %v", 1330646400000, agg.Buckets[1].Key)
2656 }
2657 if agg.Buckets[1].KeyAsString == nil {
2658 t.Fatalf("expected key_as_string != nil; got: %v", agg.Buckets[1].KeyAsString)
2659 }
2660 if *agg.Buckets[1].KeyAsString != "2013-03-02" {
2661 t.Errorf("expected key_as_string %q; got: %q", "2013-03-02", *agg.Buckets[1].KeyAsString)
2662 }
2663 if agg.Buckets[1].DocCount != 2 {
2664 t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[1].DocCount)
2665 }
2666 }
2667
2668 func TestAggsMetricsGeoBounds(t *testing.T) {
2669 s := `{
2670 "viewport": {
2671 "bounds": {
2672 "top_left": {
2673 "lat": 80.45,
2674 "lon": -160.22
2675 },
2676 "bottom_right": {
2677 "lat": 40.65,
2678 "lon": 42.57
2679 }
2680 }
2681 }
2682 }`
2683
2684 aggs := new(Aggregations)
2685 err := json.Unmarshal([]byte(s), &aggs)
2686 if err != nil {
2687 t.Fatalf("expected no error decoding; got: %v", err)
2688 }
2689
2690 agg, found := aggs.GeoBounds("viewport")
2691 if !found {
2692 t.Fatalf("expected aggregation to be found; got: %v", found)
2693 }
2694 if agg == nil {
2695 t.Fatalf("expected aggregation != nil; got: %v", agg)
2696 }
2697 if agg.Bounds.TopLeft.Latitude != float64(80.45) {
2698 t.Fatalf("expected Bounds.TopLeft.Latitude != %v; got: %v", float64(80.45), agg.Bounds.TopLeft.Latitude)
2699 }
2700 if agg.Bounds.TopLeft.Longitude != float64(-160.22) {
2701 t.Fatalf("expected Bounds.TopLeft.Longitude != %v; got: %v", float64(-160.22), agg.Bounds.TopLeft.Longitude)
2702 }
2703 if agg.Bounds.BottomRight.Latitude != float64(40.65) {
2704 t.Fatalf("expected Bounds.BottomRight.Latitude != %v; got: %v", float64(40.65), agg.Bounds.BottomRight.Latitude)
2705 }
2706 if agg.Bounds.BottomRight.Longitude != float64(42.57) {
2707 t.Fatalf("expected Bounds.BottomRight.Longitude != %v; got: %v", float64(42.57), agg.Bounds.BottomRight.Longitude)
2708 }
2709 }
2710
2711 func TestAggsBucketGeoHash(t *testing.T) {
2712 s := `{
2713 "myLarge-GrainGeoHashGrid": {
2714 "buckets": [
2715 {
2716 "key": "svz",
2717 "doc_count": 10964
2718 },
2719 {
2720 "key": "sv8",
2721 "doc_count": 3198
2722 }
2723 ]
2724 }
2725 }`
2726
2727 aggs := new(Aggregations)
2728 err := json.Unmarshal([]byte(s), &aggs)
2729 if err != nil {
2730 t.Fatalf("expected no error decoding; got: %v", err)
2731 }
2732
2733 agg, found := aggs.GeoHash("myLarge-GrainGeoHashGrid")
2734 if !found {
2735 t.Fatalf("expected aggregation to be found; got: %v", found)
2736 }
2737 if agg == nil {
2738 t.Fatalf("expected aggregation != nil; got: %v", agg)
2739 }
2740 if agg.Buckets == nil {
2741 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2742 }
2743 if len(agg.Buckets) != 2 {
2744 t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
2745 }
2746 if agg.Buckets[0].Key != "svz" {
2747 t.Errorf("expected key %q; got: %q", "svz", agg.Buckets[0].Key)
2748 }
2749 if agg.Buckets[0].DocCount != 10964 {
2750 t.Errorf("expected doc count %d; got: %d", 10964, agg.Buckets[0].DocCount)
2751 }
2752 if agg.Buckets[1].Key != "sv8" {
2753 t.Errorf("expected key %q; got: %q", "sv8", agg.Buckets[1].Key)
2754 }
2755 if agg.Buckets[1].DocCount != 3198 {
2756 t.Errorf("expected doc count %d; got: %d", 3198, agg.Buckets[1].DocCount)
2757 }
2758 }
2759
2760 func TestAggsMetricsGeoCentroid(t *testing.T) {
2761 s := `{
2762 "centroid": {
2763 "location": {
2764 "lat": 80.45,
2765 "lon": -160.22
2766 },
2767 "count": 6
2768 }
2769 }`
2770
2771 aggs := new(Aggregations)
2772 err := json.Unmarshal([]byte(s), &aggs)
2773 if err != nil {
2774 t.Fatalf("expected no error decoding; got: %v", err)
2775 }
2776
2777 agg, found := aggs.GeoCentroid("centroid")
2778 if !found {
2779 t.Fatalf("expected aggregation to be found; got: %v", found)
2780 }
2781 if agg == nil {
2782 t.Fatalf("expected aggregation != nil; got: %v", agg)
2783 }
2784 if agg.Location.Latitude != float64(80.45) {
2785 t.Fatalf("expected Location.Latitude != %v; got: %v", float64(80.45), agg.Location.Latitude)
2786 }
2787 if agg.Location.Longitude != float64(-160.22) {
2788 t.Fatalf("expected Location.Longitude != %v; got: %v", float64(-160.22), agg.Location.Longitude)
2789 }
2790 if agg.Count != int(6) {
2791 t.Fatalf("expected Count != %v; got: %v", int(6), agg.Count)
2792 }
2793 }
2794
2795 func TestAggsBucketGeoDistance(t *testing.T) {
2796 s := `{
2797 "rings" : {
2798 "buckets": [
2799 {
2800 "unit": "km",
2801 "to": 100.0,
2802 "doc_count": 3
2803 },
2804 {
2805 "unit": "km",
2806 "from": 100.0,
2807 "to": 300.0,
2808 "doc_count": 1
2809 },
2810 {
2811 "unit": "km",
2812 "from": 300.0,
2813 "doc_count": 7
2814 }
2815 ]
2816 }
2817 }`
2818
2819 aggs := new(Aggregations)
2820 err := json.Unmarshal([]byte(s), &aggs)
2821 if err != nil {
2822 t.Fatalf("expected no error decoding; got: %v", err)
2823 }
2824
2825 agg, found := aggs.GeoDistance("rings")
2826 if !found {
2827 t.Fatalf("expected aggregation to be found; got: %v", found)
2828 }
2829 if agg == nil {
2830 t.Fatalf("expected aggregation != nil; got: %v", agg)
2831 }
2832 if agg.Buckets == nil {
2833 t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
2834 }
2835 if len(agg.Buckets) != 3 {
2836 t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
2837 }
2838 if agg.Buckets[0].From != nil {
2839 t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
2840 }
2841 if agg.Buckets[0].To == nil {
2842 t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
2843 }
2844 if *agg.Buckets[0].To != float64(100.0) {
2845 t.Errorf("expected To = %v; got: %v", float64(100.0), *agg.Buckets[0].To)
2846 }
2847 if agg.Buckets[0].DocCount != 3 {
2848 t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[0].DocCount)
2849 }
2850
2851 if agg.Buckets[1].From == nil {
2852 t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
2853 }
2854 if *agg.Buckets[1].From != float64(100.0) {
2855 t.Errorf("expected From = %v; got: %v", float64(100.0), *agg.Buckets[1].From)
2856 }
2857 if agg.Buckets[1].To == nil {
2858 t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[1].To)
2859 }
2860 if *agg.Buckets[1].To != float64(300.0) {
2861 t.Errorf("expected From = %v; got: %v", float64(300.0), *agg.Buckets[1].To)
2862 }
2863 if agg.Buckets[1].DocCount != 1 {
2864 t.Errorf("expected DocCount = %d; got: %d", 1, agg.Buckets[1].DocCount)
2865 }
2866
2867 if agg.Buckets[2].From == nil {
2868 t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[2].From)
2869 }
2870 if *agg.Buckets[2].From != float64(300.0) {
2871 t.Errorf("expected From = %v; got: %v", float64(300.0), *agg.Buckets[2].From)
2872 }
2873 if agg.Buckets[2].To != nil {
2874 t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[2].To)
2875 }
2876 if agg.Buckets[2].DocCount != 7 {
2877 t.Errorf("expected DocCount = %d; got: %d", 7, agg.Buckets[2].DocCount)
2878 }
2879 }
2880
2881 func TestAggsSubAggregates(t *testing.T) {
2882 rs := `{
2883 "users" : {
2884 "doc_count_error_upper_bound" : 1,
2885 "sum_other_doc_count" : 2,
2886 "buckets" : [ {
2887 "key" : "olivere",
2888 "doc_count" : 2,
2889 "ts" : {
2890 "buckets" : [ {
2891 "key_as_string" : "2012-01-01T00:00:00.000Z",
2892 "key" : 1325376000000,
2893 "doc_count" : 2
2894 } ]
2895 }
2896 }, {
2897 "key" : "sandrae",
2898 "doc_count" : 1,
2899 "ts" : {
2900 "buckets" : [ {
2901 "key_as_string" : "2011-01-01T00:00:00.000Z",
2902 "key" : 1293840000000,
2903 "doc_count" : 1
2904 } ]
2905 }
2906 } ]
2907 }
2908 }`
2909
2910 aggs := new(Aggregations)
2911 err := json.Unmarshal([]byte(rs), &aggs)
2912 if err != nil {
2913 t.Fatalf("expected no error decoding; got: %v", err)
2914 }
2915
2916 // Access top-level aggregation
2917 users, found := aggs.Terms("users")
2918 if !found {
2919 t.Fatalf("expected users aggregation to be found; got: %v", found)
2920 }
2921 if users == nil {
2922 t.Fatalf("expected users aggregation; got: %v", users)
2923 }
2924 if users.Buckets == nil {
2925 t.Fatalf("expected users buckets; got: %v", users.Buckets)
2926 }
2927 if len(users.Buckets) != 2 {
2928 t.Errorf("expected %d bucket entries; got: %d", 2, len(users.Buckets))
2929 }
2930 if users.Buckets[0].Key != "olivere" {
2931 t.Errorf("expected key %q; got: %q", "olivere", users.Buckets[0].Key)
2932 }
2933 if users.Buckets[0].DocCount != 2 {
2934 t.Errorf("expected doc count %d; got: %d", 2, users.Buckets[0].DocCount)
2935 }
2936 if users.Buckets[1].Key != "sandrae" {
2937 t.Errorf("expected key %q; got: %q", "sandrae", users.Buckets[1].Key)
2938 }
2939 if users.Buckets[1].DocCount != 1 {
2940 t.Errorf("expected doc count %d; got: %d", 1, users.Buckets[1].DocCount)
2941 }
2942
2943 // Access sub-aggregation
2944 ts, found := users.Buckets[0].DateHistogram("ts")
2945 if !found {
2946 t.Fatalf("expected ts aggregation to be found; got: %v", found)
2947 }
2948 if ts == nil {
2949 t.Fatalf("expected ts aggregation; got: %v", ts)
2950 }
2951 if ts.Buckets == nil {
2952 t.Fatalf("expected ts buckets; got: %v", ts.Buckets)
2953 }
2954 if len(ts.Buckets) != 1 {
2955 t.Errorf("expected %d bucket entries; got: %d", 1, len(ts.Buckets))
2956 }
2957 if ts.Buckets[0].Key != 1325376000000 {
2958 t.Errorf("expected key %v; got: %v", 1325376000000, ts.Buckets[0].Key)
2959 }
2960 if ts.Buckets[0].KeyAsString == nil {
2961 t.Fatalf("expected key_as_string != %v; got: %v", nil, ts.Buckets[0].KeyAsString)
2962 }
2963 if *ts.Buckets[0].KeyAsString != "2012-01-01T00:00:00.000Z" {
2964 t.Errorf("expected key_as_string %q; got: %q", "2012-01-01T00:00:00.000Z", *ts.Buckets[0].KeyAsString)
2965 }
2966 }
2967
2968 func TestAggsPipelineAvgBucket(t *testing.T) {
2969 s := `{
2970 "avg_monthly_sales" : {
2971 "value" : 328.33333333333333
2972 }
2973 }`
2974
2975 aggs := new(Aggregations)
2976 err := json.Unmarshal([]byte(s), &aggs)
2977 if err != nil {
2978 t.Fatalf("expected no error decoding; got: %v", err)
2979 }
2980
2981 agg, found := aggs.AvgBucket("avg_monthly_sales")
2982 if !found {
2983 t.Fatalf("expected aggregation to be found; got: %v", found)
2984 }
2985 if agg == nil {
2986 t.Fatalf("expected aggregation != nil; got: %v", agg)
2987 }
2988 if agg.Value == nil {
2989 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
2990 }
2991 if *agg.Value != float64(328.33333333333333) {
2992 t.Fatalf("expected aggregation value = %v; got: %v", float64(328.33333333333333), *agg.Value)
2993 }
2994 }
2995
2996 func TestAggsPipelineSumBucket(t *testing.T) {
2997 s := `{
2998 "sum_monthly_sales" : {
2999 "value" : 985
3000 }
3001 }`
3002
3003 aggs := new(Aggregations)
3004 err := json.Unmarshal([]byte(s), &aggs)
3005 if err != nil {
3006 t.Fatalf("expected no error decoding; got: %v", err)
3007 }
3008
3009 agg, found := aggs.SumBucket("sum_monthly_sales")
3010 if !found {
3011 t.Fatalf("expected aggregation to be found; got: %v", found)
3012 }
3013 if agg == nil {
3014 t.Fatalf("expected aggregation != nil; got: %v", agg)
3015 }
3016 if agg.Value == nil {
3017 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
3018 }
3019 if *agg.Value != float64(985) {
3020 t.Fatalf("expected aggregation value = %v; got: %v", float64(985), *agg.Value)
3021 }
3022 }
3023
3024 func TestAggsPipelineMaxBucket(t *testing.T) {
3025 s := `{
3026 "max_monthly_sales" : {
3027 "keys": ["2015/01/01 00:00:00"],
3028 "value" : 550
3029 }
3030 }`
3031
3032 aggs := new(Aggregations)
3033 err := json.Unmarshal([]byte(s), &aggs)
3034 if err != nil {
3035 t.Fatalf("expected no error decoding; got: %v", err)
3036 }
3037
3038 agg, found := aggs.MaxBucket("max_monthly_sales")
3039 if !found {
3040 t.Fatalf("expected aggregation to be found; got: %v", found)
3041 }
3042 if agg == nil {
3043 t.Fatalf("expected aggregation != nil; got: %v", agg)
3044 }
3045 if len(agg.Keys) != 1 {
3046 t.Fatalf("expected 1 key; got: %d", len(agg.Keys))
3047 }
3048 if got, want := agg.Keys[0], "2015/01/01 00:00:00"; got != want {
3049 t.Fatalf("expected key %q; got: %v (%T)", want, got, got)
3050 }
3051 if agg.Value == nil {
3052 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
3053 }
3054 if *agg.Value != float64(550) {
3055 t.Fatalf("expected aggregation value = %v; got: %v", float64(550), *agg.Value)
3056 }
3057 }
3058
3059 func TestAggsPipelineMinBucket(t *testing.T) {
3060 s := `{
3061 "min_monthly_sales" : {
3062 "keys": ["2015/02/01 00:00:00"],
3063 "value" : 60
3064 }
3065 }`
3066
3067 aggs := new(Aggregations)
3068 err := json.Unmarshal([]byte(s), &aggs)
3069 if err != nil {
3070 t.Fatalf("expected no error decoding; got: %v", err)
3071 }
3072
3073 agg, found := aggs.MinBucket("min_monthly_sales")
3074 if !found {
3075 t.Fatalf("expected aggregation to be found; got: %v", found)
3076 }
3077 if agg == nil {
3078 t.Fatalf("expected aggregation != nil; got: %v", agg)
3079 }
3080 if len(agg.Keys) != 1 {
3081 t.Fatalf("expected 1 key; got: %d", len(agg.Keys))
3082 }
3083 if got, want := agg.Keys[0], "2015/02/01 00:00:00"; got != want {
3084 t.Fatalf("expected key %q; got: %v (%T)", want, got, got)
3085 }
3086 if agg.Value == nil {
3087 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
3088 }
3089 if *agg.Value != float64(60) {
3090 t.Fatalf("expected aggregation value = %v; got: %v", float64(60), *agg.Value)
3091 }
3092 }
3093
3094 func TestAggsPipelineMovAvg(t *testing.T) {
3095 s := `{
3096 "the_movavg" : {
3097 "value" : 12.0
3098 }
3099 }`
3100
3101 aggs := new(Aggregations)
3102 err := json.Unmarshal([]byte(s), &aggs)
3103 if err != nil {
3104 t.Fatalf("expected no error decoding; got: %v", err)
3105 }
3106
3107 agg, found := aggs.MovAvg("the_movavg")
3108 if !found {
3109 t.Fatalf("expected aggregation to be found; got: %v", found)
3110 }
3111 if agg == nil {
3112 t.Fatalf("expected aggregation != nil; got: %v", agg)
3113 }
3114 if agg.Value == nil {
3115 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
3116 }
3117 if *agg.Value != float64(12.0) {
3118 t.Fatalf("expected aggregation value = %v; got: %v", float64(12.0), *agg.Value)
3119 }
3120 }
3121
3122 func TestAggsPipelineDerivative(t *testing.T) {
3123 s := `{
3124 "sales_deriv" : {
3125 "value" : 315
3126 }
3127 }`
3128
3129 aggs := new(Aggregations)
3130 err := json.Unmarshal([]byte(s), &aggs)
3131 if err != nil {
3132 t.Fatalf("expected no error decoding; got: %v", err)
3133 }
3134
3135 agg, found := aggs.Derivative("sales_deriv")
3136 if !found {
3137 t.Fatalf("expected aggregation to be found; got: %v", found)
3138 }
3139 if agg == nil {
3140 t.Fatalf("expected aggregation != nil; got: %v", agg)
3141 }
3142 if agg.Value == nil {
3143 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
3144 }
3145 if *agg.Value != float64(315) {
3146 t.Fatalf("expected aggregation value = %v; got: %v", float64(315), *agg.Value)
3147 }
3148 }
3149
3150 func TestAggsPipelinePercentilesBucket(t *testing.T) {
3151 s := `{
3152 "sales_percentiles": {
3153 "values": {
3154 "25.0": 100,
3155 "50.0": 200,
3156 "75.0": 300
3157 }
3158 }
3159 }`
3160 aggs := new(Aggregations)
3161 err := json.Unmarshal([]byte(s), &aggs)
3162 if err != nil {
3163 t.Fatalf("expected no error decoding; got: %v", err)
3164 }
3165
3166 agg, found := aggs.PercentilesBucket("sales_percentiles")
3167 if !found {
3168 t.Fatalf("expected aggregation to be found; got: %v", found)
3169 }
3170 if agg == nil {
3171 t.Fatalf("expected aggregation != nil; got: %v", agg)
3172 }
3173 if len(agg.Values) != 3 {
3174 t.Fatalf("expected aggregation map with three entries; got: %v", agg.Values)
3175 }
3176 }
3177
3178 func TestAggsPipelineStatsBucket(t *testing.T) {
3179 s := `{
3180 "stats_monthly_sales": {
3181 "count": 3,
3182 "min": 60.0,
3183 "max": 550.0,
3184 "avg": 328.3333333333333,
3185 "sum": 985.0
3186 }
3187 }`
3188
3189 aggs := new(Aggregations)
3190 err := json.Unmarshal([]byte(s), &aggs)
3191 if err != nil {
3192 t.Fatalf("expected no error decoding; got: %v", err)
3193 }
3194
3195 agg, found := aggs.StatsBucket("stats_monthly_sales")
3196 if !found {
3197 t.Fatalf("expected aggregation to be found; got: %v", found)
3198 }
3199 if agg == nil {
3200 t.Fatalf("expected aggregation != nil; got: %v", agg)
3201 }
3202 if agg.Count != 3 {
3203 t.Fatalf("expected aggregation count = %v; got: %v", 3, agg.Count)
3204 }
3205 if agg.Min == nil {
3206 t.Fatalf("expected aggregation min != nil; got: %v", agg.Min)
3207 }
3208 if *agg.Min != float64(60.0) {
3209 t.Fatalf("expected aggregation min = %v; got: %v", float64(60.0), *agg.Min)
3210 }
3211 if agg.Max == nil {
3212 t.Fatalf("expected aggregation max != nil; got: %v", agg.Max)
3213 }
3214 if *agg.Max != float64(550.0) {
3215 t.Fatalf("expected aggregation max = %v; got: %v", float64(550.0), *agg.Max)
3216 }
3217 if agg.Avg == nil {
3218 t.Fatalf("expected aggregation avg != nil; got: %v", agg.Avg)
3219 }
3220 if *agg.Avg != float64(328.3333333333333) {
3221 t.Fatalf("expected aggregation average = %v; got: %v", float64(328.3333333333333), *agg.Avg)
3222 }
3223 if agg.Sum == nil {
3224 t.Fatalf("expected aggregation sum != nil; got: %v", agg.Sum)
3225 }
3226 if *agg.Sum != float64(985.0) {
3227 t.Fatalf("expected aggregation sum = %v; got: %v", float64(985.0), *agg.Sum)
3228 }
3229 }
3230
3231 func TestAggsPipelineCumulativeSum(t *testing.T) {
3232 s := `{
3233 "cumulative_sales" : {
3234 "value" : 550
3235 }
3236 }`
3237
3238 aggs := new(Aggregations)
3239 err := json.Unmarshal([]byte(s), &aggs)
3240 if err != nil {
3241 t.Fatalf("expected no error decoding; got: %v", err)
3242 }
3243
3244 agg, found := aggs.CumulativeSum("cumulative_sales")
3245 if !found {
3246 t.Fatalf("expected aggregation to be found; got: %v", found)
3247 }
3248 if agg == nil {
3249 t.Fatalf("expected aggregation != nil; got: %v", agg)
3250 }
3251 if agg.Value == nil {
3252 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
3253 }
3254 if *agg.Value != float64(550) {
3255 t.Fatalf("expected aggregation value = %v; got: %v", float64(550), *agg.Value)
3256 }
3257 }
3258
3259 func TestAggsPipelineBucketScript(t *testing.T) {
3260 s := `{
3261 "t-shirt-percentage" : {
3262 "value" : 20
3263 }
3264 }`
3265
3266 aggs := new(Aggregations)
3267 err := json.Unmarshal([]byte(s), &aggs)
3268 if err != nil {
3269 t.Fatalf("expected no error decoding; got: %v", err)
3270 }
3271
3272 agg, found := aggs.BucketScript("t-shirt-percentage")
3273 if !found {
3274 t.Fatalf("expected aggregation to be found; got: %v", found)
3275 }
3276 if agg == nil {
3277 t.Fatalf("expected aggregation != nil; got: %v", agg)
3278 }
3279 if agg.Value == nil {
3280 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
3281 }
3282 if *agg.Value != float64(20) {
3283 t.Fatalf("expected aggregation value = %v; got: %v", float64(20), *agg.Value)
3284 }
3285 }
3286
3287 func TestAggsPipelineSerialDiff(t *testing.T) {
3288 s := `{
3289 "the_diff" : {
3290 "value" : -722.0
3291 }
3292 }`
3293
3294 aggs := new(Aggregations)
3295 err := json.Unmarshal([]byte(s), &aggs)
3296 if err != nil {
3297 t.Fatalf("expected no error decoding; got: %v", err)
3298 }
3299
3300 agg, found := aggs.SerialDiff("the_diff")
3301 if !found {
3302 t.Fatalf("expected aggregation to be found; got: %v", found)
3303 }
3304 if agg == nil {
3305 t.Fatalf("expected aggregation != nil; got: %v", agg)
3306 }
3307 if agg.Value == nil {
3308 t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
3309 }
3310 if *agg.Value != float64(-722.0) {
3311 t.Fatalf("expected aggregation value = %v; got: %v", float64(20), *agg.Value)
3312 }
3313 }