search_aggs_date_range.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. import (
  6. "time"
  7. )
  8. // DateRangeAggregation is a range aggregation that is dedicated for
  9. // date values. The main difference between this aggregation and the
  10. // normal range aggregation is that the from and to values can be expressed
  11. // in Date Math expressions, and it is also possible to specify a
  12. // date format by which the from and to response fields will be returned.
  13. // Note that this aggregration includes the from value and excludes the to
  14. // value for each range.
  15. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html
  16. type DateRangeAggregation struct {
  17. field string
  18. script string
  19. scriptFile string
  20. lang string
  21. params map[string]interface{}
  22. subAggregations map[string]Aggregation
  23. keyed *bool
  24. unmapped *bool
  25. format string
  26. entries []DateRangeAggregationEntry
  27. }
  28. type DateRangeAggregationEntry struct {
  29. Key string
  30. From interface{}
  31. To interface{}
  32. }
  33. func NewDateRangeAggregation() DateRangeAggregation {
  34. a := DateRangeAggregation{
  35. params: make(map[string]interface{}),
  36. subAggregations: make(map[string]Aggregation),
  37. entries: make([]DateRangeAggregationEntry, 0),
  38. }
  39. return a
  40. }
  41. func (a DateRangeAggregation) Field(field string) DateRangeAggregation {
  42. a.field = field
  43. return a
  44. }
  45. func (a DateRangeAggregation) Script(script string) DateRangeAggregation {
  46. a.script = script
  47. return a
  48. }
  49. func (a DateRangeAggregation) ScriptFile(scriptFile string) DateRangeAggregation {
  50. a.scriptFile = scriptFile
  51. return a
  52. }
  53. func (a DateRangeAggregation) Lang(lang string) DateRangeAggregation {
  54. a.lang = lang
  55. return a
  56. }
  57. func (a DateRangeAggregation) Param(name string, value interface{}) DateRangeAggregation {
  58. a.params[name] = value
  59. return a
  60. }
  61. func (a DateRangeAggregation) SubAggregation(name string, subAggregation Aggregation) DateRangeAggregation {
  62. a.subAggregations[name] = subAggregation
  63. return a
  64. }
  65. func (a DateRangeAggregation) Keyed(keyed bool) DateRangeAggregation {
  66. a.keyed = &keyed
  67. return a
  68. }
  69. func (a DateRangeAggregation) Unmapped(unmapped bool) DateRangeAggregation {
  70. a.unmapped = &unmapped
  71. return a
  72. }
  73. func (a DateRangeAggregation) Format(format string) DateRangeAggregation {
  74. a.format = format
  75. return a
  76. }
  77. func (a DateRangeAggregation) AddRange(from, to interface{}) DateRangeAggregation {
  78. a.entries = append(a.entries, DateRangeAggregationEntry{From: from, To: to})
  79. return a
  80. }
  81. func (a DateRangeAggregation) AddRangeWithKey(key string, from, to interface{}) DateRangeAggregation {
  82. a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: from, To: to})
  83. return a
  84. }
  85. func (a DateRangeAggregation) AddUnboundedTo(from interface{}) DateRangeAggregation {
  86. a.entries = append(a.entries, DateRangeAggregationEntry{From: from, To: nil})
  87. return a
  88. }
  89. func (a DateRangeAggregation) AddUnboundedToWithKey(key string, from interface{}) DateRangeAggregation {
  90. a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: from, To: nil})
  91. return a
  92. }
  93. func (a DateRangeAggregation) AddUnboundedFrom(to interface{}) DateRangeAggregation {
  94. a.entries = append(a.entries, DateRangeAggregationEntry{From: nil, To: to})
  95. return a
  96. }
  97. func (a DateRangeAggregation) AddUnboundedFromWithKey(key string, to interface{}) DateRangeAggregation {
  98. a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: nil, To: to})
  99. return a
  100. }
  101. func (a DateRangeAggregation) Lt(to interface{}) DateRangeAggregation {
  102. a.entries = append(a.entries, DateRangeAggregationEntry{From: nil, To: to})
  103. return a
  104. }
  105. func (a DateRangeAggregation) LtWithKey(key string, to interface{}) DateRangeAggregation {
  106. a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: nil, To: to})
  107. return a
  108. }
  109. func (a DateRangeAggregation) Between(from, to interface{}) DateRangeAggregation {
  110. a.entries = append(a.entries, DateRangeAggregationEntry{From: from, To: to})
  111. return a
  112. }
  113. func (a DateRangeAggregation) BetweenWithKey(key string, from, to interface{}) DateRangeAggregation {
  114. a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: from, To: to})
  115. return a
  116. }
  117. func (a DateRangeAggregation) Gt(from interface{}) DateRangeAggregation {
  118. a.entries = append(a.entries, DateRangeAggregationEntry{From: from, To: nil})
  119. return a
  120. }
  121. func (a DateRangeAggregation) GtWithKey(key string, from interface{}) DateRangeAggregation {
  122. a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: from, To: nil})
  123. return a
  124. }
  125. func (a DateRangeAggregation) Source() interface{} {
  126. // Example:
  127. // {
  128. // "aggs" : {
  129. // "range" : {
  130. // "date_range": {
  131. // "field": "date",
  132. // "format": "MM-yyy",
  133. // "ranges": [
  134. // { "to": "now-10M/M" },
  135. // { "from": "now-10M/M" }
  136. // ]
  137. // }
  138. // }
  139. // }
  140. // }
  141. // }
  142. //
  143. // This method returns only the { "date_range" : { ... } } part.
  144. source := make(map[string]interface{})
  145. opts := make(map[string]interface{})
  146. source["date_range"] = opts
  147. // ValuesSourceAggregationBuilder
  148. if a.field != "" {
  149. opts["field"] = a.field
  150. }
  151. if a.script != "" {
  152. opts["script"] = a.script
  153. }
  154. if a.scriptFile != "" {
  155. opts["script_file"] = a.scriptFile
  156. }
  157. if a.lang != "" {
  158. opts["lang"] = a.lang
  159. }
  160. if len(a.params) > 0 {
  161. opts["params"] = a.params
  162. }
  163. if a.keyed != nil {
  164. opts["keyed"] = *a.keyed
  165. }
  166. if a.unmapped != nil {
  167. opts["unmapped"] = *a.unmapped
  168. }
  169. if a.format != "" {
  170. opts["format"] = a.format
  171. }
  172. ranges := make([]interface{}, 0)
  173. for _, ent := range a.entries {
  174. r := make(map[string]interface{})
  175. if ent.Key != "" {
  176. r["key"] = ent.Key
  177. }
  178. if ent.From != nil {
  179. switch from := ent.From.(type) {
  180. case int, int16, int32, int64, float32, float64:
  181. r["from"] = from
  182. case time.Time:
  183. r["from"] = from.Format(time.RFC3339)
  184. case string:
  185. r["from"] = from
  186. }
  187. }
  188. if ent.To != nil {
  189. switch to := ent.To.(type) {
  190. case int, int16, int32, int64, float32, float64:
  191. r["to"] = to
  192. case time.Time:
  193. r["to"] = to.Format(time.RFC3339)
  194. case string:
  195. r["to"] = to
  196. }
  197. }
  198. ranges = append(ranges, r)
  199. }
  200. opts["ranges"] = ranges
  201. // AggregationBuilder (SubAggregations)
  202. if len(a.subAggregations) > 0 {
  203. aggsMap := make(map[string]interface{})
  204. source["aggregations"] = aggsMap
  205. for name, aggregate := range a.subAggregations {
  206. aggsMap[name] = aggregate.Source()
  207. }
  208. }
  209. return source
  210. }