Debian Codegraph @

search_aggs_bucket_terms.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 // TermsAggregation is a multi-bucket value source based aggregation
8 // where buckets are dynamically built - one per unique value.
9 // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
10 type TermsAggregation struct {
11 field string
12 script *Script
13 missing interface{}
14 subAggregations map[string]Aggregation
15 meta map[string]interface{}
16
17 size *int
18 shardSize *int
19 requiredSize *int
20 minDocCount *int
21 shardMinDocCount *int
22 valueType string
23 includeExclude *TermsAggregationIncludeExclude
24 executionHint string
25 collectionMode string
26 showTermDocCountError *bool
27 order []TermsOrder
28 }
29
30 func NewTermsAggregation() *TermsAggregation {
31 return &TermsAggregation{
32 subAggregations: make(map[string]Aggregation, 0),
33 }
34 }
35
36 func (a *TermsAggregation) Field(field string) *TermsAggregation {
37 a.field = field
38 return a
39 }
40
41 func (a *TermsAggregation) Script(script *Script) *TermsAggregation {
42 a.script = script
43 return a
44 }
45
46 // Missing configures the value to use when documents miss a value.
47 func (a *TermsAggregation) Missing(missing interface{}) *TermsAggregation {
48 a.missing = missing
49 return a
50 }
51
52 func (a *TermsAggregation) SubAggregation(name string, subAggregation Aggregation) *TermsAggregation {
53 a.subAggregations[name] = subAggregation
54 return a
55 }
56
57 // Meta sets the meta data to be included in the aggregation response.
58 func (a *TermsAggregation) Meta(metaData map[string]interface{}) *TermsAggregation {
59 a.meta = metaData
60 return a
61 }
62
63 func (a *TermsAggregation) Size(size int) *TermsAggregation {
64 a.size = &size
65 return a
66 }
67
68 func (a *TermsAggregation) RequiredSize(requiredSize int) *TermsAggregation {
69 a.requiredSize = &requiredSize
70 return a
71 }
72
73 func (a *TermsAggregation) ShardSize(shardSize int) *TermsAggregation {
74 a.shardSize = &shardSize
75 return a
76 }
77
78 func (a *TermsAggregation) MinDocCount(minDocCount int) *TermsAggregation {
79 a.minDocCount = &minDocCount
80 return a
81 }
82
83 func (a *TermsAggregation) ShardMinDocCount(shardMinDocCount int) *TermsAggregation {
84 a.shardMinDocCount = &shardMinDocCount
85 return a
86 }
87
88 func (a *TermsAggregation) Include(regexp string) *TermsAggregation {
89 if a.includeExclude == nil {
90 a.includeExclude = &TermsAggregationIncludeExclude{}
91 }
92 a.includeExclude.Include = regexp
93 return a
94 }
95
96 func (a *TermsAggregation) IncludeValues(values ...interface{}) *TermsAggregation {
97 if a.includeExclude == nil {
98 a.includeExclude = &TermsAggregationIncludeExclude{}
99 }
100 a.includeExclude.IncludeValues = append(a.includeExclude.IncludeValues, values...)
101 return a
102 }
103
104 func (a *TermsAggregation) Exclude(regexp string) *TermsAggregation {
105 if a.includeExclude == nil {
106 a.includeExclude = &TermsAggregationIncludeExclude{}
107 }
108 a.includeExclude.Exclude = regexp
109 return a
110 }
111
112 func (a *TermsAggregation) ExcludeValues(values ...interface{}) *TermsAggregation {
113 if a.includeExclude == nil {
114 a.includeExclude = &TermsAggregationIncludeExclude{}
115 }
116 a.includeExclude.ExcludeValues = append(a.includeExclude.ExcludeValues, values...)
117 return a
118 }
119
120 func (a *TermsAggregation) Partition(p int) *TermsAggregation {
121 if a.includeExclude == nil {
122 a.includeExclude = &TermsAggregationIncludeExclude{}
123 }
124 a.includeExclude.Partition = p
125 return a
126 }
127
128 func (a *TermsAggregation) NumPartitions(n int) *TermsAggregation {
129 if a.includeExclude == nil {
130 a.includeExclude = &TermsAggregationIncludeExclude{}
131 }
132 a.includeExclude.NumPartitions = n
133 return a
134 }
135
136 // ValueType can be string, long, or double.
137 func (a *TermsAggregation) ValueType(valueType string) *TermsAggregation {
138 a.valueType = valueType
139 return a
140 }
141
142 func (a *TermsAggregation) Order(order string, asc bool) *TermsAggregation {
143 a.order = append(a.order, TermsOrder{Field: order, Ascending: asc})
144 return a
145 }
146
147 func (a *TermsAggregation) OrderByCount(asc bool) *TermsAggregation {
148 // "order" : { "_count" : "asc" }
149 a.order = append(a.order, TermsOrder{Field: "_count", Ascending: asc})
150 return a
151 }
152
153 func (a *TermsAggregation) OrderByCountAsc() *TermsAggregation {
154 return a.OrderByCount(true)
155 }
156
157 func (a *TermsAggregation) OrderByCountDesc() *TermsAggregation {
158 return a.OrderByCount(false)
159 }
160
161 func (a *TermsAggregation) OrderByTerm(asc bool) *TermsAggregation {
162 // "order" : { "_term" : "asc" }
163 a.order = append(a.order, TermsOrder{Field: "_term", Ascending: asc})
164 return a
165 }
166
167 func (a *TermsAggregation) OrderByTermAsc() *TermsAggregation {
168 return a.OrderByTerm(true)
169 }
170
171 func (a *TermsAggregation) OrderByTermDesc() *TermsAggregation {
172 return a.OrderByTerm(false)
173 }
174
175 // OrderByAggregation creates a bucket ordering strategy which sorts buckets
176 // based on a single-valued calc get.
177 func (a *TermsAggregation) OrderByAggregation(aggName string, asc bool) *TermsAggregation {
178 // {
179 // "aggs" : {
180 // "genders" : {
181 // "terms" : {
182 // "field" : "gender",
183 // "order" : { "avg_height" : "desc" }
184 // },
185 // "aggs" : {
186 // "avg_height" : { "avg" : { "field" : "height" } }
187 // }
188 // }
189 // }
190 // }
191 a.order = append(a.order, TermsOrder{Field: aggName, Ascending: asc})
192 return a
193 }
194
195 // OrderByAggregationAndMetric creates a bucket ordering strategy which
196 // sorts buckets based on a multi-valued calc get.
197 func (a *TermsAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) *TermsAggregation {
198 // {
199 // "aggs" : {
200 // "genders" : {
201 // "terms" : {
202 // "field" : "gender",
203 // "order" : { "height_stats.avg" : "desc" }
204 // },
205 // "aggs" : {
206 // "height_stats" : { "stats" : { "field" : "height" } }
207 // }
208 // }
209 // }
210 // }
211 a.order = append(a.order, TermsOrder{Field: aggName + "." + metric, Ascending: asc})
212 return a
213 }
214
215 func (a *TermsAggregation) ExecutionHint(hint string) *TermsAggregation {
216 a.executionHint = hint
217 return a
218 }
219
220 // Collection mode can be depth_first or breadth_first as of 1.4.0.
221 func (a *TermsAggregation) CollectionMode(collectionMode string) *TermsAggregation {
222 a.collectionMode = collectionMode
223 return a
224 }
225
226 func (a *TermsAggregation) ShowTermDocCountError(showTermDocCountError bool) *TermsAggregation {
227 a.showTermDocCountError = &showTermDocCountError
228 return a
229 }
230
231 func (a *TermsAggregation) Source() (interface{}, error) {
232 // Example:
233 // {
234 // "aggs" : {
235 // "genders" : {
236 // "terms" : { "field" : "gender" }
237 // }
238 // }
239 // }
240 // This method returns only the { "terms" : { "field" : "gender" } } part.
241
242 source := make(map[string]interface{})
243 opts := make(map[string]interface{})
244 source["terms"] = opts
245
246 // ValuesSourceAggregationBuilder
247 if a.field != "" {
248 opts["field"] = a.field
249 }
250 if a.script != nil {
251 src, err := a.script.Source()
252 if err != nil {
253 return nil, err
254 }
255 opts["script"] = src
256 }
257 if a.missing != nil {
258 opts["missing"] = a.missing
259 }
260
261 // TermsBuilder
262 if a.size != nil && *a.size >= 0 {
263 opts["size"] = *a.size
264 }
265 if a.shardSize != nil && *a.shardSize >= 0 {
266 opts["shard_size"] = *a.shardSize
267 }
268 if a.requiredSize != nil && *a.requiredSize >= 0 {
269 opts["required_size"] = *a.requiredSize
270 }
271 if a.minDocCount != nil && *a.minDocCount >= 0 {
272 opts["min_doc_count"] = *a.minDocCount
273 }
274 if a.shardMinDocCount != nil && *a.shardMinDocCount >= 0 {
275 opts["shard_min_doc_count"] = *a.shardMinDocCount
276 }
277 if a.showTermDocCountError != nil {
278 opts["show_term_doc_count_error"] = *a.showTermDocCountError
279 }
280 if a.collectionMode != "" {
281 opts["collect_mode"] = a.collectionMode
282 }
283 if a.valueType != "" {
284 opts["value_type"] = a.valueType
285 }
286 if len(a.order) > 0 {
287 var orderSlice []interface{}
288 for _, order := range a.order {
289 src, err := order.Source()
290 if err != nil {
291 return nil, err
292 }
293 orderSlice = append(orderSlice, src)
294 }
295 opts["order"] = orderSlice
296 }
297 // Include/Exclude
298 if ie := a.includeExclude; ie != nil {
299 // Include
300 if ie.Include != "" {
301 opts["include"] = ie.Include
302 } else if len(ie.IncludeValues) > 0 {
303 opts["include"] = ie.IncludeValues
304 } else if ie.NumPartitions > 0 {
305 inc := make(map[string]interface{})
306 inc["partition"] = ie.Partition
307 inc["num_partitions"] = ie.NumPartitions
308 opts["include"] = inc
309 }
310 // Exclude
311 if ie.Exclude != "" {
312 opts["exclude"] = ie.Exclude
313 } else if len(ie.ExcludeValues) > 0 {
314 opts["exclude"] = ie.ExcludeValues
315 }
316 }
317
318 if a.executionHint != "" {
319 opts["execution_hint"] = a.executionHint
320 }
321
322 // AggregationBuilder (SubAggregations)
323 if len(a.subAggregations) > 0 {
324 aggsMap := make(map[string]interface{})
325 source["aggregations"] = aggsMap
326 for name, aggregate := range a.subAggregations {
327 src, err := aggregate.Source()
328 if err != nil {
329 return nil, err
330 }
331 aggsMap[name] = src
332 }
333 }
334
335 // Add Meta data if available
336 if len(a.meta) > 0 {
337 source["meta"] = a.meta
338 }
339
340 return source, nil
341 }
342
343 // TermsAggregationIncludeExclude allows for include/exclude in a TermsAggregation.
344 type TermsAggregationIncludeExclude struct {
345 Include string
346 Exclude string
347 IncludeValues []interface{}
348 ExcludeValues []interface{}
349 Partition int
350 NumPartitions int
351 }
352
353 // TermsOrder specifies a single order field for a terms aggregation.
354 type TermsOrder struct {
355 Field string
356 Ascending bool
357 }
358
359 // Source returns serializable JSON of the TermsOrder.
360 func (order *TermsOrder) Source() (interface{}, error) {
361 source := make(map[string]string)
362 if order.Ascending {
363 source[order.Field] = "asc"
364 } else {
365 source[order.Field] = "desc"
366 }
367 return source, nil
368 }