search_aggs_pipeline_bucket_selector.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // BucketSelectorAggregation is a parent pipeline aggregation which
  6. // determines whether the current bucket will be retained in the parent
  7. // multi-bucket aggregation. The specific metric must be numeric and
  8. // the script must return a boolean value. If the script language is
  9. // expression then a numeric return value is permitted. In this case 0.0
  10. // will be evaluated as false and all other values will evaluate to true.
  11. //
  12. // For more details, see
  13. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-bucket-selector-aggregation.html
  14. type BucketSelectorAggregation struct {
  15. format string
  16. gapPolicy string
  17. script *Script
  18. subAggregations map[string]Aggregation
  19. meta map[string]interface{}
  20. bucketsPathsMap map[string]string
  21. }
  22. // NewBucketSelectorAggregation creates and initializes a new BucketSelectorAggregation.
  23. func NewBucketSelectorAggregation() *BucketSelectorAggregation {
  24. return &BucketSelectorAggregation{
  25. subAggregations: make(map[string]Aggregation),
  26. bucketsPathsMap: make(map[string]string),
  27. }
  28. }
  29. func (a *BucketSelectorAggregation) Format(format string) *BucketSelectorAggregation {
  30. a.format = format
  31. return a
  32. }
  33. // GapPolicy defines what should be done when a gap in the series is discovered.
  34. // Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
  35. func (a *BucketSelectorAggregation) GapPolicy(gapPolicy string) *BucketSelectorAggregation {
  36. a.gapPolicy = gapPolicy
  37. return a
  38. }
  39. // GapInsertZeros inserts zeros for gaps in the series.
  40. func (a *BucketSelectorAggregation) GapInsertZeros() *BucketSelectorAggregation {
  41. a.gapPolicy = "insert_zeros"
  42. return a
  43. }
  44. // GapSkip skips gaps in the series.
  45. func (a *BucketSelectorAggregation) GapSkip() *BucketSelectorAggregation {
  46. a.gapPolicy = "skip"
  47. return a
  48. }
  49. // Script is the script to run.
  50. func (a *BucketSelectorAggregation) Script(script *Script) *BucketSelectorAggregation {
  51. a.script = script
  52. return a
  53. }
  54. // SubAggregation adds a sub-aggregation to this aggregation.
  55. func (a *BucketSelectorAggregation) SubAggregation(name string, subAggregation Aggregation) *BucketSelectorAggregation {
  56. a.subAggregations[name] = subAggregation
  57. return a
  58. }
  59. // Meta sets the meta data to be included in the aggregation response.
  60. func (a *BucketSelectorAggregation) Meta(metaData map[string]interface{}) *BucketSelectorAggregation {
  61. a.meta = metaData
  62. return a
  63. }
  64. // BucketsPathsMap sets the paths to the buckets to use for this pipeline aggregator.
  65. func (a *BucketSelectorAggregation) BucketsPathsMap(bucketsPathsMap map[string]string) *BucketSelectorAggregation {
  66. a.bucketsPathsMap = bucketsPathsMap
  67. return a
  68. }
  69. // AddBucketsPath adds a bucket path to use for this pipeline aggregator.
  70. func (a *BucketSelectorAggregation) AddBucketsPath(name, path string) *BucketSelectorAggregation {
  71. if a.bucketsPathsMap == nil {
  72. a.bucketsPathsMap = make(map[string]string)
  73. }
  74. a.bucketsPathsMap[name] = path
  75. return a
  76. }
  77. func (a *BucketSelectorAggregation) Source() (interface{}, error) {
  78. source := make(map[string]interface{})
  79. params := make(map[string]interface{})
  80. source["bucket_selector"] = params
  81. if a.format != "" {
  82. params["format"] = a.format
  83. }
  84. if a.gapPolicy != "" {
  85. params["gap_policy"] = a.gapPolicy
  86. }
  87. if a.script != nil {
  88. src, err := a.script.Source()
  89. if err != nil {
  90. return nil, err
  91. }
  92. params["script"] = src
  93. }
  94. // Add buckets paths
  95. if len(a.bucketsPathsMap) > 0 {
  96. params["buckets_path"] = a.bucketsPathsMap
  97. }
  98. // AggregationBuilder (SubAggregations)
  99. if len(a.subAggregations) > 0 {
  100. aggsMap := make(map[string]interface{})
  101. source["aggregations"] = aggsMap
  102. for name, aggregate := range a.subAggregations {
  103. src, err := aggregate.Source()
  104. if err != nil {
  105. return nil, err
  106. }
  107. aggsMap[name] = src
  108. }
  109. }
  110. // Add Meta data if available
  111. if len(a.meta) > 0 {
  112. source["meta"] = a.meta
  113. }
  114. return source, nil
  115. }