search_aggs_pipeline_bucket_script.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/5.2/search-aggregations-pipeline-bucket-script-aggregation.html
  12. type BucketScriptAggregation struct {
  13. format string
  14. gapPolicy string
  15. script *Script
  16. subAggregations map[string]Aggregation
  17. meta map[string]interface{}
  18. bucketsPathsMap map[string]string
  19. }
  20. // NewBucketScriptAggregation creates and initializes a new BucketScriptAggregation.
  21. func NewBucketScriptAggregation() *BucketScriptAggregation {
  22. return &BucketScriptAggregation{
  23. subAggregations: make(map[string]Aggregation),
  24. bucketsPathsMap: make(map[string]string),
  25. }
  26. }
  27. func (a *BucketScriptAggregation) Format(format string) *BucketScriptAggregation {
  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 *BucketScriptAggregation) GapPolicy(gapPolicy string) *BucketScriptAggregation {
  34. a.gapPolicy = gapPolicy
  35. return a
  36. }
  37. // GapInsertZeros inserts zeros for gaps in the series.
  38. func (a *BucketScriptAggregation) GapInsertZeros() *BucketScriptAggregation {
  39. a.gapPolicy = "insert_zeros"
  40. return a
  41. }
  42. // GapSkip skips gaps in the series.
  43. func (a *BucketScriptAggregation) GapSkip() *BucketScriptAggregation {
  44. a.gapPolicy = "skip"
  45. return a
  46. }
  47. // Script is the script to run.
  48. func (a *BucketScriptAggregation) Script(script *Script) *BucketScriptAggregation {
  49. a.script = script
  50. return a
  51. }
  52. // SubAggregation adds a sub-aggregation to this aggregation.
  53. func (a *BucketScriptAggregation) SubAggregation(name string, subAggregation Aggregation) *BucketScriptAggregation {
  54. a.subAggregations[name] = subAggregation
  55. return a
  56. }
  57. // Meta sets the meta data to be included in the aggregation response.
  58. func (a *BucketScriptAggregation) Meta(metaData map[string]interface{}) *BucketScriptAggregation {
  59. a.meta = metaData
  60. return a
  61. }
  62. // BucketsPathsMap sets the paths to the buckets to use for this pipeline aggregator.
  63. func (a *BucketScriptAggregation) BucketsPathsMap(bucketsPathsMap map[string]string) *BucketScriptAggregation {
  64. a.bucketsPathsMap = bucketsPathsMap
  65. return a
  66. }
  67. // AddBucketsPath adds a bucket path to use for this pipeline aggregator.
  68. func (a *BucketScriptAggregation) AddBucketsPath(name, path string) *BucketScriptAggregation {
  69. if a.bucketsPathsMap == nil {
  70. a.bucketsPathsMap = make(map[string]string)
  71. }
  72. a.bucketsPathsMap[name] = path
  73. return a
  74. }
  75. func (a *BucketScriptAggregation) Source() (interface{}, error) {
  76. source := make(map[string]interface{})
  77. params := make(map[string]interface{})
  78. source["bucket_script"] = params
  79. if a.format != "" {
  80. params["format"] = a.format
  81. }
  82. if a.gapPolicy != "" {
  83. params["gap_policy"] = a.gapPolicy
  84. }
  85. if a.script != nil {
  86. src, err := a.script.Source()
  87. if err != nil {
  88. return nil, err
  89. }
  90. params["script"] = src
  91. }
  92. // Add buckets paths
  93. if len(a.bucketsPathsMap) > 0 {
  94. params["buckets_path"] = a.bucketsPathsMap
  95. }
  96. // AggregationBuilder (SubAggregations)
  97. if len(a.subAggregations) > 0 {
  98. aggsMap := make(map[string]interface{})
  99. source["aggregations"] = aggsMap
  100. for name, aggregate := range a.subAggregations {
  101. src, err := aggregate.Source()
  102. if err != nil {
  103. return nil, err
  104. }
  105. aggsMap[name] = src
  106. }
  107. }
  108. // Add Meta data if available
  109. if len(a.meta) > 0 {
  110. source["meta"] = a.meta
  111. }
  112. return source, nil
  113. }