search_aggs_metrics_percentiles.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. method string
  15. compression *float64
  16. numberOfSignificantValueDigits *int
  17. estimator string
  18. }
  19. func NewPercentilesAggregation() *PercentilesAggregation {
  20. return &PercentilesAggregation{
  21. subAggregations: make(map[string]Aggregation),
  22. percentiles: make([]float64, 0),
  23. method: "tdigest",
  24. }
  25. }
  26. func (a *PercentilesAggregation) Field(field string) *PercentilesAggregation {
  27. a.field = field
  28. return a
  29. }
  30. func (a *PercentilesAggregation) Script(script *Script) *PercentilesAggregation {
  31. a.script = script
  32. return a
  33. }
  34. func (a *PercentilesAggregation) Format(format string) *PercentilesAggregation {
  35. a.format = format
  36. return a
  37. }
  38. func (a *PercentilesAggregation) SubAggregation(name string, subAggregation Aggregation) *PercentilesAggregation {
  39. a.subAggregations[name] = subAggregation
  40. return a
  41. }
  42. // Meta sets the meta data to be included in the aggregation response.
  43. func (a *PercentilesAggregation) Meta(metaData map[string]interface{}) *PercentilesAggregation {
  44. a.meta = metaData
  45. return a
  46. }
  47. func (a *PercentilesAggregation) Percentiles(percentiles ...float64) *PercentilesAggregation {
  48. a.percentiles = append(a.percentiles, percentiles...)
  49. return a
  50. }
  51. // Method is the percentiles method, which can be "tdigest" (default) or "hdr".
  52. func (a *PercentilesAggregation) Method(method string) *PercentilesAggregation {
  53. a.method = method
  54. return a
  55. }
  56. func (a *PercentilesAggregation) Compression(compression float64) *PercentilesAggregation {
  57. a.compression = &compression
  58. return a
  59. }
  60. func (a *PercentilesAggregation) NumberOfSignificantValueDigits(digits int) *PercentilesAggregation {
  61. a.numberOfSignificantValueDigits = &digits
  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{}, error) {
  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 != nil {
  90. src, err := a.script.Source()
  91. if err != nil {
  92. return nil, err
  93. }
  94. opts["script"] = src
  95. }
  96. if a.format != "" {
  97. opts["format"] = a.format
  98. }
  99. if len(a.percentiles) > 0 {
  100. opts["percents"] = a.percentiles
  101. }
  102. switch a.method {
  103. case "tdigest":
  104. if c := a.compression; c != nil {
  105. opts[a.method] = map[string]interface{}{
  106. "compression": *c,
  107. }
  108. }
  109. case "hdr":
  110. if n := a.numberOfSignificantValueDigits; n != nil {
  111. opts[a.method] = map[string]interface{}{
  112. "number_of_significant_value_digits": *n,
  113. }
  114. }
  115. }
  116. if a.estimator != "" {
  117. opts["estimator"] = a.estimator
  118. }
  119. // AggregationBuilder (SubAggregations)
  120. if len(a.subAggregations) > 0 {
  121. aggsMap := make(map[string]interface{})
  122. source["aggregations"] = aggsMap
  123. for name, aggregate := range a.subAggregations {
  124. src, err := aggregate.Source()
  125. if err != nil {
  126. return nil, err
  127. }
  128. aggsMap[name] = src
  129. }
  130. }
  131. // Add Meta data if available
  132. if len(a.meta) > 0 {
  133. source["meta"] = a.meta
  134. }
  135. return source, nil
  136. }