search_aggs_metrics_percentile_ranks.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // PercentileRanksAggregation
  6. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-percentile-rank-aggregation.html
  7. type PercentileRanksAggregation struct {
  8. field string
  9. script *Script
  10. format string
  11. subAggregations map[string]Aggregation
  12. meta map[string]interface{}
  13. values []float64
  14. compression *float64
  15. estimator string
  16. }
  17. func NewPercentileRanksAggregation() *PercentileRanksAggregation {
  18. return &PercentileRanksAggregation{
  19. subAggregations: make(map[string]Aggregation),
  20. values: make([]float64, 0),
  21. }
  22. }
  23. func (a *PercentileRanksAggregation) Field(field string) *PercentileRanksAggregation {
  24. a.field = field
  25. return a
  26. }
  27. func (a *PercentileRanksAggregation) Script(script *Script) *PercentileRanksAggregation {
  28. a.script = script
  29. return a
  30. }
  31. func (a *PercentileRanksAggregation) Format(format string) *PercentileRanksAggregation {
  32. a.format = format
  33. return a
  34. }
  35. func (a *PercentileRanksAggregation) SubAggregation(name string, subAggregation Aggregation) *PercentileRanksAggregation {
  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 *PercentileRanksAggregation) Meta(metaData map[string]interface{}) *PercentileRanksAggregation {
  41. a.meta = metaData
  42. return a
  43. }
  44. func (a *PercentileRanksAggregation) Values(values ...float64) *PercentileRanksAggregation {
  45. a.values = append(a.values, values...)
  46. return a
  47. }
  48. func (a *PercentileRanksAggregation) Compression(compression float64) *PercentileRanksAggregation {
  49. a.compression = &compression
  50. return a
  51. }
  52. func (a *PercentileRanksAggregation) Estimator(estimator string) *PercentileRanksAggregation {
  53. a.estimator = estimator
  54. return a
  55. }
  56. func (a *PercentileRanksAggregation) Source() (interface{}, error) {
  57. // Example:
  58. // {
  59. // "aggs" : {
  60. // "load_time_outlier" : {
  61. // "percentile_ranks" : {
  62. // "field" : "load_time"
  63. // "values" : [15, 30]
  64. // }
  65. // }
  66. // }
  67. // }
  68. // This method returns only the
  69. // { "percentile_ranks" : { "field" : "load_time", "values" : [15, 30] } }
  70. // part.
  71. source := make(map[string]interface{})
  72. opts := make(map[string]interface{})
  73. source["percentile_ranks"] = opts
  74. // ValuesSourceAggregationBuilder
  75. if a.field != "" {
  76. opts["field"] = a.field
  77. }
  78. if a.script != nil {
  79. src, err := a.script.Source()
  80. if err != nil {
  81. return nil, err
  82. }
  83. opts["script"] = src
  84. }
  85. if a.format != "" {
  86. opts["format"] = a.format
  87. }
  88. if len(a.values) > 0 {
  89. opts["values"] = a.values
  90. }
  91. if a.compression != nil {
  92. opts["compression"] = *a.compression
  93. }
  94. if a.estimator != "" {
  95. opts["estimator"] = a.estimator
  96. }
  97. // AggregationBuilder (SubAggregations)
  98. if len(a.subAggregations) > 0 {
  99. aggsMap := make(map[string]interface{})
  100. source["aggregations"] = aggsMap
  101. for name, aggregate := range a.subAggregations {
  102. src, err := aggregate.Source()
  103. if err != nil {
  104. return nil, err
  105. }
  106. aggsMap[name] = src
  107. }
  108. }
  109. // Add Meta data if available
  110. if len(a.meta) > 0 {
  111. source["meta"] = a.meta
  112. }
  113. return source, nil
  114. }