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