Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a unit test for MultiTermsAggregator.buildEmptyAggregations (#7089) #7319

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,51 @@
import org.opensearch.common.network.InetAddresses;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.time.DateFormatter;
import org.opensearch.common.util.BigArrays;
import org.opensearch.common.util.MockPageCacheRecycler;
import org.opensearch.index.IndexService;
import org.opensearch.index.cache.IndexCache;
import org.opensearch.index.mapper.BooleanFieldMapper;
import org.opensearch.index.mapper.DateFieldMapper;
import org.opensearch.index.mapper.GeoPointFieldMapper;
import org.opensearch.index.mapper.IpFieldMapper;
import org.opensearch.index.mapper.KeywordFieldMapper;
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.mapper.NumberFieldMapper;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.indices.breaker.NoneCircuitBreakerService;
import org.opensearch.script.MockScriptEngine;
import org.opensearch.script.Script;
import org.opensearch.script.ScriptEngine;
import org.opensearch.script.ScriptModule;
import org.opensearch.script.ScriptService;
import org.opensearch.script.ScriptType;
import org.opensearch.search.DocValueFormat;
import org.opensearch.search.aggregations.AggregationBuilder;
import org.opensearch.search.aggregations.Aggregator;
import org.opensearch.search.aggregations.AggregatorFactories;
import org.opensearch.search.aggregations.AggregatorTestCase;
import org.opensearch.search.aggregations.BucketOrder;
import org.opensearch.search.aggregations.CardinalityUpperBound;
import org.opensearch.search.aggregations.InternalAggregation;
import org.opensearch.search.aggregations.metrics.InternalMax;
import org.opensearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.opensearch.search.aggregations.support.CoreValuesSourceType;
import org.opensearch.search.aggregations.support.MultiTermsValuesSourceConfig;
import org.opensearch.search.aggregations.support.ValueType;
import org.opensearch.search.aggregations.support.ValuesSourceType;
import org.opensearch.search.internal.SearchContext;
import org.opensearch.search.lookup.LeafDocLookup;
import org.opensearch.test.TestSearchContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;

Expand All @@ -68,8 +83,12 @@
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MultiTermsAggregatorTests extends AggregatorTestCase {
private static final String FIELD_NAME = "field";
Expand Down Expand Up @@ -852,6 +871,56 @@ public void testIncludeExclude() throws IOException {
);
}

public void testEmptyAggregations() throws IOException {
QueryShardContext queryShardContext = mock(QueryShardContext.class);
IndexShard indexShard = mock(IndexShard.class);
BigArrays bigArrays = new BigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService(), "");
IndexService indexService = mock(IndexService.class);
when(indexService.getShardOrNull(0)).thenReturn(indexShard);
IndexCache cache = mock(IndexCache.class);
when(cache.bitsetFilterCache()).thenReturn(null);
when(indexService.cache()).thenReturn(cache);
SearchContext context = new TestSearchContext(bigArrays, indexService);
when(indexService.newQueryShardContext(0, null, () -> 0L, null)).thenReturn(queryShardContext);
AggregatorFactories factories = AggregatorFactories.EMPTY;
boolean showTermDocCountError = true;
MultiTermsAggregator.InternalValuesSource internalValuesSources = mock(MultiTermsAggregator.InternalValuesSource.class);
DocValueFormat format = mock(DocValueFormat.class);
BucketOrder order = mock(BucketOrder.class);
Aggregator.SubAggCollectionMode collectMode = Aggregator.SubAggCollectionMode.BREADTH_FIRST;
TermsAggregator.BucketCountThresholds bucketCountThresholds = mock(TermsAggregator.BucketCountThresholds.class);
Aggregator parent = mock(Aggregator.class);
CardinalityUpperBound cardinality = CardinalityUpperBound.ONE;
Map<String, Object> metadata = new HashMap<>();
String k1 = UUID.randomUUID().toString();
String v1 = UUID.randomUUID().toString();
metadata.put(k1, v1);

MultiTermsAggregator mAgg = new MultiTermsAggregator(
AGG_NAME,
factories,
showTermDocCountError,
List.of(internalValuesSources),
List.of(format),
order,
collectMode,
bucketCountThresholds,
context,
parent,
cardinality,
metadata
);
InternalAggregation emptyAgg = mAgg.buildEmptyAggregation();

MatcherAssert.assertThat(emptyAgg.getName(), equalTo(AGG_NAME));
MatcherAssert.assertThat(emptyAgg, instanceOf(InternalMultiTerms.class));

InternalMultiTerms mt = (InternalMultiTerms) emptyAgg;
MatcherAssert.assertThat(mt.getMetadata().keySet(), contains(k1));
MatcherAssert.assertThat(mt.getMetadata().get(k1), equalTo(v1));
MatcherAssert.assertThat(mt.getBuckets(), empty());
}

private void testAggregation(
Query query,
List<MultiTermsValuesSourceConfig> terms,
Expand Down