search_aggs_percentile_ranks.go 3.5 KB

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