search_aggs_pipeline_cumulative_sum.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // CumulativeSumAggregation is a parent pipeline aggregation which calculates
  6. // the cumulative sum of a specified metric in a parent histogram (or date_histogram)
  7. // aggregation. The specified metric must be numeric and the enclosing
  8. // histogram must have min_doc_count set to 0 (default for histogram aggregations).
  9. //
  10. // For more details, see
  11. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-cumulative-sum-aggregation.html
  12. type CumulativeSumAggregation struct {
  13. format string
  14. subAggregations map[string]Aggregation
  15. meta map[string]interface{}
  16. bucketsPaths []string
  17. }
  18. // NewCumulativeSumAggregation creates and initializes a new CumulativeSumAggregation.
  19. func NewCumulativeSumAggregation() *CumulativeSumAggregation {
  20. return &CumulativeSumAggregation{
  21. subAggregations: make(map[string]Aggregation),
  22. bucketsPaths: make([]string, 0),
  23. }
  24. }
  25. func (a *CumulativeSumAggregation) Format(format string) *CumulativeSumAggregation {
  26. a.format = format
  27. return a
  28. }
  29. // SubAggregation adds a sub-aggregation to this aggregation.
  30. func (a *CumulativeSumAggregation) SubAggregation(name string, subAggregation Aggregation) *CumulativeSumAggregation {
  31. a.subAggregations[name] = subAggregation
  32. return a
  33. }
  34. // Meta sets the meta data to be included in the aggregation response.
  35. func (a *CumulativeSumAggregation) Meta(metaData map[string]interface{}) *CumulativeSumAggregation {
  36. a.meta = metaData
  37. return a
  38. }
  39. // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
  40. func (a *CumulativeSumAggregation) BucketsPath(bucketsPaths ...string) *CumulativeSumAggregation {
  41. a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...)
  42. return a
  43. }
  44. func (a *CumulativeSumAggregation) Source() (interface{}, error) {
  45. source := make(map[string]interface{})
  46. params := make(map[string]interface{})
  47. source["cumulative_sum"] = params
  48. if a.format != "" {
  49. params["format"] = a.format
  50. }
  51. // Add buckets paths
  52. switch len(a.bucketsPaths) {
  53. case 0:
  54. case 1:
  55. params["buckets_path"] = a.bucketsPaths[0]
  56. default:
  57. params["buckets_path"] = a.bucketsPaths
  58. }
  59. // AggregationBuilder (SubAggregations)
  60. if len(a.subAggregations) > 0 {
  61. aggsMap := make(map[string]interface{})
  62. source["aggregations"] = aggsMap
  63. for name, aggregate := range a.subAggregations {
  64. src, err := aggregate.Source()
  65. if err != nil {
  66. return nil, err
  67. }
  68. aggsMap[name] = src
  69. }
  70. }
  71. // Add Meta data if available
  72. if len(a.meta) > 0 {
  73. source["meta"] = a.meta
  74. }
  75. return source, nil
  76. }