Feat: Support include filter for termAggs

This commit is contained in:
Hardy 2021-03-12 20:54:48 +08:00
parent cc685d325e
commit 12616dd9d3
2 changed files with 52 additions and 0 deletions

View File

@ -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"},
},
},
},
},
},
})
}

View File

@ -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,
}