Search in sources :

Example 1 with AggregatorFactory

use of org.opensearch.search.aggregations.AggregatorFactory in project OpenSearch by opensearch-project.

the class CompositeAggregationBuilder method doBuild.

@Override
protected AggregatorFactory doBuild(QueryShardContext queryShardContext, AggregatorFactory parent, AggregatorFactories.Builder subfactoriesBuilder) throws IOException {
    AggregatorFactory invalid = checkParentIsNullOrNested(parent);
    if (invalid != null) {
        throw new IllegalArgumentException("[composite] aggregation cannot be used with a parent aggregation of" + " type: [" + invalid.getClass().getSimpleName() + "]");
    }
    CompositeValuesSourceConfig[] configs = new CompositeValuesSourceConfig[sources.size()];
    for (int i = 0; i < configs.length; i++) {
        configs[i] = sources.get(i).build(queryShardContext);
        if (configs[i].valuesSource().needsScores()) {
            throw new IllegalArgumentException("[sources] cannot access _score");
        }
    }
    final CompositeKey afterKey;
    if (after != null) {
        if (after.size() != configs.length) {
            throw new IllegalArgumentException("[after] has " + after.size() + " value(s) but [sources] has " + sources.size());
        }
        Comparable[] values = new Comparable[sources.size()];
        for (int i = 0; i < sources.size(); i++) {
            String sourceName = sources.get(i).name();
            if (after.containsKey(sourceName) == false) {
                throw new IllegalArgumentException("Missing value for [after." + sources.get(i).name() + "]");
            }
            Object obj = after.get(sourceName);
            if (configs[i].missingBucket() && obj == null) {
                values[i] = null;
            } else if (obj instanceof Comparable) {
                values[i] = (Comparable) obj;
            } else {
                throw new IllegalArgumentException("Invalid value for [after." + sources.get(i).name() + "], expected comparable, got [" + (obj == null ? "null" : obj.getClass().getSimpleName()) + "]");
            }
        }
        afterKey = new CompositeKey(values);
    } else {
        afterKey = null;
    }
    return new CompositeAggregationFactory(name, queryShardContext, parent, subfactoriesBuilder, metadata, size, configs, afterKey);
}
Also used : AggregatorFactory(org.opensearch.search.aggregations.AggregatorFactory) NestedAggregatorFactory(org.opensearch.search.aggregations.bucket.nested.NestedAggregatorFactory)

Example 2 with AggregatorFactory

use of org.opensearch.search.aggregations.AggregatorFactory in project OpenSearch by opensearch-project.

the class AdjacencyMatrixAggregationBuilderTests method testFilterSizeLimitation.

public void testFilterSizeLimitation() throws Exception {
    // filter size grater than max size should thrown a exception
    QueryShardContext queryShardContext = mock(QueryShardContext.class);
    IndexShard indexShard = mock(IndexShard.class);
    Settings settings = Settings.builder().put("index.max_adjacency_matrix_filters", 2).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2).build();
    IndexMetadata indexMetadata = IndexMetadata.builder("index").settings(settings).build();
    IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY);
    when(indexShard.indexSettings()).thenReturn(indexSettings);
    when(queryShardContext.getIndexSettings()).thenReturn(indexSettings);
    SearchContext context = new TestSearchContext(queryShardContext, indexShard);
    Map<String, QueryBuilder> filters = new HashMap<>(3);
    for (int i = 0; i < 3; i++) {
        QueryBuilder queryBuilder = mock(QueryBuilder.class);
        // return builder itself to skip rewrite
        when(queryBuilder.rewrite(queryShardContext)).thenReturn(queryBuilder);
        filters.put("filter" + i, queryBuilder);
    }
    AdjacencyMatrixAggregationBuilder builder = new AdjacencyMatrixAggregationBuilder("dummy", filters);
    IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> builder.doBuild(context.getQueryShardContext(), null, new AggregatorFactories.Builder()));
    assertThat(ex.getMessage(), equalTo("Number of filters is too large, must be less than or equal to: [2] but was [3]." + "This limit can be set by changing the [" + IndexSettings.MAX_ADJACENCY_MATRIX_FILTERS_SETTING.getKey() + "] index level setting."));
    // filter size not grater than max size should return an instance of AdjacencyMatrixAggregatorFactory
    Map<String, QueryBuilder> emptyFilters = Collections.emptyMap();
    AdjacencyMatrixAggregationBuilder aggregationBuilder = new AdjacencyMatrixAggregationBuilder("dummy", emptyFilters);
    AggregatorFactory factory = aggregationBuilder.doBuild(context.getQueryShardContext(), null, new AggregatorFactories.Builder());
    assertThat(factory instanceof AdjacencyMatrixAggregatorFactory, is(true));
    assertThat(factory.name(), equalTo("dummy"));
    assertWarnings("[index.max_adjacency_matrix_filters] setting was deprecated in OpenSearch and will be " + "removed in a future release! See the breaking changes documentation for the next major version.");
}
Also used : HashMap(java.util.HashMap) IndexShard(org.opensearch.index.shard.IndexShard) IndexSettings(org.opensearch.index.IndexSettings) QueryBuilder(org.opensearch.index.query.QueryBuilder) SearchContext(org.opensearch.search.internal.SearchContext) TestSearchContext(org.opensearch.test.TestSearchContext) QueryBuilder(org.opensearch.index.query.QueryBuilder) AggregatorFactory(org.opensearch.search.aggregations.AggregatorFactory) TestSearchContext(org.opensearch.test.TestSearchContext) QueryShardContext(org.opensearch.index.query.QueryShardContext) AggregatorFactories(org.opensearch.search.aggregations.AggregatorFactories) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Aggregations

AggregatorFactory (org.opensearch.search.aggregations.AggregatorFactory)2 HashMap (java.util.HashMap)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 Settings (org.opensearch.common.settings.Settings)1 IndexSettings (org.opensearch.index.IndexSettings)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1 QueryShardContext (org.opensearch.index.query.QueryShardContext)1 IndexShard (org.opensearch.index.shard.IndexShard)1 AggregatorFactories (org.opensearch.search.aggregations.AggregatorFactories)1 NestedAggregatorFactory (org.opensearch.search.aggregations.bucket.nested.NestedAggregatorFactory)1 SearchContext (org.opensearch.search.internal.SearchContext)1 TestSearchContext (org.opensearch.test.TestSearchContext)1