search_aggs_pipeline_derivative.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // DerivativeAggregation is a parent pipeline aggregation which calculates
  6. // the derivative 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-derivative-aggregation.html
  12. type DerivativeAggregation struct {
  13. format string
  14. gapPolicy string
  15. unit string
  16. subAggregations map[string]Aggregation
  17. meta map[string]interface{}
  18. bucketsPaths []string
  19. }
  20. // NewDerivativeAggregation creates and initializes a new DerivativeAggregation.
  21. func NewDerivativeAggregation() *DerivativeAggregation {
  22. return &DerivativeAggregation{
  23. subAggregations: make(map[string]Aggregation),
  24. bucketsPaths: make([]string, 0),
  25. }
  26. }
  27. func (a *DerivativeAggregation) Format(format string) *DerivativeAggregation {
  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 *DerivativeAggregation) GapPolicy(gapPolicy string) *DerivativeAggregation {
  34. a.gapPolicy = gapPolicy
  35. return a
  36. }
  37. // GapInsertZeros inserts zeros for gaps in the series.
  38. func (a *DerivativeAggregation) GapInsertZeros() *DerivativeAggregation {
  39. a.gapPolicy = "insert_zeros"
  40. return a
  41. }
  42. // GapSkip skips gaps in the series.
  43. func (a *DerivativeAggregation) GapSkip() *DerivativeAggregation {
  44. a.gapPolicy = "skip"
  45. return a
  46. }
  47. // Unit sets the unit provided, e.g. "1d" or "1y".
  48. // It is only useful when calculating the derivative using a date_histogram.
  49. func (a *DerivativeAggregation) Unit(unit string) *DerivativeAggregation {
  50. a.unit = unit
  51. return a
  52. }
  53. // SubAggregation adds a sub-aggregation to this aggregation.
  54. func (a *DerivativeAggregation) SubAggregation(name string, subAggregation Aggregation) *DerivativeAggregation {
  55. a.subAggregations[name] = subAggregation
  56. return a
  57. }
  58. // Meta sets the meta data to be included in the aggregation response.
  59. func (a *DerivativeAggregation) Meta(metaData map[string]interface{}) *DerivativeAggregation {
  60. a.meta = metaData
  61. return a
  62. }
  63. // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
  64. func (a *DerivativeAggregation) BucketsPath(bucketsPaths ...string) *DerivativeAggregation {
  65. a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...)
  66. return a
  67. }
  68. func (a *DerivativeAggregation) Source() (interface{}, error) {
  69. source := make(map[string]interface{})
  70. params := make(map[string]interface{})
  71. source["derivative"] = params
  72. if a.format != "" {
  73. params["format"] = a.format
  74. }
  75. if a.gapPolicy != "" {
  76. params["gap_policy"] = a.gapPolicy
  77. }
  78. if a.unit != "" {
  79. params["unit"] = a.unit
  80. }
  81. // Add buckets paths
  82. switch len(a.bucketsPaths) {
  83. case 0:
  84. case 1:
  85. params["buckets_path"] = a.bucketsPaths[0]
  86. default:
  87. params["buckets_path"] = a.bucketsPaths
  88. }
  89. // AggregationBuilder (SubAggregations)
  90. if len(a.subAggregations) > 0 {
  91. aggsMap := make(map[string]interface{})
  92. source["aggregations"] = aggsMap
  93. for name, aggregate := range a.subAggregations {
  94. src, err := aggregate.Source()
  95. if err != nil {
  96. return nil, err
  97. }
  98. aggsMap[name] = src
  99. }
  100. }
  101. // Add Meta data if available
  102. if len(a.meta) > 0 {
  103. source["meta"] = a.meta
  104. }
  105. return source, nil
  106. }