|
81 | 81 | import static org.hamcrest.Matchers.hasSize;
|
82 | 82 | import static org.hamcrest.Matchers.instanceOf;
|
83 | 83 | import static org.hamcrest.Matchers.lessThan;
|
| 84 | +import static org.hamcrest.Matchers.nullValue; |
84 | 85 | import static org.mockito.Mockito.mock;
|
85 | 86 |
|
86 | 87 | public class FiltersAggregatorTests extends AggregatorTestCase {
|
@@ -115,6 +116,35 @@ public void testEmpty() throws Exception {
|
115 | 116 | directory.close();
|
116 | 117 | }
|
117 | 118 |
|
| 119 | + public void testBuildEmpty() throws IOException { |
| 120 | + int numFilters = randomIntBetween(1, 10); |
| 121 | + QueryBuilder[] filters = new QueryBuilder[numFilters]; |
| 122 | + for (int i = 0; i < filters.length; i++) { |
| 123 | + filters[i] = QueryBuilders.termQuery("field", randomAlphaOfLength(5)); |
| 124 | + } |
| 125 | + FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", filters); |
| 126 | + boolean askForOtherBucket = true; |
| 127 | + if (askForOtherBucket) { |
| 128 | + builder.otherBucket(true).otherBucketKey("other"); |
| 129 | + } |
| 130 | + withAggregator( |
| 131 | + builder, |
| 132 | + new MatchAllDocsQuery(), |
| 133 | + iw -> {}, |
| 134 | + (searcher, aggregator) -> { |
| 135 | + InternalFilters result = (InternalFilters) aggregator.buildEmptyAggregation(); |
| 136 | + for (int i = 0; i < filters.length; i++) { |
| 137 | + assertThat(result.getBucketByKey(String.valueOf(i)).getDocCount(), equalTo(0L)); |
| 138 | + } |
| 139 | + if (askForOtherBucket) { |
| 140 | + assertThat(result.getBucketByKey("other").getDocCount(), equalTo(0L)); |
| 141 | + } else { |
| 142 | + assertThat(result.getBucketByKey("other"), nullValue()); |
| 143 | + } |
| 144 | + } |
| 145 | + ); |
| 146 | + } |
| 147 | + |
118 | 148 | public void testNoFilters() throws IOException {
|
119 | 149 | testCase(new FiltersAggregationBuilder("test", new KeyedFilter[0]), new MatchAllDocsQuery(), iw -> {
|
120 | 150 | iw.addDocument(List.of());
|
|
0 commit comments