date-histogram-agg #3

Merged
maxim.yanchuk merged 2 commits from date-histogram-agg into master 2023-10-30 12:07:01 +00:00
3 changed files with 166 additions and 1 deletions

View File

@ -158,6 +158,7 @@ The following aggregations are currently supported:
| `"string_stats"` | `StringStats()` | | `"string_stats"` | `StringStats()` |
| `"top_hits"` | `TopHits()` | | `"top_hits"` | `TopHits()` |
| `"terms"` | `TermsAgg()` | | `"terms"` | `TermsAgg()` |
| `"date_histogram"` | `DateHistogramAgg()` |
### Supported Top Level Options ### Supported Top Level Options

View File

@ -62,7 +62,14 @@ func TestAggregations(t *testing.T) {
"a complex, multi-aggregation, nested", "a complex, multi-aggregation, nested",
Aggregate( Aggregate(
NestedAgg("categories", "categories"). NestedAgg("categories", "categories").
Aggs(TermsAgg("type", "outdoors")), Aggs(
TermsAgg("type", "outdoors").Aggs(
DateHistogramAgg("time", "timestamp").
Fixedinterval("3m").MinDocCount(0).Aggs(
Sum("sumPeople", "people"),
),
),
),
FilterAgg("filtered", FilterAgg("filtered",
Term("type", "t-shirt")), Term("type", "t-shirt")),
), ),
@ -77,6 +84,22 @@ func TestAggregations(t *testing.T) {
"terms": map[string]interface{}{ "terms": map[string]interface{}{
"field": "outdoors", "field": "outdoors",
}, },
"aggs": map[string]interface{}{
"time": map[string]interface{}{
"date_histogram": map[string]interface{}{
"field": "timestamp",
"fixed_interval": "3m",
"min_doc_count": 0,
},
"aggs": map[string]interface{}{
"sumPeople": map[string]interface{}{
"sum": map[string]interface{}{
"field": "people",
},
},
},
},
},
}, },
}, },
}, },

View File

@ -110,3 +110,144 @@ func (agg *TermsAggregation) Map() map[string]interface{} {
return outerMap return outerMap
} }
//----------------------------------------------------------------------------//
// DateHistogramAggregation represents an aggregation of type "date_histogram", as described in
// https://www.elastic.co/guide/en/elasticsearch/reference/current/
// search-aggregations-bucket-datehistogram-aggregation.html
type DateHistogramAggregation struct {
name string
field string
calendarInterval string
fixedInterval string
format string
offset string
keyed *bool
minDocCount *uint64
missing string
order map[string]string
aggs []Aggregation
}
// DateHistogramAgg creates a new aggregation of type "date_histogram".
func DateHistogramAgg(name, field string) *DateHistogramAggregation {
return &DateHistogramAggregation{
name: name,
field: field,
}
}
// Name returns the name of the aggregation.
func (agg *DateHistogramAggregation) Name() string {
return agg.name
}
// Aggs sets sub-aggregations for the aggregation.
func (agg *DateHistogramAggregation) Aggs(aggs ...Aggregation) *DateHistogramAggregation {
agg.aggs = aggs
return agg
}
// CalendarInterval sets calendarInterval
func (agg *DateHistogramAggregation) CalendarInterval(interval string) *DateHistogramAggregation {
agg.calendarInterval = interval
return agg
}
// Fixedinterval sets fixedInterval
func (agg *DateHistogramAggregation) Fixedinterval(interval string) *DateHistogramAggregation {
agg.fixedInterval = interval
return agg
}
// Format sets format
func (agg *DateHistogramAggregation) Format(format string) *DateHistogramAggregation {
agg.format = format
return agg
}
// Offset sets offset
func (agg *DateHistogramAggregation) Offset(offset string) *DateHistogramAggregation {
agg.offset = offset
return agg
}
// Order sets the sort for terms agg
func (agg *DateHistogramAggregation) Order(order map[string]string) *DateHistogramAggregation {
agg.order = order
return agg
}
// Keyed sets keyed is true or false
func (agg *DateHistogramAggregation) Keyed(keyed bool) *DateHistogramAggregation {
agg.keyed = &keyed
return agg
}
// Missing sets missing value
func (agg *DateHistogramAggregation) Missing(missing string) *DateHistogramAggregation {
agg.missing = missing
return agg
}
// MinDocCount sets min doc count
func (agg *DateHistogramAggregation) MinDocCount(minDocCount uint64) *DateHistogramAggregation {
agg.minDocCount = &minDocCount
return agg
}
// Map returns a map representation of the aggregation, thus implementing the
// Mappable interface.
func (agg *DateHistogramAggregation) Map() map[string]interface{} {
innerMap := map[string]interface{}{
"field": agg.field,
}
if agg.calendarInterval != "" {
innerMap["calendar_interval"] = agg.calendarInterval
}
if agg.fixedInterval != "" {
innerMap["fixed_interval"] = agg.fixedInterval
}
if agg.format != "" {
innerMap["format"] = agg.format
}
if agg.offset != "" {
innerMap["offset"] = agg.offset
}
if agg.missing != "" {
innerMap["missing"] = agg.missing
}
if agg.minDocCount != nil {
innerMap["min_doc_count"] = agg.minDocCount
}
if agg.keyed != nil {
innerMap["keyed"] = *agg.keyed
}
if agg.order != nil {
innerMap["order"] = agg.order
}
outerMap := map[string]interface{}{
"date_histogram": innerMap,
}
if len(agg.aggs) > 0 {
subAggs := make(map[string]map[string]interface{})
for _, sub := range agg.aggs {
subAggs[sub.Name()] = sub.Map()
}
outerMap["aggs"] = subAggs
}
return outerMap
}