diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java index 4f11818aa43aa..e32d8f6652ec2 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.search.aggregations.support; -import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationExecutionException; @@ -123,11 +122,10 @@ public AggregatorSupplier getAggregator(ValuesSourceConfig valuesSourceConfig, S aggregatorRegistry.get(aggregationName) ); if (supplier == null) { - // TODO: push building the description into ValuesSourceConfig - MappedFieldType fieldType = valuesSourceConfig.fieldContext().fieldType(); - String fieldDescription = fieldType.typeName(); - throw new IllegalArgumentException("Field [" + fieldType.name() + "] of type [" + fieldDescription + - "] is not supported for aggregation [" + aggregationName + "]"); } + throw new IllegalArgumentException( + valuesSourceConfig.getDescription() + " is not supported for aggregation [" + aggregationName + "]" + ); + } return supplier; } throw new AggregationExecutionException("Unregistered Aggregation [" + aggregationName + "]"); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistryTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistryTests.java new file mode 100644 index 0000000000000..f3f0372084253 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistryTests.java @@ -0,0 +1,68 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.support; + +import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.script.AggregationScript; +import org.elasticsearch.test.ESTestCase; +import org.mockito.Mockito; + +import java.util.Collections; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ValuesSourceRegistryTests extends ESTestCase { + + public void testAggregatorNotFoundException() { + final QueryShardContext queryShardContext = mock(QueryShardContext.class); + final AggregationScript.Factory mockAggScriptFactory = mock(AggregationScript.Factory.class); + when(mockAggScriptFactory.newFactory(Mockito.any(), Mockito.any())).thenReturn(mock(AggregationScript.LeafFactory.class)); + when(queryShardContext.compile(Mockito.any(), Mockito.any())).thenReturn(mockAggScriptFactory); + + ValuesSourceConfig fieldOnly = ValuesSourceConfig.resolve( + queryShardContext, + null, + "field", + null, + null, + null, + null, + CoreValuesSourceType.BYTES + ); + + ValuesSourceConfig scriptOnly = ValuesSourceConfig.resolve( + queryShardContext, + null, + null, + mockScript("fakeScript"), + null, + null, + null, + CoreValuesSourceType.BYTES + ); + ValuesSourceRegistry registry = new ValuesSourceRegistry( + Collections.singletonMap("bogus", Collections.emptyList()), + null); + expectThrows(IllegalArgumentException.class, () -> registry.getAggregator(fieldOnly, "bogus")); + expectThrows(IllegalArgumentException.class, () -> registry.getAggregator(scriptOnly, "bogus")); + } + +}