search_aggs_pipeline_avg_bucket.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. package elastic
  5. // AvgBucketAggregation is a sibling pipeline aggregation which calculates
  6. // the (mean) average value of a specified metric in a sibling aggregation.
  7. // The specified metric must be numeric and the sibling aggregation must
  8. // be a multi-bucket aggregation.
  9. //
  10. // For more details, see
  11. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-avg-bucket-aggregation.html
  12. type AvgBucketAggregation struct {
  13. format string
  14. gapPolicy string
  15. subAggregations map[string]Aggregation
  16. meta map[string]interface{}
  17. bucketsPaths []string
  18. }
  19. // NewAvgBucketAggregation creates and initializes a new AvgBucketAggregation.
  20. func NewAvgBucketAggregation() *AvgBucketAggregation {
  21. return &AvgBucketAggregation{
  22. subAggregations: make(map[string]Aggregation),
  23. bucketsPaths: make([]string, 0),
  24. }
  25. }
  26. func (a *AvgBucketAggregation) Format(format string) *AvgBucketAggregation {
  27. a.format = format
  28. return a
  29. }
  30. // GapPolicy defines what should be done when a gap in the series is discovered.
  31. // Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
  32. func (a *AvgBucketAggregation) GapPolicy(gapPolicy string) *AvgBucketAggregation {
  33. a.gapPolicy = gapPolicy
  34. return a
  35. }
  36. // GapInsertZeros inserts zeros for gaps in the series.
  37. func (a *AvgBucketAggregation) GapInsertZeros() *AvgBucketAggregation {
  38. a.gapPolicy = "insert_zeros"
  39. return a
  40. }
  41. // GapSkip skips gaps in the series.
  42. func (a *AvgBucketAggregation) GapSkip() *AvgBucketAggregation {
  43. a.gapPolicy = "skip"
  44. return a
  45. }
  46. // SubAggregation adds a sub-aggregation to this aggregation.
  47. func (a *AvgBucketAggregation) SubAggregation(name string, subAggregation Aggregation) *AvgBucketAggregation {
  48. a.subAggregations[name] = subAggregation
  49. return a
  50. }
  51. // Meta sets the meta data to be included in the aggregation response.
  52. func (a *AvgBucketAggregation) Meta(metaData map[string]interface{}) *AvgBucketAggregation {
  53. a.meta = metaData
  54. return a
  55. }
  56. // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
  57. func (a *AvgBucketAggregation) BucketsPath(bucketsPaths ...string) *AvgBucketAggregation {
  58. a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...)
  59. return a
  60. }
  61. func (a *AvgBucketAggregation) Source() (interface{}, error) {
  62. source := make(map[string]interface{})
  63. params := make(map[string]interface{})
  64. source["avg_bucket"] = params
  65. if a.format != "" {
  66. params["format"] = a.format
  67. }
  68. if a.gapPolicy != "" {
  69. params["gap_policy"] = a.gapPolicy
  70. }
  71. // Add buckets paths
  72. switch len(a.bucketsPaths) {
  73. case 0:
  74. case 1:
  75. params["buckets_path"] = a.bucketsPaths[0]
  76. default:
  77. params["buckets_path"] = a.bucketsPaths
  78. }
  79. // AggregationBuilder (SubAggregations)
  80. if len(a.subAggregations) > 0 {
  81. aggsMap := make(map[string]interface{})
  82. source["aggregations"] = aggsMap
  83. for name, aggregate := range a.subAggregations {
  84. src, err := aggregate.Source()
  85. if err != nil {
  86. return nil, err
  87. }
  88. aggsMap[name] = src
  89. }
  90. }
  91. // Add Meta data if available
  92. if len(a.meta) > 0 {
  93. source["meta"] = a.meta
  94. }
  95. return source, nil
  96. }