Skip to content

Commit 244418f

Browse files
feature: add missing bucket aggregation types (#73)
Implement the following bucket aggregations with full DSL support: - FilterAgg: Single filter bucket aggregation with Aggs, Meta - FiltersAgg: Named multi-bucket filter aggregation with Filter, OtherBucket, OtherBucketKey, Aggs, Meta - RangeAgg: Numeric range bucket aggregation with Range, Keyed, Missing, Aggs, Meta + RangeEntry (From, To, Key) - DateRangeAgg: Date range bucket aggregation with Range, Format, Keyed, Missing, TimeZone, Aggs, Meta + DateRangeEntry (From, To, Key) - HistogramAgg: Fixed-interval numeric histogram with MinDocCount, ExtendedBounds, HardBounds, Offset, Keyed, Missing, Order, Aggs, Meta - DateHistogramAgg: Date-based histogram with CalendarInterval, FixedInterval, Format, TimeZone, Offset, MinDocCount, ExtendedBounds, HardBounds, Keyed, Missing, Order, Aggs, Meta - ReverseNestedAgg: Reverse nested aggregation with Path, Aggs, Meta All implementations follow existing project conventions and patterns. * test: add order field tests for histogram aggregations Add test cases verifying Order method generates correct JSON output for HistogramAgg and DateHistogramAgg with _count desc ordering.
1 parent ba0c8d7 commit 244418f

14 files changed

Lines changed: 2834 additions & 0 deletions

es/aggregation_date_histogram.go

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
package es
2+
3+
type dateHistogramAggType Object
4+
5+
// DateHistogramAgg creates a date histogram aggregation for a given field.
6+
//
7+
// A date histogram aggregation is similar to the normal histogram aggregation, but it can
8+
// only be applied on date or date range values. The interval is specified using date/time
9+
// expressions.
10+
//
11+
// Example usage:
12+
//
13+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month")
14+
// // This creates a date histogram aggregation on "timestamp" with monthly intervals.
15+
//
16+
// Parameters:
17+
// - field: The date field on which the date histogram aggregation is applied.
18+
//
19+
// Returns:
20+
//
21+
// An es.dateHistogramAggType object representing the date histogram aggregation.
22+
func DateHistogramAgg(field string) dateHistogramAggType {
23+
return dateHistogramAggType{
24+
"date_histogram": Object{
25+
"field": field,
26+
},
27+
}
28+
}
29+
30+
// CalendarInterval sets the "calendar_interval" parameter in the date histogram aggregation.
31+
//
32+
// Calendar-aware intervals understand that daylight savings changes the length of specific
33+
// days, months have different amounts of days, and leap seconds can be tacked on to a
34+
// particular year. Supported values: minute, 1m, hour, 1h, day, 1d, week, 1w, month, 1M,
35+
// quarter, 1q, year, 1y.
36+
//
37+
// Example usage:
38+
//
39+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month")
40+
//
41+
// Parameters:
42+
// - calendarInterval: A string representing the calendar interval.
43+
//
44+
// Returns:
45+
//
46+
// The updated es.dateHistogramAggType object with the "calendar_interval" parameter set.
47+
func (d dateHistogramAggType) CalendarInterval(calendarInterval string) dateHistogramAggType {
48+
return d.putInTheField("calendar_interval", calendarInterval)
49+
}
50+
51+
// FixedInterval sets the "fixed_interval" parameter in the date histogram aggregation.
52+
//
53+
// Fixed intervals are a fixed number of SI units and never deviate, regardless of where
54+
// they fall on the calendar. Supported units: ms (milliseconds), s (seconds), m (minutes),
55+
// h (hours), d (days).
56+
//
57+
// Example usage:
58+
//
59+
// agg := es.DateHistogramAgg("timestamp").FixedInterval("30d")
60+
//
61+
// Parameters:
62+
// - fixedInterval: A string representing the fixed interval.
63+
//
64+
// Returns:
65+
//
66+
// The updated es.dateHistogramAggType object with the "fixed_interval" parameter set.
67+
func (d dateHistogramAggType) FixedInterval(fixedInterval string) dateHistogramAggType {
68+
return d.putInTheField("fixed_interval", fixedInterval)
69+
}
70+
71+
// Format sets the "format" parameter in the date histogram aggregation.
72+
//
73+
// This method specifies the date format used to format the bucket keys in the response.
74+
//
75+
// Example usage:
76+
//
77+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month").Format("yyyy-MM-dd")
78+
//
79+
// Parameters:
80+
// - format: A string representing the date format pattern.
81+
//
82+
// Returns:
83+
//
84+
// The updated es.dateHistogramAggType object with the "format" parameter set.
85+
func (d dateHistogramAggType) Format(format string) dateHistogramAggType {
86+
return d.putInTheField("format", format)
87+
}
88+
89+
// TimeZone sets the "time_zone" parameter in the date histogram aggregation.
90+
//
91+
// This method specifies the time zone to use for date calculations. Date-times are stored
92+
// in UTC in Elasticsearch. By default, all bucketing and rounding is done in UTC. The
93+
// time_zone parameter can be used to indicate that bucketing should use a different time zone.
94+
//
95+
// Example usage:
96+
//
97+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("day").TimeZone("-01:00")
98+
//
99+
// Parameters:
100+
// - timeZone: A string representing the time zone (e.g., "CET", "UTC", "+01:00").
101+
//
102+
// Returns:
103+
//
104+
// The updated es.dateHistogramAggType object with the "time_zone" parameter set.
105+
func (d dateHistogramAggType) TimeZone(timeZone string) dateHistogramAggType {
106+
return d.putInTheField("time_zone", timeZone)
107+
}
108+
109+
// Offset sets the "offset" parameter in the date histogram aggregation.
110+
//
111+
// This method changes the start value of each bucket by the specified positive (+) or
112+
// negative offset (-) duration, such as 1h for an hour, or 1d for a day.
113+
//
114+
// Example usage:
115+
//
116+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("day").Offset("+6h")
117+
//
118+
// Parameters:
119+
// - offset: A string representing the offset duration.
120+
//
121+
// Returns:
122+
//
123+
// The updated es.dateHistogramAggType object with the "offset" parameter set.
124+
func (d dateHistogramAggType) Offset(offset string) dateHistogramAggType {
125+
return d.putInTheField("offset", offset)
126+
}
127+
128+
// MinDocCount sets the minimum document count required for a bucket to be included.
129+
//
130+
// Example usage:
131+
//
132+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month").MinDocCount(1)
133+
//
134+
// Parameters:
135+
// - minDocCount: The minimum number of documents required for a bucket.
136+
//
137+
// Returns:
138+
//
139+
// The updated es.dateHistogramAggType object with the "min_doc_count" parameter set.
140+
func (d dateHistogramAggType) MinDocCount(minDocCount int) dateHistogramAggType {
141+
return d.putInTheField("min_doc_count", minDocCount)
142+
}
143+
144+
// ExtendedBounds sets the "extended_bounds" parameter in the date histogram aggregation.
145+
//
146+
// This method forces the date histogram to start building buckets on a specific min date
147+
// and keep building buckets up to a max date, even if there are no documents in some buckets.
148+
//
149+
// Example usage:
150+
//
151+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month").
152+
// ExtendedBounds("2020-01-01", "2020-12-31")
153+
//
154+
// Parameters:
155+
// - min: The minimum boundary for the date histogram.
156+
// - max: The maximum boundary for the date histogram.
157+
//
158+
// Returns:
159+
//
160+
// The updated es.dateHistogramAggType object with the "extended_bounds" parameter set.
161+
func (d dateHistogramAggType) ExtendedBounds(min, max any) dateHistogramAggType {
162+
return d.putInTheField("extended_bounds", Object{
163+
"min": min,
164+
"max": max,
165+
})
166+
}
167+
168+
// HardBounds sets the "hard_bounds" parameter in the date histogram aggregation.
169+
//
170+
// This method limits the range of buckets in the date histogram. Buckets outside the
171+
// hard bounds will not be generated.
172+
//
173+
// Example usage:
174+
//
175+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month").
176+
// HardBounds("2020-01-01", "2020-12-31")
177+
//
178+
// Parameters:
179+
// - min: The minimum hard boundary.
180+
// - max: The maximum hard boundary.
181+
//
182+
// Returns:
183+
//
184+
// The updated es.dateHistogramAggType object with the "hard_bounds" parameter set.
185+
func (d dateHistogramAggType) HardBounds(min, max any) dateHistogramAggType {
186+
return d.putInTheField("hard_bounds", Object{
187+
"min": min,
188+
"max": max,
189+
})
190+
}
191+
192+
// Keyed sets the "keyed" parameter in the date histogram aggregation.
193+
//
194+
// This method specifies whether the buckets should be returned as a hash instead of an array.
195+
//
196+
// Example usage:
197+
//
198+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month").Keyed(true)
199+
//
200+
// Parameters:
201+
// - keyed: A boolean indicating whether to return keyed buckets.
202+
//
203+
// Returns:
204+
//
205+
// The updated es.dateHistogramAggType object with the "keyed" parameter set.
206+
func (d dateHistogramAggType) Keyed(keyed bool) dateHistogramAggType {
207+
return d.putInTheField("keyed", keyed)
208+
}
209+
210+
// Missing sets a default value to use for documents that do not contain the field.
211+
//
212+
// Example usage:
213+
//
214+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month").Missing("2000-01-01")
215+
//
216+
// Parameters:
217+
// - missing: The value to use when a document lacks the field.
218+
//
219+
// Returns:
220+
//
221+
// An es.dateHistogramAggType object with the "missing" field set.
222+
func (d dateHistogramAggType) Missing(missing any) dateHistogramAggType {
223+
return d.putInTheField("missing", missing)
224+
}
225+
226+
// Order sets the sorting order of the date histogram buckets.
227+
//
228+
// Example usage:
229+
//
230+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month").
231+
// Order(es.AggOrder("_count", Order.Desc))
232+
//
233+
// Parameters:
234+
// - orders: A variadic list of sorting rules.
235+
//
236+
// Returns:
237+
//
238+
// The updated es.dateHistogramAggType object with the "order" parameter set.
239+
func (d dateHistogramAggType) Order(orders ...aggOrder) dateHistogramAggType {
240+
if len(orders) == 1 && orders[0] == nil {
241+
return d
242+
}
243+
return d.putInTheField("order", orders)
244+
}
245+
246+
// Aggs adds sub-aggregations to the date histogram aggregation.
247+
//
248+
// Example usage:
249+
//
250+
// agg := es.DateHistogramAgg("timestamp").CalendarInterval("month").
251+
// Aggs(es.Agg("avg_price", es.AvgAgg("price")))
252+
//
253+
// Parameters:
254+
// - aggs: A variadic list of sub-aggregations.
255+
//
256+
// Returns:
257+
//
258+
// An es.dateHistogramAggType object with the specified sub-aggregations added.
259+
func (d dateHistogramAggType) Aggs(aggs ...aggsType) dateHistogramAggType {
260+
return genericPutAggsInRoot(d, aggs)
261+
}
262+
263+
// Meta adds metadata to the date histogram aggregation.
264+
//
265+
// Example usage:
266+
//
267+
// agg := es.DateHistogramAgg("timestamp").Meta("description", "Monthly histogram")
268+
//
269+
// Parameters:
270+
// - key: Metadata key.
271+
// - value: Metadata value.
272+
//
273+
// Returns:
274+
//
275+
// A modified es.dateHistogramAggType with the meta field set.
276+
func (d dateHistogramAggType) Meta(key string, value any) dateHistogramAggType {
277+
meta, ok := d["meta"].(Object)
278+
if !ok {
279+
meta = Object{}
280+
}
281+
meta[key] = value
282+
d["meta"] = meta
283+
return d
284+
}
285+
286+
func (d dateHistogramAggType) putInTheField(key string, value any) dateHistogramAggType {
287+
return genericPutInTheField(d, "date_histogram", key, value)
288+
}

0 commit comments

Comments
 (0)