Debian Codegraph @

search_aggs_pipeline_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 "testing"
10 )
11
12 func TestAggsIntegrationAvgBucket(t *testing.T) {
13 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
14 client := setupTestClientAndCreateIndexAndAddDocs(t)
15
16 esversion, err := client.ElasticsearchVersion(DefaultURL)
17 if err != nil {
18 t.Fatal(err)
19 }
20
21 if esversion < "2.0" {
22 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
23 return
24 }
25
26 // Match all should return all documents
27 builder := client.Search().
28 Index(testIndexName).
29 Type("order").
30 Query(NewMatchAllQuery()).
31 Pretty(true)
32 h := NewDateHistogramAggregation().Field("time").Interval("month")
33 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
34 builder = builder.Aggregation("sales_per_month", h)
35 builder = builder.Aggregation("avg_monthly_sales", NewAvgBucketAggregation().BucketsPath("sales_per_month>sales"))
36
37 res, err := builder.Do(context.TODO())
38 if err != nil {
39 t.Fatal(err)
40 }
41 if res.Hits == nil {
42 t.Errorf("expected Hits != nil; got: nil")
43 }
44
45 aggs := res.Aggregations
46 if aggs == nil {
47 t.Fatal("expected aggregations != nil; got: nil")
48 }
49
50 agg, found := aggs.AvgBucket("avg_monthly_sales")
51 if !found {
52 t.Fatal("expected avg_monthly_sales aggregation")
53 }
54 if agg == nil {
55 t.Fatal("expected avg_monthly_sales aggregation")
56 }
57 if agg.Value == nil {
58 t.Fatal("expected avg_monthly_sales.value != nil")
59 }
60 if got, want := *agg.Value, float64(939.2); got != want {
61 t.Fatalf("expected avg_monthly_sales.value=%v; got: %v", want, got)
62 }
63 }
64
65 func TestAggsIntegrationDerivative(t *testing.T) {
66 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
67 client := setupTestClientAndCreateIndexAndAddDocs(t)
68
69 esversion, err := client.ElasticsearchVersion(DefaultURL)
70 if err != nil {
71 t.Fatal(err)
72 }
73
74 if esversion < "2.0" {
75 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
76 return
77 }
78
79 // Match all should return all documents
80 builder := client.Search().
81 Index(testIndexName).
82 Type("order").
83 Query(NewMatchAllQuery()).
84 Pretty(true)
85 h := NewDateHistogramAggregation().Field("time").Interval("month")
86 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
87 h = h.SubAggregation("sales_deriv", NewDerivativeAggregation().BucketsPath("sales"))
88 builder = builder.Aggregation("sales_per_month", h)
89
90 res, err := builder.Do(context.TODO())
91 if err != nil {
92 t.Fatal(err)
93 }
94 if res.Hits == nil {
95 t.Errorf("expected Hits != nil; got: nil")
96 }
97
98 aggs := res.Aggregations
99 if aggs == nil {
100 t.Fatal("expected aggregations != nil; got: nil")
101 }
102
103 agg, found := aggs.DateHistogram("sales_per_month")
104 if !found {
105 t.Fatal("expected sales_per_month aggregation")
106 }
107 if agg == nil {
108 t.Fatal("expected sales_per_month aggregation")
109 }
110 if got, want := len(agg.Buckets), 6; got != want {
111 t.Fatalf("expected %d buckets; got: %d", want, got)
112 }
113
114 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
115 t.Fatalf("expected DocCount=%d; got: %d", want, got)
116 }
117 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {
118 t.Fatalf("expected DocCount=%d; got: %d", want, got)
119 }
120 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {
121 t.Fatalf("expected DocCount=%d; got: %d", want, got)
122 }
123 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {
124 t.Fatalf("expected DocCount=%d; got: %d", want, got)
125 }
126 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {
127 t.Fatalf("expected DocCount=%d; got: %d", want, got)
128 }
129 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {
130 t.Fatalf("expected DocCount=%d; got: %d", want, got)
131 }
132
133 d, found := agg.Buckets[0].Derivative("sales_deriv")
134 if found {
135 t.Fatal("expected no sales_deriv aggregation")
136 }
137 if d != nil {
138 t.Fatal("expected no sales_deriv aggregation")
139 }
140
141 d, found = agg.Buckets[1].Derivative("sales_deriv")
142 if !found {
143 t.Fatal("expected sales_deriv aggregation")
144 }
145 if d == nil {
146 t.Fatal("expected sales_deriv aggregation")
147 }
148 if d.Value != nil {
149 t.Fatal("expected sales_deriv value == nil")
150 }
151
152 d, found = agg.Buckets[2].Derivative("sales_deriv")
153 if !found {
154 t.Fatal("expected sales_deriv aggregation")
155 }
156 if d == nil {
157 t.Fatal("expected sales_deriv aggregation")
158 }
159 if d.Value != nil {
160 t.Fatal("expected sales_deriv value == nil")
161 }
162
163 d, found = agg.Buckets[3].Derivative("sales_deriv")
164 if !found {
165 t.Fatal("expected sales_deriv aggregation")
166 }
167 if d == nil {
168 t.Fatal("expected sales_deriv aggregation")
169 }
170 if d.Value == nil {
171 t.Fatal("expected sales_deriv value != nil")
172 }
173 if got, want := *d.Value, float64(2348.0); got != want {
174 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)
175 }
176
177 d, found = agg.Buckets[4].Derivative("sales_deriv")
178 if !found {
179 t.Fatal("expected sales_deriv aggregation")
180 }
181 if d == nil {
182 t.Fatal("expected sales_deriv aggregation")
183 }
184 if d.Value == nil {
185 t.Fatal("expected sales_deriv value != nil")
186 }
187 if got, want := *d.Value, float64(-1658.0); got != want {
188 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)
189 }
190
191 d, found = agg.Buckets[5].Derivative("sales_deriv")
192 if !found {
193 t.Fatal("expected sales_deriv aggregation")
194 }
195 if d == nil {
196 t.Fatal("expected sales_deriv aggregation")
197 }
198 if d.Value == nil {
199 t.Fatal("expected sales_deriv value != nil")
200 }
201 if got, want := *d.Value, float64(-722.0); got != want {
202 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)
203 }
204 }
205
206 func TestAggsIntegrationMaxBucket(t *testing.T) {
207 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
208 client := setupTestClientAndCreateIndexAndAddDocs(t)
209
210 esversion, err := client.ElasticsearchVersion(DefaultURL)
211 if err != nil {
212 t.Fatal(err)
213 }
214
215 if esversion < "2.0" {
216 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
217 return
218 }
219
220 // Match all should return all documents
221 builder := client.Search().
222 Index(testIndexName).
223 Type("order").
224 Query(NewMatchAllQuery()).
225 Pretty(true)
226 h := NewDateHistogramAggregation().Field("time").Interval("month")
227 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
228 builder = builder.Aggregation("sales_per_month", h)
229 builder = builder.Aggregation("max_monthly_sales", NewMaxBucketAggregation().BucketsPath("sales_per_month>sales"))
230
231 res, err := builder.Do(context.TODO())
232 if err != nil {
233 t.Fatal(err)
234 }
235 if res.Hits == nil {
236 t.Errorf("expected Hits != nil; got: nil")
237 }
238
239 aggs := res.Aggregations
240 if aggs == nil {
241 t.Fatal("expected aggregations != nil; got: nil")
242 }
243
244 agg, found := aggs.MaxBucket("max_monthly_sales")
245 if !found {
246 t.Fatal("expected max_monthly_sales aggregation")
247 }
248 if agg == nil {
249 t.Fatal("expected max_monthly_sales aggregation")
250 }
251 if got, want := len(agg.Keys), 1; got != want {
252 t.Fatalf("expected len(max_monthly_sales.keys)=%d; got: %d", want, got)
253 }
254 if got, want := agg.Keys[0], "2015-04-01"; got != want {
255 t.Fatalf("expected max_monthly_sales.keys[0]=%v; got: %v", want, got)
256 }
257 if agg.Value == nil {
258 t.Fatal("expected max_monthly_sales.value != nil")
259 }
260 if got, want := *agg.Value, float64(2448); got != want {
261 t.Fatalf("expected max_monthly_sales.value=%v; got: %v", want, got)
262 }
263 }
264
265 func TestAggsIntegrationMinBucket(t *testing.T) {
266 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
267 client := setupTestClientAndCreateIndexAndAddDocs(t)
268
269 esversion, err := client.ElasticsearchVersion(DefaultURL)
270 if err != nil {
271 t.Fatal(err)
272 }
273
274 if esversion < "2.0" {
275 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
276 return
277 }
278
279 // Match all should return all documents
280 builder := client.Search().
281 Index(testIndexName).
282 Type("order").
283 Query(NewMatchAllQuery()).
284 Pretty(true)
285 h := NewDateHistogramAggregation().Field("time").Interval("month")
286 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
287 builder = builder.Aggregation("sales_per_month", h)
288 builder = builder.Aggregation("min_monthly_sales", NewMinBucketAggregation().BucketsPath("sales_per_month>sales"))
289
290 res, err := builder.Do(context.TODO())
291 if err != nil {
292 t.Fatal(err)
293 }
294 if res.Hits == nil {
295 t.Errorf("expected Hits != nil; got: nil")
296 }
297
298 aggs := res.Aggregations
299 if aggs == nil {
300 t.Fatal("expected aggregations != nil; got: nil")
301 }
302
303 agg, found := aggs.MinBucket("min_monthly_sales")
304 if !found {
305 t.Fatal("expected min_monthly_sales aggregation")
306 }
307 if agg == nil {
308 t.Fatal("expected min_monthly_sales aggregation")
309 }
310 if got, want := len(agg.Keys), 1; got != want {
311 t.Fatalf("expected len(min_monthly_sales.keys)=%d; got: %d", want, got)
312 }
313 if got, want := agg.Keys[0], "2015-06-01"; got != want {
314 t.Fatalf("expected min_monthly_sales.keys[0]=%v; got: %v", want, got)
315 }
316 if agg.Value == nil {
317 t.Fatal("expected min_monthly_sales.value != nil")
318 }
319 if got, want := *agg.Value, float64(68); got != want {
320 t.Fatalf("expected min_monthly_sales.value=%v; got: %v", want, got)
321 }
322 }
323
324 func TestAggsIntegrationSumBucket(t *testing.T) {
325 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
326 client := setupTestClientAndCreateIndexAndAddDocs(t)
327
328 esversion, err := client.ElasticsearchVersion(DefaultURL)
329 if err != nil {
330 t.Fatal(err)
331 }
332
333 if esversion < "2.0" {
334 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
335 return
336 }
337
338 // Match all should return all documents
339 builder := client.Search().
340 Index(testIndexName).
341 Type("order").
342 Query(NewMatchAllQuery()).
343 Pretty(true)
344 h := NewDateHistogramAggregation().Field("time").Interval("month")
345 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
346 builder = builder.Aggregation("sales_per_month", h)
347 builder = builder.Aggregation("sum_monthly_sales", NewSumBucketAggregation().BucketsPath("sales_per_month>sales"))
348
349 res, err := builder.Do(context.TODO())
350 if err != nil {
351 t.Fatal(err)
352 }
353 if res.Hits == nil {
354 t.Errorf("expected Hits != nil; got: nil")
355 }
356
357 aggs := res.Aggregations
358 if aggs == nil {
359 t.Fatal("expected aggregations != nil; got: nil")
360 }
361
362 agg, found := aggs.SumBucket("sum_monthly_sales")
363 if !found {
364 t.Fatal("expected sum_monthly_sales aggregation")
365 }
366 if agg == nil {
367 t.Fatal("expected sum_monthly_sales aggregation")
368 }
369 if agg.Value == nil {
370 t.Fatal("expected sum_monthly_sales.value != nil")
371 }
372 if got, want := *agg.Value, float64(4696.0); got != want {
373 t.Fatalf("expected sum_monthly_sales.value=%v; got: %v", want, got)
374 }
375 }
376
377 func TestAggsIntegrationMovAvg(t *testing.T) {
378 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
379 client := setupTestClientAndCreateIndexAndAddDocs(t)
380
381 esversion, err := client.ElasticsearchVersion(DefaultURL)
382 if err != nil {
383 t.Fatal(err)
384 }
385
386 if esversion < "2.0" {
387 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
388 return
389 }
390
391 // Match all should return all documents
392 builder := client.Search().
393 Index(testIndexName).
394 Type("order").
395 Query(NewMatchAllQuery()).
396 Pretty(true)
397 h := NewDateHistogramAggregation().Field("time").Interval("month")
398 h = h.SubAggregation("the_sum", NewSumAggregation().Field("price"))
399 h = h.SubAggregation("the_movavg", NewMovAvgAggregation().BucketsPath("the_sum"))
400 builder = builder.Aggregation("my_date_histo", h)
401
402 res, err := builder.Do(context.TODO())
403 if err != nil {
404 t.Fatal(err)
405 }
406 if res.Hits == nil {
407 t.Errorf("expected Hits != nil; got: nil")
408 }
409
410 aggs := res.Aggregations
411 if aggs == nil {
412 t.Fatal("expected aggregations != nil; got: nil")
413 }
414
415 agg, found := aggs.DateHistogram("my_date_histo")
416 if !found {
417 t.Fatal("expected sum_monthly_sales aggregation")
418 }
419 if agg == nil {
420 t.Fatal("expected sum_monthly_sales aggregation")
421 }
422 if got, want := len(agg.Buckets), 6; got != want {
423 t.Fatalf("expected %d buckets; got: %d", want, got)
424 }
425
426 d, found := agg.Buckets[0].MovAvg("the_movavg")
427 if found {
428 t.Fatal("expected no the_movavg aggregation")
429 }
430 if d != nil {
431 t.Fatal("expected no the_movavg aggregation")
432 }
433
434 d, found = agg.Buckets[1].MovAvg("the_movavg")
435 if found {
436 t.Fatal("expected no the_movavg aggregation")
437 }
438 if d != nil {
439 t.Fatal("expected no the_movavg aggregation")
440 }
441
442 d, found = agg.Buckets[2].MovAvg("the_movavg")
443 if !found {
444 t.Fatal("expected the_movavg aggregation")
445 }
446 if d == nil {
447 t.Fatal("expected the_movavg aggregation")
448 }
449 if d.Value == nil {
450 t.Fatal("expected the_movavg value")
451 }
452 if got, want := *d.Value, float64(1290.0); got != want {
453 t.Fatalf("expected %v buckets; got: %v", want, got)
454 }
455
456 d, found = agg.Buckets[3].MovAvg("the_movavg")
457 if !found {
458 t.Fatal("expected the_movavg aggregation")
459 }
460 if d == nil {
461 t.Fatal("expected the_movavg aggregation")
462 }
463 if d.Value == nil {
464 t.Fatal("expected the_movavg value")
465 }
466 if got, want := *d.Value, float64(695.0); got != want {
467 t.Fatalf("expected %v buckets; got: %v", want, got)
468 }
469
470 d, found = agg.Buckets[4].MovAvg("the_movavg")
471 if !found {
472 t.Fatal("expected the_movavg aggregation")
473 }
474 if d == nil {
475 t.Fatal("expected the_movavg aggregation")
476 }
477 if d.Value == nil {
478 t.Fatal("expected the_movavg value")
479 }
480 if got, want := *d.Value, float64(1279.3333333333333); got != want {
481 t.Fatalf("expected %v buckets; got: %v", want, got)
482 }
483
484 d, found = agg.Buckets[5].MovAvg("the_movavg")
485 if !found {
486 t.Fatal("expected the_movavg aggregation")
487 }
488 if d == nil {
489 t.Fatal("expected the_movavg aggregation")
490 }
491 if d.Value == nil {
492 t.Fatal("expected the_movavg value")
493 }
494 if got, want := *d.Value, float64(1157.0); got != want {
495 t.Fatalf("expected %v buckets; got: %v", want, got)
496 }
497 }
498
499 func TestAggsIntegrationCumulativeSum(t *testing.T) {
500 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
501 client := setupTestClientAndCreateIndexAndAddDocs(t)
502
503 esversion, err := client.ElasticsearchVersion(DefaultURL)
504 if err != nil {
505 t.Fatal(err)
506 }
507
508 if esversion < "2.0" {
509 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
510 return
511 }
512
513 // Match all should return all documents
514 builder := client.Search().
515 Index(testIndexName).
516 Type("order").
517 Query(NewMatchAllQuery()).
518 Pretty(true)
519 h := NewDateHistogramAggregation().Field("time").Interval("month")
520 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
521 h = h.SubAggregation("cumulative_sales", NewCumulativeSumAggregation().BucketsPath("sales"))
522 builder = builder.Aggregation("sales_per_month", h)
523
524 res, err := builder.Do(context.TODO())
525 if err != nil {
526 t.Fatal(err)
527 }
528 if res.Hits == nil {
529 t.Errorf("expected Hits != nil; got: nil")
530 }
531
532 aggs := res.Aggregations
533 if aggs == nil {
534 t.Fatal("expected aggregations != nil; got: nil")
535 }
536
537 agg, found := aggs.DateHistogram("sales_per_month")
538 if !found {
539 t.Fatal("expected sales_per_month aggregation")
540 }
541 if agg == nil {
542 t.Fatal("expected sales_per_month aggregation")
543 }
544 if got, want := len(agg.Buckets), 6; got != want {
545 t.Fatalf("expected %d buckets; got: %d", want, got)
546 }
547
548 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
549 t.Fatalf("expected DocCount=%d; got: %d", want, got)
550 }
551 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {
552 t.Fatalf("expected DocCount=%d; got: %d", want, got)
553 }
554 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {
555 t.Fatalf("expected DocCount=%d; got: %d", want, got)
556 }
557 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {
558 t.Fatalf("expected DocCount=%d; got: %d", want, got)
559 }
560 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {
561 t.Fatalf("expected DocCount=%d; got: %d", want, got)
562 }
563 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {
564 t.Fatalf("expected DocCount=%d; got: %d", want, got)
565 }
566
567 d, found := agg.Buckets[0].CumulativeSum("cumulative_sales")
568 if !found {
569 t.Fatal("expected cumulative_sales aggregation")
570 }
571 if d == nil {
572 t.Fatal("expected cumulative_sales aggregation")
573 }
574 if d.Value == nil {
575 t.Fatal("expected cumulative_sales value != nil")
576 }
577 if got, want := *d.Value, float64(1290.0); got != want {
578 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
579 }
580
581 d, found = agg.Buckets[1].CumulativeSum("cumulative_sales")
582 if !found {
583 t.Fatal("expected cumulative_sales aggregation")
584 }
585 if d == nil {
586 t.Fatal("expected cumulative_sales aggregation")
587 }
588 if d.Value == nil {
589 t.Fatal("expected cumulative_sales value != nil")
590 }
591 if got, want := *d.Value, float64(1290.0); got != want {
592 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
593 }
594
595 d, found = agg.Buckets[2].CumulativeSum("cumulative_sales")
596 if !found {
597 t.Fatal("expected cumulative_sales aggregation")
598 }
599 if d == nil {
600 t.Fatal("expected cumulative_sales aggregation")
601 }
602 if d.Value == nil {
603 t.Fatal("expected cumulative_sales value != nil")
604 }
605 if got, want := *d.Value, float64(1390.0); got != want {
606 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
607 }
608
609 d, found = agg.Buckets[3].CumulativeSum("cumulative_sales")
610 if !found {
611 t.Fatal("expected cumulative_sales aggregation")
612 }
613 if d == nil {
614 t.Fatal("expected cumulative_sales aggregation")
615 }
616 if d.Value == nil {
617 t.Fatal("expected cumulative_sales value != nil")
618 }
619 if got, want := *d.Value, float64(3838.0); got != want {
620 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
621 }
622
623 d, found = agg.Buckets[4].CumulativeSum("cumulative_sales")
624 if !found {
625 t.Fatal("expected cumulative_sales aggregation")
626 }
627 if d == nil {
628 t.Fatal("expected cumulative_sales aggregation")
629 }
630 if d.Value == nil {
631 t.Fatal("expected cumulative_sales value != nil")
632 }
633 if got, want := *d.Value, float64(4628.0); got != want {
634 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
635 }
636
637 d, found = agg.Buckets[5].CumulativeSum("cumulative_sales")
638 if !found {
639 t.Fatal("expected cumulative_sales aggregation")
640 }
641 if d == nil {
642 t.Fatal("expected cumulative_sales aggregation")
643 }
644 if d.Value == nil {
645 t.Fatal("expected cumulative_sales value != nil")
646 }
647 if got, want := *d.Value, float64(4696.0); got != want {
648 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
649 }
650 }
651
652 func TestAggsIntegrationBucketScript(t *testing.T) {
653 client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
654
655 esversion, err := client.ElasticsearchVersion(DefaultURL)
656 if err != nil {
657 t.Fatal(err)
658 }
659
660 if esversion < "2.0" {
661 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
662 return
663 }
664
665 // Match all should return all documents
666 builder := client.Search().
667 Index(testIndexName).
668 Type("order").
669 Query(NewMatchAllQuery()).
670 Pretty(true)
671 h := NewDateHistogramAggregation().Field("time").Interval("month")
672 h = h.SubAggregation("total_sales", NewSumAggregation().Field("price"))
673 appleFilter := NewFilterAggregation().Filter(NewTermQuery("manufacturer", "Apple"))
674 appleFilter = appleFilter.SubAggregation("sales", NewSumAggregation().Field("price"))
675 h = h.SubAggregation("apple_sales", appleFilter)
676 h = h.SubAggregation("apple_percentage",
677 NewBucketScriptAggregation().
678 GapPolicy("insert_zeros").
679 AddBucketsPath("appleSales", "apple_sales>sales").
680 AddBucketsPath("totalSales", "total_sales").
681 Script(NewScript("params.appleSales / params.totalSales * 100")))
682 builder = builder.Aggregation("sales_per_month", h)
683
684 res, err := builder.Pretty(true).Do(context.TODO())
685 if err != nil {
686 t.Fatalf("%v (maybe scripting is disabled?)", err)
687 }
688 if res.Hits == nil {
689 t.Errorf("expected Hits != nil; got: nil")
690 }
691
692 aggs := res.Aggregations
693 if aggs == nil {
694 t.Fatal("expected aggregations != nil; got: nil")
695 }
696
697 agg, found := aggs.DateHistogram("sales_per_month")
698 if !found {
699 t.Fatal("expected sales_per_month aggregation")
700 }
701 if agg == nil {
702 t.Fatal("expected sales_per_month aggregation")
703 }
704 if got, want := len(agg.Buckets), 6; got != want {
705 t.Fatalf("expected %d buckets; got: %d", want, got)
706 }
707
708 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
709 t.Fatalf("expected DocCount=%d; got: %d", want, got)
710 }
711 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {
712 t.Fatalf("expected DocCount=%d; got: %d", want, got)
713 }
714 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {
715 t.Fatalf("expected DocCount=%d; got: %d", want, got)
716 }
717 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {
718 t.Fatalf("expected DocCount=%d; got: %d", want, got)
719 }
720 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {
721 t.Fatalf("expected DocCount=%d; got: %d", want, got)
722 }
723 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {
724 t.Fatalf("expected DocCount=%d; got: %d", want, got)
725 }
726
727 d, found := agg.Buckets[0].BucketScript("apple_percentage")
728 if !found {
729 t.Fatal("expected apple_percentage aggregation")
730 }
731 if d == nil {
732 t.Fatal("expected apple_percentage aggregation")
733 }
734 if d.Value == nil {
735 t.Fatal("expected apple_percentage value != nil")
736 }
737 if got, want := *d.Value, float64(100.0); got != want {
738 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
739 }
740
741 d, found = agg.Buckets[1].BucketScript("apple_percentage")
742 if !found {
743 t.Fatal("expected apple_percentage aggregation")
744 }
745 if d == nil {
746 t.Fatal("expected apple_percentage aggregation")
747 }
748 if d.Value != nil {
749 t.Fatal("expected apple_percentage value == nil")
750 }
751
752 d, found = agg.Buckets[2].BucketScript("apple_percentage")
753 if !found {
754 t.Fatal("expected apple_percentage aggregation")
755 }
756 if d == nil {
757 t.Fatal("expected apple_percentage aggregation")
758 }
759 if d.Value == nil {
760 t.Fatal("expected apple_percentage value != nil")
761 }
762 if got, want := *d.Value, float64(0.0); got != want {
763 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
764 }
765
766 d, found = agg.Buckets[3].BucketScript("apple_percentage")
767 if !found {
768 t.Fatal("expected apple_percentage aggregation")
769 }
770 if d == nil {
771 t.Fatal("expected apple_percentage aggregation")
772 }
773 if d.Value == nil {
774 t.Fatal("expected apple_percentage value != nil")
775 }
776 if got, want := *d.Value, float64(34.64052287581699); got != want {
777 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
778 }
779
780 d, found = agg.Buckets[4].BucketScript("apple_percentage")
781 if !found {
782 t.Fatal("expected apple_percentage aggregation")
783 }
784 if d == nil {
785 t.Fatal("expected apple_percentage aggregation")
786 }
787 if d.Value == nil {
788 t.Fatal("expected apple_percentage value != nil")
789 }
790 if got, want := *d.Value, float64(0.0); got != want {
791 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
792 }
793
794 d, found = agg.Buckets[5].BucketScript("apple_percentage")
795 if !found {
796 t.Fatal("expected apple_percentage aggregation")
797 }
798 if d == nil {
799 t.Fatal("expected apple_percentage aggregation")
800 }
801 if d.Value == nil {
802 t.Fatal("expected apple_percentage value != nil")
803 }
804 if got, want := *d.Value, float64(0.0); got != want {
805 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
806 }
807 }
808
809 func TestAggsIntegrationBucketSelector(t *testing.T) {
810 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
811 client := setupTestClientAndCreateIndexAndAddDocs(t)
812
813 esversion, err := client.ElasticsearchVersion(DefaultURL)
814 if err != nil {
815 t.Fatal(err)
816 }
817
818 if esversion < "2.0" {
819 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
820 return
821 }
822
823 // Match all should return all documents
824 builder := client.Search().
825 Index(testIndexName).
826 Type("order").
827 Query(NewMatchAllQuery()).
828 Pretty(true)
829 h := NewDateHistogramAggregation().Field("time").Interval("month")
830 h = h.SubAggregation("total_sales", NewSumAggregation().Field("price"))
831 h = h.SubAggregation("sales_bucket_filter",
832 NewBucketSelectorAggregation().
833 AddBucketsPath("totalSales", "total_sales").
834 Script(NewScript("params.totalSales <= 100")))
835 builder = builder.Aggregation("sales_per_month", h)
836
837 res, err := builder.Do(context.TODO())
838 if err != nil {
839 t.Fatalf("%v (maybe scripting is disabled?)", err)
840 }
841 if res.Hits == nil {
842 t.Errorf("expected Hits != nil; got: nil")
843 }
844
845 aggs := res.Aggregations
846 if aggs == nil {
847 t.Fatal("expected aggregations != nil; got: nil")
848 }
849
850 agg, found := aggs.DateHistogram("sales_per_month")
851 if !found {
852 t.Fatal("expected sales_per_month aggregation")
853 }
854 if agg == nil {
855 t.Fatal("expected sales_per_month aggregation")
856 }
857 if got, want := len(agg.Buckets), 2; got != want {
858 t.Fatalf("expected %d buckets; got: %d", want, got)
859 }
860
861 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
862 t.Fatalf("expected DocCount=%d; got: %d", want, got)
863 }
864 if got, want := agg.Buckets[1].DocCount, int64(2); got != want {
865 t.Fatalf("expected DocCount=%d; got: %d", want, got)
866 }
867 }
868
869 func TestAggsIntegrationSerialDiff(t *testing.T) {
870 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
871 client := setupTestClientAndCreateIndexAndAddDocs(t)
872
873 esversion, err := client.ElasticsearchVersion(DefaultURL)
874 if err != nil {
875 t.Fatal(err)
876 }
877
878 if esversion < "2.0" {
879 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
880 return
881 }
882
883 // Match all should return all documents
884 builder := client.Search().
885 Index(testIndexName).
886 Type("order").
887 Query(NewMatchAllQuery()).
888 Pretty(true)
889 h := NewDateHistogramAggregation().Field("time").Interval("month")
890 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
891 h = h.SubAggregation("the_diff", NewSerialDiffAggregation().BucketsPath("sales").Lag(1))
892 builder = builder.Aggregation("sales_per_month", h)
893
894 res, err := builder.Do(context.TODO())
895 if err != nil {
896 t.Fatal(err)
897 }
898 if res.Hits == nil {
899 t.Errorf("expected Hits != nil; got: nil")
900 }
901
902 aggs := res.Aggregations
903 if aggs == nil {
904 t.Fatal("expected aggregations != nil; got: nil")
905 }
906
907 agg, found := aggs.DateHistogram("sales_per_month")
908 if !found {
909 t.Fatal("expected sales_per_month aggregation")
910 }
911 if agg == nil {
912 t.Fatal("expected sales_per_month aggregation")
913 }
914 if got, want := len(agg.Buckets), 6; got != want {
915 t.Fatalf("expected %d buckets; got: %d", want, got)
916 }
917
918 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
919 t.Fatalf("expected DocCount=%d; got: %d", want, got)
920 }
921 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {
922 t.Fatalf("expected DocCount=%d; got: %d", want, got)
923 }
924 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {
925 t.Fatalf("expected DocCount=%d; got: %d", want, got)
926 }
927 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {
928 t.Fatalf("expected DocCount=%d; got: %d", want, got)
929 }
930 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {
931 t.Fatalf("expected DocCount=%d; got: %d", want, got)
932 }
933 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {
934 t.Fatalf("expected DocCount=%d; got: %d", want, got)
935 }
936
937 d, found := agg.Buckets[0].SerialDiff("the_diff")
938 if found {
939 t.Fatal("expected no the_diff aggregation")
940 }
941 if d != nil {
942 t.Fatal("expected no the_diff aggregation")
943 }
944
945 d, found = agg.Buckets[1].SerialDiff("the_diff")
946 if found {
947 t.Fatal("expected no the_diff aggregation")
948 }
949 if d != nil {
950 t.Fatal("expected no the_diff aggregation")
951 }
952
953 d, found = agg.Buckets[2].SerialDiff("the_diff")
954 if found {
955 t.Fatal("expected no the_diff aggregation")
956 }
957 if d != nil {
958 t.Fatal("expected no the_diff aggregation")
959 }
960
961 d, found = agg.Buckets[3].SerialDiff("the_diff")
962 if !found {
963 t.Fatal("expected the_diff aggregation")
964 }
965 if d == nil {
966 t.Fatal("expected the_diff aggregation")
967 }
968 if d.Value == nil {
969 t.Fatal("expected the_diff value != nil")
970 }
971 if got, want := *d.Value, float64(2348.0); got != want {
972 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)
973 }
974
975 d, found = agg.Buckets[4].SerialDiff("the_diff")
976 if !found {
977 t.Fatal("expected the_diff aggregation")
978 }
979 if d == nil {
980 t.Fatal("expected the_diff aggregation")
981 }
982 if d.Value == nil {
983 t.Fatal("expected the_diff value != nil")
984 }
985 if got, want := *d.Value, float64(-1658.0); got != want {
986 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)
987 }
988
989 d, found = agg.Buckets[5].SerialDiff("the_diff")
990 if !found {
991 t.Fatal("expected the_diff aggregation")
992 }
993 if d == nil {
994 t.Fatal("expected the_diff aggregation")
995 }
996 if d.Value == nil {
997 t.Fatal("expected the_diff value != nil")
998 }
999 if got, want := *d.Value, float64(-722.0); got != want {
1000 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)
1001 }
1002 }