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());
}
}
};
}
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]"));
}
Aggregations