2021-03-14 07:50:22 +00:00
|
|
|
package esquery
|
|
|
|
|
|
|
|
type FilterAggregation struct {
|
|
|
|
name string
|
|
|
|
filter Mappable
|
|
|
|
aggs []Aggregation
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:30:33 +00:00
|
|
|
// FilterAgg creates a new aggregation of type "filter". The method name includes
|
2021-03-14 07:50:22 +00:00
|
|
|
// the "Agg" suffix to prevent conflict with the "filter" query.
|
|
|
|
func FilterAgg(name string, filter Mappable) *FilterAggregation {
|
|
|
|
return &FilterAggregation{
|
|
|
|
name: name,
|
|
|
|
filter: filter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the aggregation.
|
|
|
|
func (agg *FilterAggregation) Name() string {
|
|
|
|
return agg.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter sets the filter items
|
|
|
|
func (agg *FilterAggregation) Filter(filter Mappable) *FilterAggregation {
|
|
|
|
agg.filter = filter
|
|
|
|
return agg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aggs sets sub-aggregations for the aggregation.
|
|
|
|
func (agg *FilterAggregation) Aggs(aggs ...Aggregation) *FilterAggregation {
|
|
|
|
agg.aggs = aggs
|
|
|
|
return agg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (agg *FilterAggregation) Map() map[string]interface{} {
|
|
|
|
outerMap := map[string]interface{}{
|
|
|
|
"filter": agg.filter.Map(),
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|