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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/7.0/search-aggregations-pipeline-percentiles-bucket-aggregation.html
  12. type PercentilesBucketAggregation struct {
  13. format string
  14. gapPolicy string
  15. percents []float64
  16. bucketsPaths []string
  17. meta map[string]interface{}
  18. }
  19. // NewPercentilesBucketAggregation creates and initializes a new PercentilesBucketAggregation.
  20. func NewPercentilesBucketAggregation() *PercentilesBucketAggregation {
  21. return &PercentilesBucketAggregation{}
  22. }
  23. // Format to apply the output value of this aggregation.
  24. func (p *PercentilesBucketAggregation) Format(format string) *PercentilesBucketAggregation {
  25. p.format = format
  26. return p
  27. }
  28. // Percents to calculate percentiles for in this aggregation.
  29. func (p *PercentilesBucketAggregation) Percents(percents ...float64) *PercentilesBucketAggregation {
  30. p.percents = percents
  31. return p
  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 (p *PercentilesBucketAggregation) GapPolicy(gapPolicy string) *PercentilesBucketAggregation {
  36. p.gapPolicy = gapPolicy
  37. return p
  38. }
  39. // GapInsertZeros inserts zeros for gaps in the series.
  40. func (p *PercentilesBucketAggregation) GapInsertZeros() *PercentilesBucketAggregation {
  41. p.gapPolicy = "insert_zeros"
  42. return p
  43. }
  44. // GapSkip skips gaps in the series.
  45. func (p *PercentilesBucketAggregation) GapSkip() *PercentilesBucketAggregation {
  46. p.gapPolicy = "skip"
  47. return p
  48. }
  49. // Meta sets the meta data to be included in the aggregation response.
  50. func (p *PercentilesBucketAggregation) Meta(metaData map[string]interface{}) *PercentilesBucketAggregation {
  51. p.meta = metaData
  52. return p
  53. }
  54. // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
  55. func (p *PercentilesBucketAggregation) BucketsPath(bucketsPaths ...string) *PercentilesBucketAggregation {
  56. p.bucketsPaths = append(p.bucketsPaths, bucketsPaths...)
  57. return p
  58. }
  59. // Source returns the a JSON-serializable interface.
  60. func (p *PercentilesBucketAggregation) Source() (interface{}, error) {
  61. source := make(map[string]interface{})
  62. params := make(map[string]interface{})
  63. source["percentiles_bucket"] = params
  64. if p.format != "" {
  65. params["format"] = p.format
  66. }
  67. if p.gapPolicy != "" {
  68. params["gap_policy"] = p.gapPolicy
  69. }
  70. // Add buckets paths
  71. switch len(p.bucketsPaths) {
  72. case 0:
  73. case 1:
  74. params["buckets_path"] = p.bucketsPaths[0]
  75. default:
  76. params["buckets_path"] = p.bucketsPaths
  77. }
  78. // Add percents
  79. if len(p.percents) > 0 {
  80. params["percents"] = p.percents
  81. }
  82. // Add Meta data if available
  83. if len(p.meta) > 0 {
  84. source["meta"] = p.meta
  85. }
  86. return source, nil
  87. }