You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_aggs_pipeline_sum_bucket.go 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // SumBucketAggregation is a sibling pipeline aggregation which calculates
  6. // the sum across all buckets 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/7.0/search-aggregations-pipeline-sum-bucket-aggregation.html
  12. type SumBucketAggregation struct {
  13. format string
  14. gapPolicy string
  15. meta map[string]interface{}
  16. bucketsPaths []string
  17. }
  18. // NewSumBucketAggregation creates and initializes a new SumBucketAggregation.
  19. func NewSumBucketAggregation() *SumBucketAggregation {
  20. return &SumBucketAggregation{
  21. bucketsPaths: make([]string, 0),
  22. }
  23. }
  24. // Format to use on the output of this aggregation.
  25. func (a *SumBucketAggregation) Format(format string) *SumBucketAggregation {
  26. a.format = format
  27. return a
  28. }
  29. // GapPolicy defines what should be done when a gap in the series is discovered.
  30. // Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
  31. func (a *SumBucketAggregation) GapPolicy(gapPolicy string) *SumBucketAggregation {
  32. a.gapPolicy = gapPolicy
  33. return a
  34. }
  35. // GapInsertZeros inserts zeros for gaps in the series.
  36. func (a *SumBucketAggregation) GapInsertZeros() *SumBucketAggregation {
  37. a.gapPolicy = "insert_zeros"
  38. return a
  39. }
  40. // GapSkip skips gaps in the series.
  41. func (a *SumBucketAggregation) GapSkip() *SumBucketAggregation {
  42. a.gapPolicy = "skip"
  43. return a
  44. }
  45. // Meta sets the meta data to be included in the aggregation response.
  46. func (a *SumBucketAggregation) Meta(metaData map[string]interface{}) *SumBucketAggregation {
  47. a.meta = metaData
  48. return a
  49. }
  50. // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
  51. func (a *SumBucketAggregation) BucketsPath(bucketsPaths ...string) *SumBucketAggregation {
  52. a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...)
  53. return a
  54. }
  55. // Source returns the a JSON-serializable interface.
  56. func (a *SumBucketAggregation) Source() (interface{}, error) {
  57. source := make(map[string]interface{})
  58. params := make(map[string]interface{})
  59. source["sum_bucket"] = params
  60. if a.format != "" {
  61. params["format"] = a.format
  62. }
  63. if a.gapPolicy != "" {
  64. params["gap_policy"] = a.gapPolicy
  65. }
  66. // Add buckets paths
  67. switch len(a.bucketsPaths) {
  68. case 0:
  69. case 1:
  70. params["buckets_path"] = a.bucketsPaths[0]
  71. default:
  72. params["buckets_path"] = a.bucketsPaths
  73. }
  74. // Add Meta data if available
  75. if len(a.meta) > 0 {
  76. source["meta"] = a.meta
  77. }
  78. return source, nil
  79. }