search_aggs_percentiles.go 3.4 KB

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