Search in sources :

Example 1 with AggregationExecutionException

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

the class InternalMappedRareTerms method reduce.

@Override
public InternalAggregation reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    Map<Object, List<B>> buckets = new HashMap<>();
    InternalRareTerms<A, B> referenceTerms = null;
    SetBackedScalingCuckooFilter filter = null;
    for (InternalAggregation aggregation : aggregations) {
        // and save some type casting headaches later.
        if (aggregation.isMapped() == false) {
            continue;
        }
        @SuppressWarnings("unchecked") InternalRareTerms<A, B> terms = (InternalRareTerms<A, B>) aggregation;
        if (referenceTerms == null && aggregation.getClass().equals(UnmappedRareTerms.class) == false) {
            referenceTerms = terms;
        }
        if (referenceTerms != null && referenceTerms.getClass().equals(terms.getClass()) == false && terms.getClass().equals(UnmappedRareTerms.class) == false) {
            // is of different types in different indices.
            throw new AggregationExecutionException("Merging/Reducing the aggregations failed when computing the aggregation [" + referenceTerms.getName() + "] because the field you gave in the aggregation query existed as two different " + "types in two different indices");
        }
        for (B bucket : terms.getBuckets()) {
            List<B> bucketList = buckets.computeIfAbsent(bucket.getKey(), k -> new ArrayList<>());
            bucketList.add(bucket);
        }
        SetBackedScalingCuckooFilter otherFilter = ((InternalMappedRareTerms) aggregation).getFilter();
        if (filter == null) {
            filter = new SetBackedScalingCuckooFilter(otherFilter);
        } else {
            filter.merge(otherFilter);
        }
    }
    final List<B> rare = new ArrayList<>();
    for (List<B> sameTermBuckets : buckets.values()) {
        final B b = reduceBucket(sameTermBuckets, reduceContext);
        if ((b.getDocCount() <= maxDocCount && containsTerm(filter, b) == false)) {
            rare.add(b);
            reduceContext.consumeBucketsAndMaybeBreak(1);
        } else if (b.getDocCount() > maxDocCount) {
            // this term has gone over threshold while merging, so add it to the filter.
            // Note this may happen during incremental reductions too
            addToFilter(filter, b);
        }
    }
    CollectionUtil.introSort(rare, order.comparator());
    return createWithFilter(name, rare, filter);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) SetBackedScalingCuckooFilter(org.opensearch.common.util.SetBackedScalingCuckooFilter) ArrayList(java.util.ArrayList) List(java.util.List) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException)

Example 2 with AggregationExecutionException

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

the class TermsAggregatorTests method testOrderByPipelineAggregation.

public void testOrderByPipelineAggregation() throws Exception {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                BucketScriptPipelineAggregationBuilder bucketScriptAgg = bucketScript("script", new Script("2.718"));
                TermsAggregationBuilder termsAgg = terms("terms").field("field").userValueTypeHint(ValueType.STRING).order(BucketOrder.aggregation("script", true)).subAggregation(bucketScriptAgg);
                MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("field");
                AggregationExecutionException e = expectThrows(AggregationExecutionException.class, () -> createAggregator(termsAgg, indexSearcher, fieldType));
                assertEquals("Invalid aggregation order path [script]. The provided aggregation [script] " + "either does not exist, or is a pipeline aggregation and cannot be used to sort the buckets.", e.getMessage());
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) PipelineAggregatorBuilders.bucketScript(org.opensearch.search.aggregations.PipelineAggregatorBuilders.bucketScript) Script(org.opensearch.script.Script) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 3 with AggregationExecutionException

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

the class NumericTermsAggregatorTests method testBadIncludeExclude.

public void testBadIncludeExclude() throws IOException {
    IncludeExclude includeExclude = new IncludeExclude(new RegExp("foo"), null);
    // Numerics don't support any regex include/exclude, so should fail no matter what we do
    AggregationExecutionException e = expectThrows(AggregationExecutionException.class, () -> testSearchCase(new MatchNoDocsQuery(), dataset, aggregation -> aggregation.field(LONG_FIELD).includeExclude(includeExclude).format("yyyy-MM-dd"), agg -> fail("test should have failed with exception"), null));
    assertThat(e.getMessage(), equalTo("Aggregation [_name] cannot support regular expression style " + "include/exclude settings as they can only be applied to string fields. Use an array of numeric " + "values for include/exclude clauses used to filter numeric fields"));
    e = expectThrows(AggregationExecutionException.class, () -> testSearchCase(new MatchNoDocsQuery(), dataset, aggregation -> aggregation.field(LONG_FIELD).includeExclude(includeExclude).format("yyyy-MM-dd"), agg -> fail("test should have failed with exception"), // with type hint
    ValueType.NUMERIC));
    assertThat(e.getMessage(), equalTo("Aggregation [_name] cannot support regular expression style " + "include/exclude settings as they can only be applied to string fields. Use an array of numeric " + "values for include/exclude clauses used to filter numeric fields"));
}
Also used : Query(org.apache.lucene.search.Query) ValueType(org.opensearch.search.aggregations.support.ValueType) LongPoint(org.apache.lucene.document.LongPoint) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) Document(org.apache.lucene.document.Document) List(java.util.List) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException) Directory(org.apache.lucene.store.Directory) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) RegExp(org.apache.lucene.util.automaton.RegExp) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) RegExp(org.apache.lucene.util.automaton.RegExp) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException)

