From 062e038360ba93a7802e32c182eeaa18dbc6e787 Mon Sep 17 00:00:00 2001 From: xuzha Date: Sat, 15 Aug 2015 15:42:03 -0700 Subject: [PATCH] Throw IllegalArgumentException instead of ClassCastException, Let stats aggregation returns 400 error when performed over an invalid field closes #12842 --- .../aggregations/support/AggregationContext.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java b/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java index b7c3f33bf5b..ee917824f5c 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java @@ -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()); }