@@ -11,7 +11,8 @@ namespace Tests.Aggregations.Bucket.Filters
11
11
{
12
12
/**
13
13
* Defines a multi bucket aggregations where each bucket is associated with a filter.
14
- * Each bucket will collect all documents that match its associated filter.
14
+ * Each bucket will collect all documents that match its associated filter. For documents
15
+ * that do not match any filter, these will be collected in the other bucket.
15
16
*
16
17
* Be sure to read the elasticsearch documentation {ref}/search-aggregations-bucket-filters-aggregation.html[on this subject here]
17
18
*/
@@ -29,6 +30,8 @@ public FiltersAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
29
30
{
30
31
filters = new
31
32
{
33
+ other_bucket = true ,
34
+ other_bucket_key = "other_states_of_being" ,
32
35
filters = new
33
36
{
34
37
belly_up = new { term = new { state = new { value = "BellyUp" } } } ,
@@ -47,6 +50,8 @@ public FiltersAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
47
50
protected override Func < SearchDescriptor < Project > , ISearchRequest > Fluent => s => s
48
51
. Aggregations ( aggs => aggs
49
52
. Filters ( "projects_by_state" , agg => agg
53
+ . OtherBucket ( )
54
+ . OtherBucketKey ( "other_states_of_being" )
50
55
. NamedFilters ( filters => filters
51
56
. Filter ( "belly_up" , f => f . Term ( p => p . State , StateOfBeing . BellyUp ) )
52
57
. Filter ( "stable" , f => f . Term ( p => p . State , StateOfBeing . Stable ) )
@@ -63,6 +68,8 @@ public FiltersAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
63
68
{
64
69
Aggregations = new FiltersAggregation ( "projects_by_state" )
65
70
{
71
+ OtherBucket = true ,
72
+ OtherBucketKey = "other_states_of_being" ,
66
73
Filters = new NamedFiltersContainer
67
74
{
68
75
{ "belly_up" , Query < Project > . Term ( p=> p . State , StateOfBeing . BellyUp ) } ,
@@ -97,6 +104,9 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
97
104
namedResult . Should ( ) . NotBeNull ( ) ;
98
105
namedResult . DocCount . Should ( ) . BeGreaterThan ( 0 ) ;
99
106
107
+ namedResult = filterAgg . NamedBucket ( "other_states_of_being" ) ;
108
+ namedResult . Should ( ) . NotBeNull ( ) ;
109
+ namedResult . DocCount . Should ( ) . Be ( 0 ) ;
100
110
}
101
111
}
102
112
@@ -114,6 +124,7 @@ public AnonymousUsage(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) {
114
124
{
115
125
filters = new
116
126
{
127
+ other_bucket = true ,
117
128
filters = new [ ] {
118
129
new { term = new { state = new { value = "BellyUp" } } } ,
119
130
new { term = new { state = new { value = "Stable" } } } ,
@@ -131,6 +142,7 @@ public AnonymousUsage(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) {
131
142
protected override Func < SearchDescriptor < Project > , ISearchRequest > Fluent => s => s
132
143
. Aggregations ( aggs => aggs
133
144
. Filters ( "projects_by_state" , agg => agg
145
+ . OtherBucket ( )
134
146
. AnonymousFilters (
135
147
f => f . Term ( p => p . State , StateOfBeing . BellyUp ) ,
136
148
f => f . Term ( p => p . State , StateOfBeing . Stable ) ,
@@ -147,6 +159,7 @@ public AnonymousUsage(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) {
147
159
{
148
160
Aggregations = new FiltersAggregation ( "projects_by_state" )
149
161
{
162
+ OtherBucket = true ,
150
163
Filters = new List < QueryContainer >
151
164
{
152
165
Query < Project > . Term ( p=> p . State , StateOfBeing . BellyUp ) ,
@@ -169,11 +182,14 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
169
182
var filterAgg = response . Aggs . Filters ( "projects_by_state" ) ;
170
183
filterAgg . Should ( ) . NotBeNull ( ) ;
171
184
var results = filterAgg . AnonymousBuckets ( ) ;
172
- results . Count . Should ( ) . Be ( 3 ) ;
173
- foreach ( var singleBucket in results )
185
+ results . Count . Should ( ) . Be ( 4 ) ;
186
+
187
+ foreach ( var singleBucket in results . Take ( 3 ) )
174
188
{
175
189
singleBucket . DocCount . Should ( ) . BeGreaterThan ( 0 ) ;
176
190
}
191
+
192
+ results . Last ( ) . DocCount . Should ( ) . Be ( 0 ) ;
177
193
}
178
194
}
179
195
}
0 commit comments