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_percentiles_bucket.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2012-2015 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. // PercentilesBucketAggregation is a sibling pipeline aggregation which calculates
  6. // percentiles across all bucket of a specified metric in a sibling aggregation.
  7. // The specified metric must be numeric and the sibling aggregation must
  8. // be a multi-bucket aggregation.
  9. //
  10. // For more details, see
  11. // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-percentiles-bucket-aggregation.html
  12. type PercentilesBucketAggregation struct {
  13. format string
  14. gapPolicy string
  15. percents []float64
  16. bucketsPaths []string
  17. subAggregations map[string]Aggregation
  18. meta map[string]interface{}
  19. }
  20. // NewPercentilesBucketAggregation creates and initializes a new PercentilesBucketAggregation.
  21. func NewPercentilesBucketAggregation() *PercentilesBucketAggregation {
  22. return &PercentilesBucketAggregation{
  23. subAggregations: make(map[string]Aggregation),
  24. }
  25. }
  26. // Format to apply the output value of this aggregation.
  27. func (p *PercentilesBucketAggregation) Format(format string) *PercentilesBucketAggregation {
  28. p.format = format
  29. return p
  30. }
  31. // Percents to calculate percentiles for in this aggregation.
  32. func (p *PercentilesBucketAggregation) Percents(percents ...float64) *PercentilesBucketAggregation {
  33. p.percents = percents
  34. return p
  35. }
  36. // GapPolicy defines what should be done when a gap in the series is discovered.
  37. // Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
  38. func (p *PercentilesBucketAggregation) GapPolicy(gapPolicy string) *PercentilesBucketAggregation {
  39. p.gapPolicy = gapPolicy
  40. return p
  41. }
  42. // GapInsertZeros inserts zeros for gaps in the series.
  43. func (p *PercentilesBucketAggregation) GapInsertZeros() *PercentilesBucketAggregation {
  44. p.gapPolicy = "insert_zeros"
  45. return p
  46. }
  47. // GapSkip skips gaps in the series.
  48. func (p *PercentilesBucketAggregation) GapSkip() *PercentilesBucketAggregation {
  49. p.gapPolicy = "skip"
  50. return p
  51. }
  52. // SubAggregation adds a sub-aggregation to this aggregation.
  53. func (p *PercentilesBucketAggregation) SubAggregation(name string, subAggregation Aggregation) *PercentilesBucketAggregation {
  54. p.subAggregations[name] = subAggregation
  55. return p
  56. }
  57. // Meta sets the meta data to be included in the aggregation response.
  58. func (p *PercentilesBucketAggregation) Meta(metaData map[string]interface{}) *PercentilesBucketAggregation {
  59. p.meta = metaData
  60. return p
  61. }
  62. // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
  63. func (p *PercentilesBucketAggregation) BucketsPath(bucketsPaths ...string) *PercentilesBucketAggregation {
  64. p.bucketsPaths = append(p.bucketsPaths, bucketsPaths...)
  65. return p
  66. }
  67. func (p *PercentilesBucketAggregation) Source() (interface{}, error) {
  68. source := make(map[string]interface{})
  69. params := make(map[string]interface{})
  70. source["percentiles_bucket"] = params
  71. if p.format != "" {
  72. params["format"] = p.format
  73. }
  74. if p.gapPolicy != "" {
  75. params["gap_policy"] = p.gapPolicy
  76. }
  77. // Add buckets paths
  78. switch len(p.bucketsPaths) {
  79. case 0:
  80. case 1:
  81. params["buckets_path"] = p.bucketsPaths[0]
  82. default:
  83. params["buckets_path"] = p.bucketsPaths
  84. }
  85. // Add percents
  86. if len(p.percents) > 0 {
  87. params["percents"] = p.percents
  88. }
  89. // AggregationBuilder (SubAggregations)
  90. if len(p.subAggregations) > 0 {
  91. aggsMap := make(map[string]interface{})
  92. source["aggregations"] = aggsMap
  93. for name, aggregate := range p.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(p.meta) > 0 {
  103. source["meta"] = p.meta
  104. }
  105. return source, nil
  106. }