diff --git a/aggregations_test.go b/aggregations_test.go index 080ff30..949123d 100644 --- a/aggregations_test.go +++ b/aggregations_test.go @@ -120,5 +120,41 @@ func TestAggregations(t *testing.T) { }, }, }, + { + "Single include for termsAggs", + //eq.Aggregate(eq.TermsAgg("a1", "FIELD1").Size(0).Aggs(eq.Sum("a2", "FIELD2.SUBFIELD"))) + Aggregate( + TermsAgg("categories", "categories"). + Include("red.*|blue.*"), + ), + map[string]interface{}{ + "aggs": map[string]interface{}{ + "categories": map[string]interface{}{ + "terms": map[string]interface{}{ + "field": "categories", + "include": "red.*|blue.*", + }, + }, + }, + }, + }, + { + "Multi include for termsAggs", + //eq.Aggregate(eq.TermsAgg("a1", "FIELD1").Size(0).Aggs(eq.Sum("a2", "FIELD2.SUBFIELD"))) + Aggregate( + TermsAgg("categories", "categories"). + Include("red", "blue"), + ), + map[string]interface{}{ + "aggs": map[string]interface{}{ + "categories": map[string]interface{}{ + "terms": map[string]interface{}{ + "field": "categories", + "include": []string{"red", "blue"}, + }, + }, + }, + }, + }, }) } diff --git a/aggs_bucket.go b/aggs_bucket.go index c13a898..1a45a14 100644 --- a/aggs_bucket.go +++ b/aggs_bucket.go @@ -13,6 +13,7 @@ type TermsAggregation struct { showTermDoc *bool aggs []Aggregation order map[string]string + include []string } // TermsAgg creates a new aggregation of type "terms". The method name includes @@ -61,6 +62,12 @@ func (agg *TermsAggregation) Order(order map[string]string) *TermsAggregation { return agg } +// Include filter the values for buckets +func (agg *TermsAggregation) Include(include ...string) *TermsAggregation { + agg.include = include + return agg +} + // Map returns a map representation of the aggregation, thus implementing the // Mappable interface. func (agg *TermsAggregation) Map() map[string]interface{} { @@ -81,6 +88,15 @@ func (agg *TermsAggregation) Map() map[string]interface{} { innerMap["order"] = agg.order } + if agg.include != nil { + if len(agg.include) <= 1 { + innerMap["include"] = agg.include[0] + } else { + innerMap["include"] = agg.include + } + + } + outerMap := map[string]interface{}{ "terms": innerMap, }