|
28 | 28 | import org.opensearch.common.network.InetAddresses;
|
29 | 29 | import org.opensearch.common.settings.Settings;
|
30 | 30 | import org.opensearch.common.time.DateFormatter;
|
| 31 | +import org.opensearch.common.util.BigArrays; |
| 32 | +import org.opensearch.common.util.MockPageCacheRecycler; |
| 33 | +import org.opensearch.index.IndexService; |
| 34 | +import org.opensearch.index.cache.IndexCache; |
31 | 35 | import org.opensearch.index.mapper.BooleanFieldMapper;
|
32 | 36 | import org.opensearch.index.mapper.DateFieldMapper;
|
33 | 37 | import org.opensearch.index.mapper.GeoPointFieldMapper;
|
34 | 38 | import org.opensearch.index.mapper.IpFieldMapper;
|
35 | 39 | import org.opensearch.index.mapper.KeywordFieldMapper;
|
36 | 40 | import org.opensearch.index.mapper.MappedFieldType;
|
37 | 41 | import org.opensearch.index.mapper.NumberFieldMapper;
|
| 42 | +import org.opensearch.index.query.QueryShardContext; |
| 43 | +import org.opensearch.index.shard.IndexShard; |
| 44 | +import org.opensearch.indices.breaker.NoneCircuitBreakerService; |
38 | 45 | import org.opensearch.script.MockScriptEngine;
|
39 | 46 | import org.opensearch.script.Script;
|
40 | 47 | import org.opensearch.script.ScriptEngine;
|
41 | 48 | import org.opensearch.script.ScriptModule;
|
42 | 49 | import org.opensearch.script.ScriptService;
|
43 | 50 | import org.opensearch.script.ScriptType;
|
| 51 | +import org.opensearch.search.DocValueFormat; |
44 | 52 | import org.opensearch.search.aggregations.AggregationBuilder;
|
| 53 | +import org.opensearch.search.aggregations.Aggregator; |
| 54 | +import org.opensearch.search.aggregations.AggregatorFactories; |
45 | 55 | import org.opensearch.search.aggregations.AggregatorTestCase;
|
46 | 56 | import org.opensearch.search.aggregations.BucketOrder;
|
| 57 | +import org.opensearch.search.aggregations.CardinalityUpperBound; |
| 58 | +import org.opensearch.search.aggregations.InternalAggregation; |
47 | 59 | import org.opensearch.search.aggregations.metrics.InternalMax;
|
48 | 60 | import org.opensearch.search.aggregations.metrics.MaxAggregationBuilder;
|
49 | 61 | import org.opensearch.search.aggregations.support.CoreValuesSourceType;
|
50 | 62 | import org.opensearch.search.aggregations.support.MultiTermsValuesSourceConfig;
|
51 | 63 | import org.opensearch.search.aggregations.support.ValueType;
|
52 | 64 | import org.opensearch.search.aggregations.support.ValuesSourceType;
|
| 65 | +import org.opensearch.search.internal.SearchContext; |
53 | 66 | import org.opensearch.search.lookup.LeafDocLookup;
|
| 67 | +import org.opensearch.test.TestSearchContext; |
54 | 68 |
|
55 | 69 | import java.io.IOException;
|
56 | 70 | import java.util.ArrayList;
|
57 | 71 | import java.util.Collections;
|
58 | 72 | import java.util.HashMap;
|
59 | 73 | import java.util.List;
|
60 | 74 | import java.util.Map;
|
| 75 | +import java.util.UUID; |
61 | 76 | import java.util.function.Consumer;
|
62 | 77 | import java.util.function.Function;
|
63 | 78 |
|
|
68 | 83 | import static java.util.stream.Collectors.toList;
|
69 | 84 | import static org.hamcrest.Matchers.closeTo;
|
70 | 85 | import static org.hamcrest.Matchers.contains;
|
| 86 | +import static org.hamcrest.Matchers.empty; |
71 | 87 | import static org.hamcrest.Matchers.equalTo;
|
72 | 88 | import static org.hamcrest.Matchers.hasSize;
|
| 89 | +import static org.hamcrest.Matchers.instanceOf; |
| 90 | +import static org.mockito.Mockito.mock; |
| 91 | +import static org.mockito.Mockito.when; |
73 | 92 |
|
74 | 93 | public class MultiTermsAggregatorTests extends AggregatorTestCase {
|
75 | 94 | private static final String FIELD_NAME = "field";
|
@@ -852,6 +871,56 @@ public void testIncludeExclude() throws IOException {
|
852 | 871 | );
|
853 | 872 | }
|
854 | 873 |
|
| 874 | + public void testEmptyAggregations() throws IOException { |
| 875 | + QueryShardContext queryShardContext = mock(QueryShardContext.class); |
| 876 | + IndexShard indexShard = mock(IndexShard.class); |
| 877 | + BigArrays bigArrays = new BigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService(), ""); |
| 878 | + IndexService indexService = mock(IndexService.class); |
| 879 | + when(indexService.getShardOrNull(0)).thenReturn(indexShard); |
| 880 | + IndexCache cache = mock(IndexCache.class); |
| 881 | + when(cache.bitsetFilterCache()).thenReturn(null); |
| 882 | + when(indexService.cache()).thenReturn(cache); |
| 883 | + SearchContext context = new TestSearchContext(bigArrays, indexService); |
| 884 | + when(indexService.newQueryShardContext(0, null, () -> 0L, null)).thenReturn(queryShardContext); |
| 885 | + AggregatorFactories factories = AggregatorFactories.EMPTY; |
| 886 | + boolean showTermDocCountError = true; |
| 887 | + MultiTermsAggregator.InternalValuesSource internalValuesSources = mock(MultiTermsAggregator.InternalValuesSource.class); |
| 888 | + DocValueFormat format = mock(DocValueFormat.class); |
| 889 | + BucketOrder order = mock(BucketOrder.class); |
| 890 | + Aggregator.SubAggCollectionMode collectMode = Aggregator.SubAggCollectionMode.BREADTH_FIRST; |
| 891 | + TermsAggregator.BucketCountThresholds bucketCountThresholds = mock(TermsAggregator.BucketCountThresholds.class); |
| 892 | + Aggregator parent = mock(Aggregator.class); |
| 893 | + CardinalityUpperBound cardinality = CardinalityUpperBound.ONE; |
| 894 | + Map<String, Object> metadata = new HashMap<>(); |
| 895 | + String k1 = UUID.randomUUID().toString(); |
| 896 | + String v1 = UUID.randomUUID().toString(); |
| 897 | + metadata.put(k1, v1); |
| 898 | + |
| 899 | + MultiTermsAggregator mAgg = new MultiTermsAggregator( |
| 900 | + AGG_NAME, |
| 901 | + factories, |
| 902 | + showTermDocCountError, |
| 903 | + List.of(internalValuesSources), |
| 904 | + List.of(format), |
| 905 | + order, |
| 906 | + collectMode, |
| 907 | + bucketCountThresholds, |
| 908 | + context, |
| 909 | + parent, |
| 910 | + cardinality, |
| 911 | + metadata |
| 912 | + ); |
| 913 | + InternalAggregation emptyAgg = mAgg.buildEmptyAggregation(); |
| 914 | + |
| 915 | + MatcherAssert.assertThat(emptyAgg.getName(), equalTo(AGG_NAME)); |
| 916 | + MatcherAssert.assertThat(emptyAgg, instanceOf(InternalMultiTerms.class)); |
| 917 | + |
| 918 | + InternalMultiTerms mt = (InternalMultiTerms) emptyAgg; |
| 919 | + MatcherAssert.assertThat(mt.getMetadata().keySet(), contains(k1)); |
| 920 | + MatcherAssert.assertThat(mt.getMetadata().get(k1), equalTo(v1)); |
| 921 | + MatcherAssert.assertThat(mt.getBuckets(), empty()); |
| 922 | + } |
| 923 | + |
855 | 924 | private void testAggregation(
|
856 | 925 | Query query,
|
857 | 926 | List<MultiTermsValuesSourceConfig> terms,
|
|
0 commit comments