Example 4 with AggregationExecutionException

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

the class BinaryTermsAggregatorTests method testBadIncludeExclude.

public void testBadIncludeExclude() throws IOException {
    IncludeExclude includeExclude = new IncludeExclude(new RegExp("foo"), null);
    // Make sure the include/exclude fails regardless of how the user tries to type hint the agg
    AggregationExecutionException e = expectThrows(AggregationExecutionException.class, () -> testSearchCase(new MatchNoDocsQuery(), dataset, aggregation -> aggregation.field(BINARY_FIELD).includeExclude(includeExclude).format("yyyy-MM-dd"), agg -> fail("test should have failed with exception"), // default, no hint
    null));
    assertThat(e.getMessage(), equalTo("Aggregation [_name] cannot support regular expression style include/exclude settings as " + "they can only be applied to string fields. Use an array of values for include/exclude clauses"));
    e = expectThrows(AggregationExecutionException.class, () -> testSearchCase(new MatchNoDocsQuery(), dataset, aggregation -> aggregation.field(BINARY_FIELD).includeExclude(includeExclude).format("yyyy-MM-dd"), agg -> fail("test should have failed with exception"), // string type hint
    ValueType.STRING));
    assertThat(e.getMessage(), equalTo("Aggregation [_name] cannot support regular expression style include/exclude settings as " + "they can only be applied to string fields. Use an array of values for include/exclude clauses"));
}
Also used : Query(org.apache.lucene.search.Query) ValueType(org.opensearch.search.aggregations.support.ValueType) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DocValueFormat(org.opensearch.search.DocValueFormat) DirectoryReader(org.apache.lucene.index.DirectoryReader) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BinaryFieldMapper(org.opensearch.index.mapper.BinaryFieldMapper) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) Document(org.apache.lucene.document.Document) List(java.util.List) Numbers(org.opensearch.common.Numbers) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException) Directory(org.apache.lucene.store.Directory) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) RegExp(org.apache.lucene.util.automaton.RegExp) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) RegExp(org.apache.lucene.util.automaton.RegExp) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException)

Example 5 with AggregationExecutionException

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

the class BucketHelpersTests method testReturnMultiValueObject.

public void testReturnMultiValueObject() {
    MultiBucketsAggregation agg = new MultiBucketsAggregation() {

        @Override
        public List<? extends Bucket> getBuckets() {
            return null;
        }

        @Override
        public String getName() {
            return "foo";
        }

        @Override
        public String getType() {
            return null;
        }

        @Override
        public Map<String, Object> getMetadata() {
            return null;
        }

        @Override
        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
            return null;
        }
    };
    InternalMultiBucketAggregation.InternalBucket bucket = new InternalMultiBucketAggregation.InternalBucket() {

        @Override
        public void writeTo(StreamOutput out) throws IOException {
        }

        @Override
        public Object getKey() {
            return null;
        }

        @Override
        public String getKeyAsString() {
            return null;
        }

        @Override
        public long getDocCount() {
            return 0;
        }

        @Override
        public Aggregations getAggregations() {
            return null;
        }

        @Override
        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
            return null;
        }

        @Override
        public Object getProperty(String containingAggName, List<String> path) {
            return mock(InternalTDigestPercentiles.class);
        }
    };
    AggregationExecutionException e = expectThrows(AggregationExecutionException.class, () -> BucketHelpers.resolveBucketValue(agg, bucket, "foo>bar", BucketHelpers.GapPolicy.SKIP));
    assertThat(e.getMessage(), equalTo("buckets_path must reference either a number value or a single value numeric " + "metric aggregation, but [foo] contains multiple values. Please specify which to use."));
}
Also used : MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) List(java.util.List) InternalMultiBucketAggregation(org.opensearch.search.aggregations.InternalMultiBucketAggregation) StreamOutput(org.opensearch.common.io.stream.StreamOutput) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

AggregationExecutionException (org.opensearch.search.aggregations.AggregationExecutionException)12 List (java.util.List)5 IndexReader (org.apache.lucene.index.IndexReader)4 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 Directory (org.apache.lucene.store.Directory)4 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Consumer (java.util.function.Consumer)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Query (org.apache.lucene.search.Query)3 Matchers.equalTo (org.hamcrest.Matchers.equalTo)3 AggregatorTestCase (org.opensearch.search.aggregations.AggregatorTestCase)3 Document (org.apache.lucene.document.Document)2 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 RegExp (org.apache.lucene.util.automaton.RegExp)2 StreamOutput (org.opensearch.common.io.stream.StreamOutput)2