Debian Codegraph @

search_aggs_bucket_date_histogram.go

raw
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
5 package elastic
6
7 // DateHistogramAggregation is a multi-bucket aggregation similar to the
8 // histogram except it can only be applied on date values.
9 // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-datehistogram-aggregation.html
10 type DateHistogramAggregation struct {
11 field string
12 script *Script
13 missing interface{}
14 subAggregations map[string]Aggregation
15 meta map[string]interface{}
16
17 interval string
18 order string
19 orderAsc bool
20 minDocCount *int64
21 extendedBoundsMin interface{}
22 extendedBoundsMax interface{}
23 timeZone string
24 format string
25 offset string
26 keyed *bool
27 }
28
29 // NewDateHistogramAggregation creates a new DateHistogramAggregation.
30 func NewDateHistogramAggregation() *DateHistogramAggregation {
31 return &DateHistogramAggregation{
32 subAggregations: make(map[string]Aggregation),
33 }
34 }
35
36 // Field on which the aggregation is processed.
37 func (a *DateHistogramAggregation) Field(field string) *DateHistogramAggregation {
38 a.field = field
39 return a
40 }
41
42 func (a *DateHistogramAggregation) Script(script *Script) *DateHistogramAggregation {
43 a.script = script
44 return a
45 }
46
47 // Missing configures the value to use when documents miss a value.
48 func (a *DateHistogramAggregation) Missing(missing interface{}) *DateHistogramAggregation {
49 a.missing = missing
50 return a
51 }
52
53 func (a *DateHistogramAggregation) SubAggregation(name string, subAggregation Aggregation) *DateHistogramAggregation {
54 a.subAggregations[name] = subAggregation
55 return a
56 }
57
58 // Meta sets the meta data to be included in the aggregation response.
59 func (a *DateHistogramAggregation) Meta(metaData map[string]interface{}) *DateHistogramAggregation {
60 a.meta = metaData
61 return a
62 }
63
64 // Interval by which the aggregation gets processed.
65 // Allowed values are: "year", "quarter", "month", "week", "day",
66 // "hour", "minute". It also supports time settings like "1.5h"
67 // (up to "w" for weeks).
68 func (a *DateHistogramAggregation) Interval(interval string) *DateHistogramAggregation {
69 a.interval = interval
70 return a
71 }
72
73 // Order specifies the sort order. Valid values for order are:
74 // "_key", "_count", a sub-aggregation name, or a sub-aggregation name
75 // with a metric.
76 func (a *DateHistogramAggregation) Order(order string, asc bool) *DateHistogramAggregation {
77 a.order = order
78 a.orderAsc = asc
79 return a
80 }
81
82 func (a *DateHistogramAggregation) OrderByCount(asc bool) *DateHistogramAggregation {
83 // "order" : { "_count" : "asc" }
84 a.order = "_count"
85 a.orderAsc = asc
86 return a
87 }
88
89 func (a *DateHistogramAggregation) OrderByCountAsc() *DateHistogramAggregation {
90 return a.OrderByCount(true)
91 }
92
93 func (a *DateHistogramAggregation) OrderByCountDesc() *DateHistogramAggregation {
94 return a.OrderByCount(false)
95 }
96
97 func (a *DateHistogramAggregation) OrderByKey(asc bool) *DateHistogramAggregation {
98 // "order" : { "_key" : "asc" }
99 a.order = "_key"
100 a.orderAsc = asc
101 return a
102 }
103
104 func (a *DateHistogramAggregation) OrderByKeyAsc() *DateHistogramAggregation {
105 return a.OrderByKey(true)
106 }
107
108 func (a *DateHistogramAggregation) OrderByKeyDesc() *DateHistogramAggregation {
109 return a.OrderByKey(false)
110 }
111
112 // OrderByAggregation creates a bucket ordering strategy which sorts buckets
113 // based on a single-valued calc get.
114 func (a *DateHistogramAggregation) OrderByAggregation(aggName string, asc bool) *DateHistogramAggregation {
115 // {
116 // "aggs" : {
117 // "genders" : {
118 // "terms" : {
119 // "field" : "gender",
120 // "order" : { "avg_height" : "desc" }
121 // },
122 // "aggs" : {
123 // "avg_height" : { "avg" : { "field" : "height" } }
124 // }
125 // }
126 // }
127 // }
128 a.order = aggName
129 a.orderAsc = asc
130 return a
131 }
132
133 // OrderByAggregationAndMetric creates a bucket ordering strategy which
134 // sorts buckets based on a multi-valued calc get.
135 func (a *DateHistogramAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) *DateHistogramAggregation {
136 // {
137 // "aggs" : {
138 // "genders" : {
139 // "terms" : {
140 // "field" : "gender",
141 // "order" : { "height_stats.avg" : "desc" }
142 // },
143 // "aggs" : {
144 // "height_stats" : { "stats" : { "field" : "height" } }
145 // }
146 // }
147 // }
148 // }
149 a.order = aggName + "." + metric
150 a.orderAsc = asc
151 return a
152 }
153
154 // MinDocCount sets the minimum document count per bucket.
155 // Buckets with less documents than this min value will not be returned.
156 func (a *DateHistogramAggregation) MinDocCount(minDocCount int64) *DateHistogramAggregation {
157 a.minDocCount = &minDocCount
158 return a
159 }
160
161 // TimeZone sets the timezone in which to translate dates before computing buckets.
162 func (a *DateHistogramAggregation) TimeZone(timeZone string) *DateHistogramAggregation {
163 a.timeZone = timeZone
164 return a
165 }
166
167 // Format sets the format to use for dates.
168 func (a *DateHistogramAggregation) Format(format string) *DateHistogramAggregation {
169 a.format = format
170 return a
171 }
172
173 // Offset sets the offset of time intervals in the histogram, e.g. "+6h".
174 func (a *DateHistogramAggregation) Offset(offset string) *DateHistogramAggregation {
175 a.offset = offset
176 return a
177 }
178
179 // ExtendedBounds accepts int, int64, string, or time.Time values.
180 // In case the lower value in the histogram would be greater than min or the
181 // upper value would be less than max, empty buckets will be generated.
182 func (a *DateHistogramAggregation) ExtendedBounds(min, max interface{}) *DateHistogramAggregation {
183 a.extendedBoundsMin = min
184 a.extendedBoundsMax = max
185 return a
186 }
187
188 // ExtendedBoundsMin accepts int, int64, string, or time.Time values.
189 func (a *DateHistogramAggregation) ExtendedBoundsMin(min interface{}) *DateHistogramAggregation {
190 a.extendedBoundsMin = min
191 return a
192 }
193
194 // ExtendedBoundsMax accepts int, int64, string, or time.Time values.
195 func (a *DateHistogramAggregation) ExtendedBoundsMax(max interface{}) *DateHistogramAggregation {
196 a.extendedBoundsMax = max
197 return a
198 }
199
200 // Keyed specifies whether to return the results with a keyed response (or not).
201 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-datehistogram-aggregation.html#_keyed_response_3.
202 func (a *DateHistogramAggregation) Keyed(keyed bool) *DateHistogramAggregation {
203 a.keyed = &keyed
204 return a
205 }
206
207 func (a *DateHistogramAggregation) Source() (interface{}, error) {
208 // Example:
209 // {
210 // "aggs" : {
211 // "articles_over_time" : {
212 // "date_histogram" : {
213 // "field" : "date",
214 // "interval" : "month"
215 // }
216 // }
217 // }
218 // }
219 //
220 // This method returns only the { "date_histogram" : { ... } } part.
221
222 source := make(map[string]interface{})
223 opts := make(map[string]interface{})
224 source["date_histogram"] = opts
225
226 // ValuesSourceAggregationBuilder
227 if a.field != "" {
228 opts["field"] = a.field
229 }
230 if a.script != nil {
231 src, err := a.script.Source()
232 if err != nil {
233 return nil, err
234 }
235 opts["script"] = src
236 }
237 if a.missing != nil {
238 opts["missing"] = a.missing
239 }
240
241 opts["interval"] = a.interval
242 if a.minDocCount != nil {
243 opts["min_doc_count"] = *a.minDocCount
244 }
245 if a.order != "" {
246 o := make(map[string]interface{})
247 if a.orderAsc {
248 o[a.order] = "asc"
249 } else {
250 o[a.order] = "desc"
251 }
252 opts["order"] = o
253 }
254 if a.timeZone != "" {
255 opts["time_zone"] = a.timeZone
256 }
257 if a.offset != "" {
258 opts["offset"] = a.offset
259 }
260 if a.format != "" {
261 opts["format"] = a.format
262 }
263 if a.extendedBoundsMin != nil || a.extendedBoundsMax != nil {
264 bounds := make(map[string]interface{})
265 if a.extendedBoundsMin != nil {
266 bounds["min"] = a.extendedBoundsMin
267 }
268 if a.extendedBoundsMax != nil {
269 bounds["max"] = a.extendedBoundsMax
270 }
271 opts["extended_bounds"] = bounds
272 }
273 if a.keyed != nil {
274 opts["keyed"] = *a.keyed
275 }
276
277 // AggregationBuilder (SubAggregations)
278 if len(a.subAggregations) > 0 {
279 aggsMap := make(map[string]interface{})
280 source["aggregations"] = aggsMap
281 for name, aggregate := range a.subAggregations {
282 src, err := aggregate.Source()
283 if err != nil {
284 return nil, err
285 }
286 aggsMap[name] = src
287 }
288 }
289
290 // Add Meta data if available
291 if len(a.meta) > 0 {
292 source["meta"] = a.meta
293 }
294
295 return source, nil
296 }