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_bucket_script.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // BucketScriptAggregation is a parent pipeline aggregation which executes
  6. // a script which can perform per bucket computations on specified metrics
  7. // in the parent multi-bucket aggregation. The specified metric must be
  8. // numeric and the script must return a numeric value.
  9. //
  10. // For more details, see
  11. // https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-bucket-script-aggregation.html
  12. type BucketScriptAggregation struct {
  13. format string
  14. gapPolicy string
  15. script *Script
  16. meta map[string]interface{}
  17. bucketsPathsMap map[string]string
  18. }
  19. // NewBucketScriptAggregation creates and initializes a new BucketScriptAggregation.
  20. func NewBucketScriptAggregation() *BucketScriptAggregation {
  21. return &BucketScriptAggregation{
  22. bucketsPathsMap: make(map[string]string),
  23. }
  24. }
  25. // Format to use on the output of this aggregation.
  26. func (a *BucketScriptAggregation) Format(format string) *BucketScriptAggregation {
  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 *BucketScriptAggregation) GapPolicy(gapPolicy string) *BucketScriptAggregation {
  33. a.gapPolicy = gapPolicy
  34. return a
  35. }
  36. // GapInsertZeros inserts zeros for gaps in the series.
  37. func (a *BucketScriptAggregation) GapInsertZeros() *BucketScriptAggregation {
  38. a.gapPolicy = "insert_zeros"
  39. return a
  40. }
  41. // GapSkip skips gaps in the series.
  42. func (a *BucketScriptAggregation) GapSkip() *BucketScriptAggregation {
  43. a.gapPolicy = "skip"
  44. return a
  45. }
  46. // Script is the script to run.
  47. func (a *BucketScriptAggregation) Script(script *Script) *BucketScriptAggregation {
  48. a.script = script
  49. return a
  50. }
  51. // Meta sets the meta data to be included in the aggregation response.
  52. func (a *BucketScriptAggregation) Meta(metaData map[string]interface{}) *BucketScriptAggregation {
  53. a.meta = metaData
  54. return a
  55. }
  56. // BucketsPathsMap sets the paths to the buckets to use for this pipeline aggregator.
  57. func (a *BucketScriptAggregation) BucketsPathsMap(bucketsPathsMap map[string]string) *BucketScriptAggregation {
  58. a.bucketsPathsMap = bucketsPathsMap
  59. return a
  60. }
  61. // AddBucketsPath adds a bucket path to use for this pipeline aggregator.
  62. func (a *BucketScriptAggregation) AddBucketsPath(name, path string) *BucketScriptAggregation {
  63. if a.bucketsPathsMap == nil {
  64. a.bucketsPathsMap = make(map[string]string)
  65. }
  66. a.bucketsPathsMap[name] = path
  67. return a
  68. }
  69. // Source returns the a JSON-serializable interface.
  70. func (a *BucketScriptAggregation) Source() (interface{}, error) {
  71. source := make(map[string]interface{})
  72. params := make(map[string]interface{})
  73. source["bucket_script"] = params
  74. if a.format != "" {
  75. params["format"] = a.format
  76. }
  77. if a.gapPolicy != "" {
  78. params["gap_policy"] = a.gapPolicy
  79. }
  80. if a.script != nil {
  81. src, err := a.script.Source()
  82. if err != nil {
  83. return nil, err
  84. }
  85. params["script"] = src
  86. }
  87. // Add buckets paths
  88. if len(a.bucketsPathsMap) > 0 {
  89. params["buckets_path"] = a.bucketsPathsMap
  90. }
  91. // Add Meta data if available
  92. if len(a.meta) > 0 {
  93. source["meta"] = a.meta
  94. }
  95. return source, nil
  96. }