search-collapse #6

Merged
maxim.yanchuk merged 17 commits from search-collapse into master 2023-11-01 21:11:52 +00:00
2 changed files with 52 additions and 0 deletions
Showing only changes of commit 12616dd9d3 - Show all commits

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 showTermDoc *bool
aggs []Aggregation aggs []Aggregation
order map[string]string order map[string]string
include []string
} }
// TermsAgg creates a new aggregation of type "terms". The method name includes // 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 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 // Map returns a map representation of the aggregation, thus implementing the
// Mappable interface. // Mappable interface.
func (agg *TermsAggregation) Map() map[string]interface{} { func (agg *TermsAggregation) Map() map[string]interface{} {
@ -81,6 +88,15 @@ func (agg *TermsAggregation) Map() map[string]interface{} {
innerMap["order"] = agg.order 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{}{ outerMap := map[string]interface{}{
"terms": innerMap, "terms": innerMap,
} }