Skip to content

Commit 2910161

Browse files
committed
Add other buckets to Filters Aggregation
This commit adds the two fields `other_buckets` (true/false) and `other_buckets_key` (string) to the Filters Aggregation. For details, see the documentation at https://www.elastic.co/guide/en/elasticsearch/reference/7.8/search-aggregations-bucket-filters-aggregation.html#other-bucket Close #1350
1 parent 4d85ea4 commit 2910161

3 files changed

+51
-3
lines changed

search_aggs_bucket_filters.go

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import "errors"
1919
type FiltersAggregation struct {
2020
unnamedFilters []Query
2121
namedFilters map[string]Query
22+
otherBucket *bool
23+
otherBucketKey string
2224
subAggregations map[string]Aggregation
2325
meta map[string]interface{}
2426
}
@@ -55,6 +57,20 @@ func (a *FiltersAggregation) FilterWithName(name string, filter Query) *FiltersA
5557
return a
5658
}
5759

60+
// OtherBucket indicates whether to include a bucket for documents not
61+
// matching any filter.
62+
func (a *FiltersAggregation) OtherBucket(otherBucket bool) *FiltersAggregation {
63+
a.otherBucket = &otherBucket
64+
return a
65+
}
66+
67+
// OtherBucketKey sets the key to use for the bucket for documents not
68+
// matching any filter.
69+
func (a *FiltersAggregation) OtherBucketKey(key string) *FiltersAggregation {
70+
a.otherBucketKey = key
71+
return a
72+
}
73+
5874
// SubAggregation adds a sub-aggregation to this aggregation.
5975
func (a *FiltersAggregation) SubAggregation(name string, subAggregation Aggregation) *FiltersAggregation {
6076
a.subAggregations[name] = subAggregation
@@ -116,6 +132,13 @@ func (a *FiltersAggregation) Source() (interface{}, error) {
116132
filters["filters"] = dict
117133
}
118134

135+
if v := a.otherBucket; v != nil {
136+
filters["other_bucket"] = *v
137+
}
138+
if v := a.otherBucketKey; v != "" {
139+
filters["other_bucket_key"] = v
140+
}
141+
119142
// AggregationBuilder (SubAggregations)
120143
if len(a.subAggregations) > 0 {
121144
aggsMap := make(map[string]interface{})

search_aggs_bucket_filters_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ func TestFiltersAggregationWithKeyedAndNonKeyedFilters(t *testing.T) {
5959
}
6060
}
6161

62+
func TestFiltersAggregationWithOtherBuckets(t *testing.T) {
63+
agg := NewFiltersAggregation().
64+
FilterWithName("errors", NewMatchQuery("body", "error")).
65+
FilterWithName("warnings", NewMatchQuery("body", "warnings")).
66+
OtherBucket(true).
67+
OtherBucketKey("other")
68+
src, err := agg.Source()
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
data, err := json.Marshal(src)
73+
if err != nil {
74+
t.Fatalf("marshaling to JSON failed: %v", err)
75+
}
76+
got := string(data)
77+
expected := `{"filters":{"filters":{"errors":{"match":{"body":{"query":"error"}}},"warnings":{"match":{"body":{"query":"warnings"}}}},"other_bucket":true,"other_bucket_key":"other"}}`
78+
if got != expected {
79+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
80+
}
81+
}
82+
6283
func TestFiltersAggregationWithSubAggregation(t *testing.T) {
6384
avgPriceAgg := NewAvgAggregation().Field("price")
6485
f1 := NewRangeQuery("stock").Gt(0)

search_aggs_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ func TestAggs(t *testing.T) {
154154
builder = builder.Aggregation("centroid", geoCentroidAgg)
155155
// Unnamed filters
156156
countByUserAgg := NewFiltersAggregation().
157-
Filters(NewTermQuery("user", "olivere"), NewTermQuery("user", "sandrae"))
157+
Filters(NewTermQuery("user", "olivere"), NewTermQuery("user", "sandrae")).
158+
OtherBucket(true).OtherBucketKey("other")
158159
builder = builder.Aggregation("countByUser", countByUserAgg)
159160
// Named filters
160161
countByUserAgg2 := NewFiltersAggregation().
@@ -1172,8 +1173,8 @@ func TestAggs(t *testing.T) {
11721173
if agg == nil {
11731174
t.Fatalf("expected != nil; got: nil")
11741175
}
1175-
if len(agg.Buckets) != 2 {
1176-
t.Fatalf("expected %d; got: %d", 2, len(agg.Buckets))
1176+
if len(agg.Buckets) != 3 {
1177+
t.Fatalf("expected %d; got: %d", 3, len(agg.Buckets))
11771178
}
11781179
if len(agg.NamedBuckets) != 0 {
11791180
t.Fatalf("expected %d; got: %d", 0, len(agg.NamedBuckets))
@@ -1184,6 +1185,9 @@ func TestAggs(t *testing.T) {
11841185
if agg.Buckets[1].DocCount != 1 {
11851186
t.Errorf("expected %d; got: %d", 1, agg.Buckets[1].DocCount)
11861187
}
1188+
if agg.Buckets[1].DocCount != 0 {
1189+
t.Errorf("expected %d; got: %d", 0, agg.Buckets[2].DocCount)
1190+
}
11871191
}
11881192

11891193
// Filters agg "countByUser2" (named)

0 commit comments

Comments
 (0)