From c2693c64015b47377eece55eeac205137f9e4415 Mon Sep 17 00:00:00 2001 From: Jacky Wu Date: Fri, 21 Jan 2022 14:09:06 +0800 Subject: [PATCH 1/2] feat: add date_histogram agg. --- aggregations_test.go | 25 +++++++- aggs_bucket.go | 141 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 1 deletion(-) diff --git a/aggregations_test.go b/aggregations_test.go index 949123d..4d0238f 100644 --- a/aggregations_test.go +++ b/aggregations_test.go @@ -62,7 +62,14 @@ func TestAggregations(t *testing.T) { "a complex, multi-aggregation, nested", Aggregate( 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", Term("type", "t-shirt")), ), @@ -77,6 +84,22 @@ func TestAggregations(t *testing.T) { "terms": map[string]interface{}{ "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", + }, + }, + }, + }, + }, }, }, }, diff --git a/aggs_bucket.go b/aggs_bucket.go index 1a45a14..f4bcfbe 100644 --- a/aggs_bucket.go +++ b/aggs_bucket.go @@ -110,3 +110,144 @@ func (agg *TermsAggregation) Map() map[string]interface{} { 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 +} From 2a3074fe893c70d503c6d26ddd4ccff9a759d967 Mon Sep 17 00:00:00 2001 From: Jacky Wu Date: Fri, 21 Jan 2022 14:15:54 +0800 Subject: [PATCH 2/2] doc: add date histogram agg in supported agg function list. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7e0eea7..52d6680 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ The following aggregations are currently supported: | `"string_stats"` | `StringStats()` | | `"top_hits"` | `TopHits()` | | `"terms"` | `TermsAgg()` | +| `"date_histogram"` | `DateHistogramAgg()` | ### Supported Top Level Options