search_aggs_date_histogram.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. // DateHistogramAggregation is a multi-bucket aggregation similar to the
  6. // histogram except it can only be applied on date values.
  7. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html
  8. type DateHistogramAggregation struct {
  9. field string
  10. script string
  11. scriptFile string
  12. lang string
  13. params map[string]interface{}
  14. subAggregations map[string]Aggregation
  15. interval string
  16. order string
  17. orderAsc bool
  18. minDocCount *int64
  19. extendedBoundsMin interface{}
  20. extendedBoundsMax interface{}
  21. preZone string
  22. postZone string
  23. preZoneAdjustLargeInterval *bool
  24. format string
  25. preOffset int64
  26. postOffset int64
  27. factor *float32
  28. }
  29. func NewDateHistogramAggregation() DateHistogramAggregation {
  30. a := DateHistogramAggregation{
  31. params: make(map[string]interface{}),
  32. subAggregations: make(map[string]Aggregation),
  33. }
  34. return a
  35. }
  36. func (a DateHistogramAggregation) Field(field string) DateHistogramAggregation {
  37. a.field = field
  38. return a
  39. }
  40. func (a DateHistogramAggregation) Script(script string) DateHistogramAggregation {
  41. a.script = script
  42. return a
  43. }
  44. func (a DateHistogramAggregation) ScriptFile(scriptFile string) DateHistogramAggregation {
  45. a.scriptFile = scriptFile
  46. return a
  47. }
  48. func (a DateHistogramAggregation) Lang(lang string) DateHistogramAggregation {
  49. a.lang = lang
  50. return a
  51. }
  52. func (a DateHistogramAggregation) Param(name string, value interface{}) DateHistogramAggregation {
  53. a.params[name] = value
  54. return a
  55. }
  56. func (a DateHistogramAggregation) SubAggregation(name string, subAggregation Aggregation) DateHistogramAggregation {
  57. a.subAggregations[name] = subAggregation
  58. return a
  59. }
  60. // Allowed values are: "year", "quarter", "month", "week", "day",
  61. // "hour", "minute". It also supports time settings like "1.5h"
  62. // (up to "w" for weeks).
  63. func (a DateHistogramAggregation) Interval(interval string) DateHistogramAggregation {
  64. a.interval = interval
  65. return a
  66. }
  67. // Order specifies the sort order. Valid values for order are:
  68. // "_key", "_count", a sub-aggregation name, or a sub-aggregation name
  69. // with a metric.
  70. func (a DateHistogramAggregation) Order(order string, asc bool) DateHistogramAggregation {
  71. a.order = order
  72. a.orderAsc = asc
  73. return a
  74. }
  75. func (a DateHistogramAggregation) OrderByCount(asc bool) DateHistogramAggregation {
  76. // "order" : { "_count" : "asc" }
  77. a.order = "_count"
  78. a.orderAsc = asc
  79. return a
  80. }
  81. func (a DateHistogramAggregation) OrderByCountAsc() DateHistogramAggregation {
  82. return a.OrderByCount(true)
  83. }
  84. func (a DateHistogramAggregation) OrderByCountDesc() DateHistogramAggregation {
  85. return a.OrderByCount(false)
  86. }
  87. func (a DateHistogramAggregation) OrderByKey(asc bool) DateHistogramAggregation {
  88. // "order" : { "_key" : "asc" }
  89. a.order = "_key"
  90. a.orderAsc = asc
  91. return a
  92. }
  93. func (a DateHistogramAggregation) OrderByKeyAsc() DateHistogramAggregation {
  94. return a.OrderByKey(true)
  95. }
  96. func (a DateHistogramAggregation) OrderByKeyDesc() DateHistogramAggregation {
  97. return a.OrderByKey(false)
  98. }
  99. // OrderByAggregation creates a bucket ordering strategy which sorts buckets
  100. // based on a single-valued calc get.
  101. func (a DateHistogramAggregation) OrderByAggregation(aggName string, asc bool) DateHistogramAggregation {
  102. // {
  103. // "aggs" : {
  104. // "genders" : {
  105. // "terms" : {
  106. // "field" : "gender",
  107. // "order" : { "avg_height" : "desc" }
  108. // },
  109. // "aggs" : {
  110. // "avg_height" : { "avg" : { "field" : "height" } }
  111. // }
  112. // }
  113. // }
  114. // }
  115. a.order = aggName
  116. a.orderAsc = asc
  117. return a
  118. }
  119. // OrderByAggregationAndMetric creates a bucket ordering strategy which
  120. // sorts buckets based on a multi-valued calc get.
  121. func (a DateHistogramAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) DateHistogramAggregation {
  122. // {
  123. // "aggs" : {
  124. // "genders" : {
  125. // "terms" : {
  126. // "field" : "gender",
  127. // "order" : { "height_stats.avg" : "desc" }
  128. // },
  129. // "aggs" : {
  130. // "height_stats" : { "stats" : { "field" : "height" } }
  131. // }
  132. // }
  133. // }
  134. // }
  135. a.order = aggName + "." + metric
  136. a.orderAsc = asc
  137. return a
  138. }
  139. func (a DateHistogramAggregation) MinDocCount(minDocCount int64) DateHistogramAggregation {
  140. a.minDocCount = &minDocCount
  141. return a
  142. }
  143. func (a DateHistogramAggregation) PreZone(preZone string) DateHistogramAggregation {
  144. a.preZone = preZone
  145. return a
  146. }
  147. func (a DateHistogramAggregation) PostZone(postZone string) DateHistogramAggregation {
  148. a.postZone = postZone
  149. return a
  150. }
  151. func (a DateHistogramAggregation) PreZoneAdjustLargeInterval(preZoneAdjustLargeInterval bool) DateHistogramAggregation {
  152. a.preZoneAdjustLargeInterval = &preZoneAdjustLargeInterval
  153. return a
  154. }
  155. func (a DateHistogramAggregation) PreOffset(preOffset int64) DateHistogramAggregation {
  156. a.preOffset = preOffset
  157. return a
  158. }
  159. func (a DateHistogramAggregation) PostOffset(postOffset int64) DateHistogramAggregation {
  160. a.postOffset = postOffset
  161. return a
  162. }
  163. func (a DateHistogramAggregation) Factor(factor float32) DateHistogramAggregation {
  164. a.factor = &factor
  165. return a
  166. }
  167. func (a DateHistogramAggregation) Format(format string) DateHistogramAggregation {
  168. a.format = format
  169. return a
  170. }
  171. // ExtendedBoundsMin accepts int, int64, string, or time.Time values.
  172. func (a DateHistogramAggregation) ExtendedBoundsMin(min interface{}) DateHistogramAggregation {
  173. a.extendedBoundsMin = min
  174. return a
  175. }
  176. // ExtendedBoundsMax accepts int, int64, string, or time.Time values.
  177. func (a DateHistogramAggregation) ExtendedBoundsMax(max interface{}) DateHistogramAggregation {
  178. a.extendedBoundsMax = max
  179. return a
  180. }
  181. func (a DateHistogramAggregation) Source() interface{} {
  182. // Example:
  183. // {
  184. // "aggs" : {
  185. // "articles_over_time" : {
  186. // "date_histogram" : {
  187. // "field" : "date",
  188. // "interval" : "month"
  189. // }
  190. // }
  191. // }
  192. // }
  193. //
  194. // This method returns only the { "date_histogram" : { ... } } part.
  195. source := make(map[string]interface{})
  196. opts := make(map[string]interface{})
  197. source["date_histogram"] = opts
  198. // ValuesSourceAggregationBuilder
  199. if a.field != "" {
  200. opts["field"] = a.field
  201. }
  202. if a.script != "" {
  203. opts["script"] = a.script
  204. }
  205. if a.scriptFile != "" {
  206. opts["script_file"] = a.scriptFile
  207. }
  208. if a.lang != "" {
  209. opts["lang"] = a.lang
  210. }
  211. if len(a.params) > 0 {
  212. opts["params"] = a.params
  213. }
  214. opts["interval"] = a.interval
  215. if a.minDocCount != nil {
  216. opts["min_doc_count"] = *a.minDocCount
  217. }
  218. if a.order != "" {
  219. o := make(map[string]interface{})
  220. if a.orderAsc {
  221. o[a.order] = "asc"
  222. } else {
  223. o[a.order] = "desc"
  224. }
  225. opts["order"] = o
  226. }
  227. if a.preZone != "" {
  228. opts["pre_zone"] = a.preZone
  229. }
  230. if a.postZone != "" {
  231. opts["post_zone"] = a.postZone
  232. }
  233. if a.preZoneAdjustLargeInterval != nil {
  234. opts["pre_zone_adjust_large_interval"] = *a.preZoneAdjustLargeInterval
  235. }
  236. if a.preOffset != 0 {
  237. opts["pre_offset"] = a.preOffset
  238. }
  239. if a.postOffset != 0 {
  240. opts["post_offset"] = a.postOffset
  241. }
  242. if a.factor != nil {
  243. opts["factor"] = *a.factor
  244. }
  245. if a.format != "" {
  246. opts["format"] = a.format
  247. }
  248. if a.extendedBoundsMin != nil || a.extendedBoundsMax != nil {
  249. bounds := make(map[string]interface{})
  250. if a.extendedBoundsMin != nil {
  251. bounds["min"] = a.extendedBoundsMin
  252. }
  253. if a.extendedBoundsMax != nil {
  254. bounds["max"] = a.extendedBoundsMax
  255. }
  256. opts["extended_bounds"] = bounds
  257. }
  258. // AggregationBuilder (SubAggregations)
  259. if len(a.subAggregations) > 0 {
  260. aggsMap := make(map[string]interface{})
  261. source["aggregations"] = aggsMap
  262. for name, aggregate := range a.subAggregations {
  263. aggsMap[name] = aggregate.Source()
  264. }
  265. }
  266. return source
  267. }