Test
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_derivative.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/6.2/search-aggregations-pipeline-derivative-aggregation.html
  12. type DerivativeAggregation struct {
  13. format string
  14. gapPolicy string
  15. unit string
  16. meta map[string]interface{}
  17. bucketsPaths []string
  18. }
  19. // NewDerivativeAggregation creates and initializes a new DerivativeAggregation.
  20. func NewDerivativeAggregation() *DerivativeAggregation {
  21. return &DerivativeAggregation{
  22. bucketsPaths: make([]string, 0),
  23. }
  24. }
  25. // Format to use on the output of this aggregation.
  26. func (a *DerivativeAggregation) Format(format string) *DerivativeAggregation {
  27. a.format = format
  28. return a
  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 (a *DerivativeAggregation) GapPolicy(gapPolicy string) *DerivativeAggregation {
  33. a.gapPolicy = gapPolicy
  34. return a
  35. }
  36. // GapInsertZeros inserts zeros for gaps in the series.
  37. func (a *DerivativeAggregation) GapInsertZeros() *DerivativeAggregation {
  38. a.gapPolicy = "insert_zeros"
  39. return a
  40. }
  41. // GapSkip skips gaps in the series.
  42. func (a *DerivativeAggregation) GapSkip() *DerivativeAggregation {
  43. a.gapPolicy = "skip"
  44. return a
  45. }
  46. // Unit sets the unit provided, e.g. "1d" or "1y".
  47. // It is only useful when calculating the derivative using a date_histogram.
  48. func (a *DerivativeAggregation) Unit(unit string) *DerivativeAggregation {
  49. a.unit = unit
  50. return a
  51. }
  52. // Meta sets the meta data to be included in the aggregation response.
  53. func (a *DerivativeAggregation) Meta(metaData map[string]interface{}) *DerivativeAggregation {
  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 *DerivativeAggregation) BucketsPath(bucketsPaths ...string) *DerivativeAggregation {
  59. a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...)
  60. return a
  61. }
  62. // Source returns the a JSON-serializable interface.
  63. func (a *DerivativeAggregation) Source() (interface{}, error) {
  64. source := make(map[string]interface{})
  65. params := make(map[string]interface{})
  66. source["derivative"] = params
  67. if a.format != "" {
  68. params["format"] = a.format
  69. }
  70. if a.gapPolicy != "" {
  71. params["gap_policy"] = a.gapPolicy
  72. }
  73. if a.unit != "" {
  74. params["unit"] = a.unit
  75. }
  76. // Add buckets paths
  77. switch len(a.bucketsPaths) {
  78. case 0:
  79. case 1:
  80. params["buckets_path"] = a.bucketsPaths[0]
  81. default:
  82. params["buckets_path"] = a.bucketsPaths
  83. }
  84. // Add Meta data if available
  85. if len(a.meta) > 0 {
  86. source["meta"] = a.meta
  87. }
  88. return source, nil
  89. }