Throw IllegalArgumentException instead of ClassCastException,

Let stats aggregation returns 400 error when performed over an invalid field

closes #12842
This commit is contained in:
xuzha 2015-08-15 15:42:03 -07:00
parent 9b08f4012e
commit 062e038360
1 changed files with 12 additions and 0 deletions

View File

@ -156,6 +156,12 @@ public class AggregationContext {
}
private ValuesSource.Numeric numericField(ValuesSourceConfig<?> config) throws IOException {
if (!(config.fieldContext.indexFieldData() instanceof IndexNumericFieldData)) {
throw new IllegalArgumentException("Expected numeric type on field [" + config.fieldContext.field() +
"], but got [" + config.fieldContext.fieldType().typeName() + "]");
}
ValuesSource.Numeric dataSource = new ValuesSource.Numeric.FieldData((IndexNumericFieldData) config.fieldContext.indexFieldData());
if (config.script != null) {
dataSource = new ValuesSource.Numeric.WithScript(dataSource, config.script);
@ -184,6 +190,12 @@ public class AggregationContext {
}
private ValuesSource.GeoPoint geoPointField(ValuesSourceConfig<?> config) throws IOException {
if (!(config.fieldContext.indexFieldData() instanceof IndexGeoPointFieldData)) {
throw new IllegalArgumentException("Expected geo_point type on field [" + config.fieldContext.field() +
"], but got [" + config.fieldContext.fieldType().typeName() + "]");
}
return new ValuesSource.GeoPoint.Fielddata((IndexGeoPointFieldData) config.fieldContext.indexFieldData());
}