search_aggs_metrics_percentiles.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // PercentilesAggregation
  6. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-percentile-aggregation.html
  7. type PercentilesAggregation struct {
  8. field string
  9. script *Script
  10. format string
  11. subAggregations map[string]Aggregation
  12. meta map[string]interface{}
  13. percentiles []float64
  14. compression *float64
  15. estimator string
  16. }
  17. func NewPercentilesAggregation() *PercentilesAggregation {
  18. return &PercentilesAggregation{
  19. subAggregations: make(map[string]Aggregation),
  20. percentiles: make([]float64, 0),
  21. }
  22. }
  23. func (a *PercentilesAggregation) Field(field string) *PercentilesAggregation {
  24. a.field = field
  25. return a
  26. }
  27. func (a *PercentilesAggregation) Script(script *Script) *PercentilesAggregation {
  28. a.script = script
  29. return a
  30. }
  31. func (a *PercentilesAggregation) Format(format string) *PercentilesAggregation {
  32. a.format = format
  33. return a
  34. }
  35. func (a *PercentilesAggregation) SubAggregation(name string, subAggregation Aggregation) *PercentilesAggregation {
  36. a.subAggregations[name] = subAggregation
  37. return a
  38. }
  39. // Meta sets the meta data to be included in the aggregation response.
  40. func (a *PercentilesAggregation) Meta(metaData map[string]interface{}) *PercentilesAggregation {
  41. a.meta = metaData
  42. return a
  43. }
  44. func (a *PercentilesAggregation) Percentiles(percentiles ...float64) *PercentilesAggregation {
  45. a.percentiles = append(a.percentiles, percentiles...)
  46. return a
  47. }
  48. func (a *PercentilesAggregation) Compression(compression float64) *PercentilesAggregation {
  49. a.compression = &compression
  50. return a
  51. }
  52. func (a *PercentilesAggregation) Estimator(estimator string) *PercentilesAggregation {
  53. a.estimator = estimator
  54. return a
  55. }
  56. func (a *PercentilesAggregation) Source() (interface{}, error) {
  57. // Example:
  58. // {
  59. // "aggs" : {
  60. // "load_time_outlier" : {
  61. // "percentiles" : {
  62. // "field" : "load_time"
  63. // }
  64. // }
  65. // }
  66. // }
  67. // This method returns only the
  68. // { "percentiles" : { "field" : "load_time" } }
  69. // part.
  70. source := make(map[string]interface{})
  71. opts := make(map[string]interface{})
  72. source["percentiles"] = opts
  73. // ValuesSourceAggregationBuilder
  74. if a.field != "" {
  75. opts["field"] = a.field
  76. }
  77. if a.script != nil {
  78. src, err := a.script.Source()
  79. if err != nil {
  80. return nil, err
  81. }
  82. opts["script"] = src
  83. }
  84. if a.format != "" {
  85. opts["format"] = a.format
  86. }
  87. if len(a.percentiles) > 0 {
  88. opts["percents"] = a.percentiles
  89. }
  90. if a.compression != nil {
  91. opts["compression"] = *a.compression
  92. }
  93. if a.estimator != "" {
  94. opts["estimator"] = a.estimator
  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. }