|
| 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 | +// ExtendedStatsBucketAggregation is a sibling pipeline aggregation which calculates |
| 8 | +// a variety of stats across all bucket of a specified metric in a sibling aggregation. |
| 9 | +// The specified metric must be numeric and the sibling aggregation must |
| 10 | +// be a multi-bucket aggregation. |
| 11 | +// |
| 12 | +// This aggregation provides a few more statistics (sum of squares, standard deviation, etc) |
| 13 | +// compared to the stats_bucket aggregation. |
| 14 | +// For more details, see |
| 15 | +// https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-pipeline-extended-stats-bucket-aggregation.html |
| 16 | +type ExtendedStatsBucketAggregation struct { |
| 17 | + format string |
| 18 | + gapPolicy string |
| 19 | + sigma *float32 |
| 20 | + meta map[string]interface{} |
| 21 | + bucketsPaths []string |
| 22 | +} |
| 23 | + |
| 24 | +// NewExtendedStatsBucketAggregation creates and initializes a new ExtendedStatsBucketAggregation. |
| 25 | +func NewExtendedStatsBucketAggregation() *ExtendedStatsBucketAggregation { |
| 26 | + return &ExtendedStatsBucketAggregation{ |
| 27 | + bucketsPaths: make([]string, 0), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Format to use on the output of this aggregation. |
| 32 | +func (s *ExtendedStatsBucketAggregation) Format(format string) *ExtendedStatsBucketAggregation { |
| 33 | + s.format = format |
| 34 | + return s |
| 35 | +} |
| 36 | + |
| 37 | +// GapPolicy defines what should be done when a gap in the series is discovered. |
| 38 | +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". |
| 39 | +func (s *ExtendedStatsBucketAggregation) GapPolicy(gapPolicy string) *ExtendedStatsBucketAggregation { |
| 40 | + s.gapPolicy = gapPolicy |
| 41 | + return s |
| 42 | +} |
| 43 | + |
| 44 | +// GapInsertZeros inserts zeros for gaps in the series. |
| 45 | +func (s *ExtendedStatsBucketAggregation) GapInsertZeros() *ExtendedStatsBucketAggregation { |
| 46 | + s.gapPolicy = "insert_zeros" |
| 47 | + return s |
| 48 | +} |
| 49 | + |
| 50 | +// GapSkip skips gaps in the series. |
| 51 | +func (s *ExtendedStatsBucketAggregation) GapSkip() *ExtendedStatsBucketAggregation { |
| 52 | + s.gapPolicy = "skip" |
| 53 | + return s |
| 54 | +} |
| 55 | + |
| 56 | +// Meta sets the meta data to be included in the aggregation response. |
| 57 | +func (s *ExtendedStatsBucketAggregation) Meta(metaData map[string]interface{}) *ExtendedStatsBucketAggregation { |
| 58 | + s.meta = metaData |
| 59 | + return s |
| 60 | +} |
| 61 | + |
| 62 | +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. |
| 63 | +func (s *ExtendedStatsBucketAggregation) BucketsPath(bucketsPaths ...string) *ExtendedStatsBucketAggregation { |
| 64 | + s.bucketsPaths = append(s.bucketsPaths, bucketsPaths...) |
| 65 | + return s |
| 66 | +} |
| 67 | + |
| 68 | +// Sigma sets number of standard deviations above/below the mean to display |
| 69 | +func (s *ExtendedStatsBucketAggregation) Sigma(sigma float32) *ExtendedStatsBucketAggregation { |
| 70 | + s.sigma = &sigma |
| 71 | + return s |
| 72 | +} |
| 73 | + |
| 74 | +// Source returns the a JSON-serializable interface. |
| 75 | +func (s *ExtendedStatsBucketAggregation) Source() (interface{}, error) { |
| 76 | + source := make(map[string]interface{}) |
| 77 | + params := make(map[string]interface{}) |
| 78 | + source["extended_stats_bucket"] = params |
| 79 | + |
| 80 | + if s.format != "" { |
| 81 | + params["format"] = s.format |
| 82 | + } |
| 83 | + if s.gapPolicy != "" { |
| 84 | + params["gap_policy"] = s.gapPolicy |
| 85 | + } |
| 86 | + |
| 87 | + // Add buckets paths |
| 88 | + switch len(s.bucketsPaths) { |
| 89 | + case 0: |
| 90 | + case 1: |
| 91 | + params["buckets_path"] = s.bucketsPaths[0] |
| 92 | + default: |
| 93 | + params["buckets_path"] = s.bucketsPaths |
| 94 | + } |
| 95 | + |
| 96 | + // Add sigma is not zero or less |
| 97 | + if s.sigma != nil && *s.sigma >= 0 { |
| 98 | + params["sigma"] = *s.sigma |
| 99 | + } |
| 100 | + |
| 101 | + // Add Meta data if available |
| 102 | + if len(s.meta) > 0 { |
| 103 | + source["meta"] = s.meta |
| 104 | + } |
| 105 | + |
| 106 | + return source, nil |
| 107 | +} |
0 commit comments