forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_aggs_bucket_auto_date_histogram.go
163 lines (141 loc) · 4.55 KB
/
search_aggs_bucket_auto_date_histogram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
// AutoDateHistogramAggregation is a multi-bucket aggregation similar to the
// histogram except it can only be applied on date values, and the buckets num can bin pointed.
// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-aggregations-bucket-autodatehistogram-aggregation.html
type AutoDateHistogramAggregation struct {
field string
script *Script
missing interface{}
subAggregations map[string]Aggregation
meta map[string]interface{}
buckets int
minDocCount *int64
timeZone string
format string
minimumInterval string
}
// NewAutoDateHistogramAggregation creates a new AutoDateHistogramAggregation.
func NewAutoDateHistogramAggregation() *AutoDateHistogramAggregation {
return &AutoDateHistogramAggregation{
subAggregations: make(map[string]Aggregation),
}
}
// Field on which the aggregation is processed.
func (a *AutoDateHistogramAggregation) Field(field string) *AutoDateHistogramAggregation {
a.field = field
return a
}
// Script on which th
func (a *AutoDateHistogramAggregation) Script(script *Script) *AutoDateHistogramAggregation {
a.script = script
return a
}
// Missing configures the value to use when documents miss a value.
func (a *AutoDateHistogramAggregation) Missing(missing interface{}) *AutoDateHistogramAggregation {
a.missing = missing
return a
}
// SubAggregation sub aggregation
func (a *AutoDateHistogramAggregation) SubAggregation(name string, subAggregation Aggregation) *AutoDateHistogramAggregation {
a.subAggregations[name] = subAggregation
return a
}
// Meta sets the meta data to be included in the aggregation response.
func (a *AutoDateHistogramAggregation) Meta(metaData map[string]interface{}) *AutoDateHistogramAggregation {
a.meta = metaData
return a
}
// Buckets buckets num by which the aggregation gets processed.
func (a *AutoDateHistogramAggregation) Buckets(buckets int) *AutoDateHistogramAggregation {
a.buckets = buckets
return a
}
// MinDocCount sets the minimum document count per bucket.
// Buckets with less documents than this min value will not be returned.
func (a *AutoDateHistogramAggregation) MinDocCount(minDocCount int64) *AutoDateHistogramAggregation {
a.minDocCount = &minDocCount
return a
}
// TimeZone sets the timezone in which to translate dates before computing buckets.
func (a *AutoDateHistogramAggregation) TimeZone(timeZone string) *AutoDateHistogramAggregation {
a.timeZone = timeZone
return a
}
// Format sets the format to use for dates.
func (a *AutoDateHistogramAggregation) Format(format string) *AutoDateHistogramAggregation {
a.format = format
return a
}
// MinimumInterval accepted units for minimum_interval are: year/month/day/hour/minute/second
func (a *AutoDateHistogramAggregation) MinimumInterval(interval string) *AutoDateHistogramAggregation {
a.minimumInterval = interval
return a
}
// Source source for AutoDateHistogramAggregation
func (a *AutoDateHistogramAggregation) Source() (interface{}, error) {
// Example:
// {
// "aggs" : {
// "articles_over_time" : {
// "auto_date_histogram" : {
// "field" : "date",
// "buckets" : 10
// }
// }
// }
// }
//
// This method returns only the { "auto_date_histogram" : { ... } } part.
source := make(map[string]interface{})
opts := make(map[string]interface{})
source["auto_date_histogram"] = opts
// ValuesSourceAggregationBuilder
if a.field != "" {
opts["field"] = a.field
}
if a.script != nil {
src, err := a.script.Source()
if err != nil {
return nil, err
}
opts["script"] = src
}
if a.missing != nil {
opts["missing"] = a.missing
}
if a.buckets > 0 {
opts["buckets"] = a.buckets
}
if a.minDocCount != nil {
opts["min_doc_count"] = *a.minDocCount
}
if a.timeZone != "" {
opts["time_zone"] = a.timeZone
}
if a.format != "" {
opts["format"] = a.format
}
if a.minimumInterval != "" {
opts["minimum_interval"] = a.minimumInterval
}
// AggregationBuilder (SubAggregations)
if len(a.subAggregations) > 0 {
aggsMap := make(map[string]interface{})
source["aggregations"] = aggsMap
for name, aggregate := range a.subAggregations {
src, err := aggregate.Source()
if err != nil {
return nil, err
}
aggsMap[name] = src
}
}
// Add Meta data if available
if len(a.meta) > 0 {
source["meta"] = a.meta
}
return source, nil
}