search_aggs_pipeline_max_bucket.go 3.3 KB

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