Search in sources :

Example 11 with AggregationExecutionException

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

the class WeightedAvgAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSources == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final BigArrays bigArrays = context.bigArrays();
    final SortedNumericDoubleValues docValues = valuesSources.getField(VALUE_FIELD.getPreferredName(), ctx);
    final SortedNumericDoubleValues docWeights = valuesSources.getField(WEIGHT_FIELD.getPreferredName(), ctx);
    final CompensatedSum compensatedValueSum = new CompensatedSum(0, 0);
    final CompensatedSum compensatedWeightSum = new CompensatedSum(0, 0);
    return new LeafBucketCollectorBase(sub, docValues) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            weights = bigArrays.grow(weights, bucket + 1);
            valueSums = bigArrays.grow(valueSums, bucket + 1);
            valueCompensations = bigArrays.grow(valueCompensations, bucket + 1);
            weightCompensations = bigArrays.grow(weightCompensations, bucket + 1);
            if (docValues.advanceExact(doc) && docWeights.advanceExact(doc)) {
                if (docWeights.docValueCount() > 1) {
                    throw new AggregationExecutionException("Encountered more than one weight for a " + "single document. Use a script to combine multiple weights-per-doc into a single value.");
                }
                // a real weight or a `missing` weight
                assert docWeights.docValueCount() == 1;
                final double weight = docWeights.nextValue();
                final int numValues = docValues.docValueCount();
                assert numValues > 0;
                double valueSum = valueSums.get(bucket);
                double valueCompensation = valueCompensations.get(bucket);
                compensatedValueSum.reset(valueSum, valueCompensation);
                double weightSum = weights.get(bucket);
                double weightCompensation = weightCompensations.get(bucket);
                compensatedWeightSum.reset(weightSum, weightCompensation);
                for (int i = 0; i < numValues; i++) {
                    compensatedValueSum.add(docValues.nextValue() * weight);
                    compensatedWeightSum.add(weight);
                }
                valueSums.set(bucket, compensatedValueSum.value());
                valueCompensations.set(bucket, compensatedValueSum.delta());
                weights.set(bucket, compensatedWeightSum.value());
                weightCompensations.set(bucket, compensatedWeightSum.delta());
            }
        }
    };
}
Also used : BigArrays(org.opensearch.common.util.BigArrays) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException)

Example 12 with AggregationExecutionException

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

the class BucketHelpersTests method testReturnsObjectArray.

public void testReturnsObjectArray() {
    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 new Object[0];
        }
    };
    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, got: [Object[]] at aggregation [foo]"));
}
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