search_aggs_pipeline_stats_bucket.go 3.3 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. // StatsBucketAggregation is a sibling pipeline aggregation which calculates
  6. // a variety of stats across all bucket 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-stats-bucket-aggregation.html
  12. type StatsBucketAggregation struct {
  13. format string
  14. gapPolicy string
  15. subAggregations map[string]Aggregation
  16. meta map[string]interface{}
  17. bucketsPaths []string
  18. }
  19. // NewStatsBucketAggregation creates and initializes a new StatsBucketAggregation.
  20. func NewStatsBucketAggregation() *StatsBucketAggregation {
  21. return &StatsBucketAggregation{
  22. subAggregations: make(map[string]Aggregation),
  23. bucketsPaths: make([]string, 0),
  24. }
  25. }
  26. func (s *StatsBucketAggregation) Format(format string) *StatsBucketAggregation {
  27. s.format = format
  28. return s
  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 (s *StatsBucketAggregation) GapPolicy(gapPolicy string) *StatsBucketAggregation {
  33. s.gapPolicy = gapPolicy
  34. return s
  35. }
  36. // GapInsertZeros inserts zeros for gaps in the series.
  37. func (s *StatsBucketAggregation) GapInsertZeros() *StatsBucketAggregation {
  38. s.gapPolicy = "insert_zeros"
  39. return s
  40. }
  41. // GapSkip skips gaps in the series.
  42. func (s *StatsBucketAggregation) GapSkip() *StatsBucketAggregation {
  43. s.gapPolicy = "skip"
  44. return s
  45. }
  46. // SubAggregation adds a sub-aggregation to this aggregation.
  47. func (s *StatsBucketAggregation) SubAggregation(name string, subAggregation Aggregation) *StatsBucketAggregation {
  48. s.subAggregations[name] = subAggregation
  49. return s
  50. }
  51. // Meta sets the meta data to be included in the aggregation response.
  52. func (s *StatsBucketAggregation) Meta(metaData map[string]interface{}) *StatsBucketAggregation {
  53. s.meta = metaData
  54. return s
  55. }
  56. // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
  57. func (s *StatsBucketAggregation) BucketsPath(bucketsPaths ...string) *StatsBucketAggregation {
  58. s.bucketsPaths = append(s.bucketsPaths, bucketsPaths...)
  59. return s
  60. }
  61. func (s *StatsBucketAggregation) Source() (interface{}, error) {
  62. source := make(map[string]interface{})
  63. params := make(map[string]interface{})
  64. source["stats_bucket"] = params
  65. if s.format != "" {
  66. params["format"] = s.format
  67. }
  68. if s.gapPolicy != "" {
  69. params["gap_policy"] = s.gapPolicy
  70. }
  71. // Add buckets paths
  72. switch len(s.bucketsPaths) {
  73. case 0:
  74. case 1:
  75. params["buckets_path"] = s.bucketsPaths[0]
  76. default:
  77. params["buckets_path"] = s.bucketsPaths
  78. }
  79. // AggregationBuilder (SubAggregations)
  80. if len(s.subAggregations) > 0 {
  81. aggsMap := make(map[string]interface{})
  82. source["aggregations"] = aggsMap
  83. for name, aggregate := range s.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(s.meta) > 0 {
  93. source["meta"] = s.meta
  94. }
  95. return source, nil
  96. }