search_aggs_histogram.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // HistogramAggregation is a multi-bucket values source based aggregation
  6. // that can be applied on numeric values extracted from the documents.
  7. // It dynamically builds fixed size (a.k.a. interval) buckets over the
  8. // values.
  9. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
  10. type HistogramAggregation struct {
  11. field string
  12. script string
  13. scriptFile string
  14. lang string
  15. params map[string]interface{}
  16. subAggregations map[string]Aggregation
  17. interval int64
  18. order string
  19. orderAsc bool
  20. minDocCount *int64
  21. extendedBoundsMin *int64
  22. extendedBoundsMax *int64
  23. }
  24. func NewHistogramAggregation() HistogramAggregation {
  25. a := HistogramAggregation{
  26. params: make(map[string]interface{}),
  27. subAggregations: make(map[string]Aggregation),
  28. }
  29. return a
  30. }
  31. func (a HistogramAggregation) Field(field string) HistogramAggregation {
  32. a.field = field
  33. return a
  34. }
  35. func (a HistogramAggregation) Script(script string) HistogramAggregation {
  36. a.script = script
  37. return a
  38. }
  39. func (a HistogramAggregation) ScriptFile(scriptFile string) HistogramAggregation {
  40. a.scriptFile = scriptFile
  41. return a
  42. }
  43. func (a HistogramAggregation) Lang(lang string) HistogramAggregation {
  44. a.lang = lang
  45. return a
  46. }
  47. func (a HistogramAggregation) Param(name string, value interface{}) HistogramAggregation {
  48. a.params[name] = value
  49. return a
  50. }
  51. func (a HistogramAggregation) SubAggregation(name string, subAggregation Aggregation) HistogramAggregation {
  52. a.subAggregations[name] = subAggregation
  53. return a
  54. }
  55. func (a HistogramAggregation) Interval(interval int64) HistogramAggregation {
  56. a.interval = interval
  57. return a
  58. }
  59. // Order specifies the sort order. Valid values for order are:
  60. // "_key", "_count", a sub-aggregation name, or a sub-aggregation name
  61. // with a metric.
  62. func (a HistogramAggregation) Order(order string, asc bool) HistogramAggregation {
  63. a.order = order
  64. a.orderAsc = asc
  65. return a
  66. }
  67. func (a HistogramAggregation) OrderByCount(asc bool) HistogramAggregation {
  68. // "order" : { "_count" : "asc" }
  69. a.order = "_count"
  70. a.orderAsc = asc
  71. return a
  72. }
  73. func (a HistogramAggregation) OrderByCountAsc() HistogramAggregation {
  74. return a.OrderByCount(true)
  75. }
  76. func (a HistogramAggregation) OrderByCountDesc() HistogramAggregation {
  77. return a.OrderByCount(false)
  78. }
  79. func (a HistogramAggregation) OrderByKey(asc bool) HistogramAggregation {
  80. // "order" : { "_key" : "asc" }
  81. a.order = "_key"
  82. a.orderAsc = asc
  83. return a
  84. }
  85. func (a HistogramAggregation) OrderByKeyAsc() HistogramAggregation {
  86. return a.OrderByKey(true)
  87. }
  88. func (a HistogramAggregation) OrderByKeyDesc() HistogramAggregation {
  89. return a.OrderByKey(false)
  90. }
  91. // OrderByAggregation creates a bucket ordering strategy which sorts buckets
  92. // based on a single-valued calc get.
  93. func (a HistogramAggregation) OrderByAggregation(aggName string, asc bool) HistogramAggregation {
  94. // {
  95. // "aggs" : {
  96. // "genders" : {
  97. // "terms" : {
  98. // "field" : "gender",
  99. // "order" : { "avg_height" : "desc" }
  100. // },
  101. // "aggs" : {
  102. // "avg_height" : { "avg" : { "field" : "height" } }
  103. // }
  104. // }
  105. // }
  106. // }
  107. a.order = aggName
  108. a.orderAsc = asc
  109. return a
  110. }
  111. // OrderByAggregationAndMetric creates a bucket ordering strategy which
  112. // sorts buckets based on a multi-valued calc get.
  113. func (a HistogramAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) HistogramAggregation {
  114. // {
  115. // "aggs" : {
  116. // "genders" : {
  117. // "terms" : {
  118. // "field" : "gender",
  119. // "order" : { "height_stats.avg" : "desc" }
  120. // },
  121. // "aggs" : {
  122. // "height_stats" : { "stats" : { "field" : "height" } }
  123. // }
  124. // }
  125. // }
  126. // }
  127. a.order = aggName + "." + metric
  128. a.orderAsc = asc
  129. return a
  130. }
  131. func (a HistogramAggregation) MinDocCount(minDocCount int64) HistogramAggregation {
  132. a.minDocCount = &minDocCount
  133. return a
  134. }
  135. func (a HistogramAggregation) ExtendedBoundsMin(min int64) HistogramAggregation {
  136. a.extendedBoundsMin = &min
  137. return a
  138. }
  139. func (a HistogramAggregation) ExtendedBoundsMax(max int64) HistogramAggregation {
  140. a.extendedBoundsMax = &max
  141. return a
  142. }
  143. func (a HistogramAggregation) Source() interface{} {
  144. // Example:
  145. // {
  146. // "aggs" : {
  147. // "prices" : {
  148. // "histogram" : {
  149. // "field" : "price",
  150. // "interval" : 50
  151. // }
  152. // }
  153. // }
  154. // }
  155. //
  156. // This method returns only the { "histogram" : { ... } } part.
  157. source := make(map[string]interface{})
  158. opts := make(map[string]interface{})
  159. source["histogram"] = opts
  160. // ValuesSourceAggregationBuilder
  161. if a.field != "" {
  162. opts["field"] = a.field
  163. }
  164. if a.script != "" {
  165. opts["script"] = a.script
  166. }
  167. if a.lang != "" {
  168. opts["lang"] = a.lang
  169. }
  170. if len(a.params) > 0 {
  171. opts["params"] = a.params
  172. }
  173. opts["interval"] = a.interval
  174. if a.order != "" {
  175. o := make(map[string]interface{})
  176. if a.orderAsc {
  177. o[a.order] = "asc"
  178. } else {
  179. o[a.order] = "desc"
  180. }
  181. opts["order"] = o
  182. }
  183. if a.minDocCount != nil {
  184. opts["min_doc_count"] = *a.minDocCount
  185. }
  186. if a.extendedBoundsMin != nil || a.extendedBoundsMax != nil {
  187. bounds := make(map[string]interface{})
  188. if a.extendedBoundsMin != nil {
  189. bounds["min"] = a.extendedBoundsMin
  190. }
  191. if a.extendedBoundsMax != nil {
  192. bounds["max"] = a.extendedBoundsMax
  193. }
  194. opts["extended_bounds"] = bounds
  195. }
  196. // AggregationBuilder (SubAggregations)
  197. if len(a.subAggregations) > 0 {
  198. aggsMap := make(map[string]interface{})
  199. source["aggregations"] = aggsMap
  200. for name, aggregate := range a.subAggregations {
  201. aggsMap[name] = aggregate.Source()
  202. }
  203. }
  204. return source
  205. }