Better error description if field(s) (statistical facet) and value_field (term_stats facet) are not a numeric field

This commit is contained in:
Martijn van Groningen 2013-04-10 11:11:52 +02:00
parent 6a3c53ef44
commit 637eeacb20
2 changed files with 13 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.core.NumberFieldMapper;
import org.elasticsearch.search.facet.FacetExecutor; import org.elasticsearch.search.facet.FacetExecutor;
import org.elasticsearch.search.facet.FacetParser; import org.elasticsearch.search.facet.FacetParser;
import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetPhaseExecutionException;
@ -104,6 +105,9 @@ public class StatisticalFacetParser extends AbstractComponent implements FacetPa
if (fieldMapper == null) { if (fieldMapper == null) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + fieldsNames[i] + "]"); throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + fieldsNames[i] + "]");
} }
if (!(fieldMapper instanceof NumberFieldMapper)) {
throw new FacetPhaseExecutionException(facetName, "field [" + field + "] isn't a number field, but a " + fieldMapper.fieldDataType().getType());
}
indexFieldDatas[i] = context.fieldData().getForField(fieldMapper); indexFieldDatas[i] = context.fieldData().getForField(fieldMapper);
} }
return new StatisticalFieldsFacetExecutor(indexFieldDatas, context); return new StatisticalFieldsFacetExecutor(indexFieldDatas, context);
@ -116,6 +120,9 @@ public class StatisticalFacetParser extends AbstractComponent implements FacetPa
if (fieldMapper == null) { if (fieldMapper == null) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + field + "]"); throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + field + "]");
} }
if (!(fieldMapper instanceof NumberFieldMapper)) {
throw new FacetPhaseExecutionException(facetName, "field [" + field + "] isn't a number field, but a " + fieldMapper.fieldDataType().getType());
}
IndexNumericFieldData indexFieldData = context.fieldData().getForField(fieldMapper); IndexNumericFieldData indexFieldData = context.fieldData().getForField(fieldMapper);
return new StatisticalFacetExecutor(indexFieldData, context); return new StatisticalFacetExecutor(indexFieldData, context);
} else { } else {

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.core.NumberFieldMapper;
import org.elasticsearch.script.SearchScript; import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.facet.FacetExecutor; import org.elasticsearch.search.facet.FacetExecutor;
import org.elasticsearch.search.facet.FacetParser; import org.elasticsearch.search.facet.FacetParser;
@ -121,7 +122,11 @@ public class TermsStatsFacetParser extends AbstractComponent implements FacetPar
IndexNumericFieldData valueIndexFieldData = null; IndexNumericFieldData valueIndexFieldData = null;
SearchScript valueScript = null; SearchScript valueScript = null;
if (valueField != null) { if (valueField != null) {
valueIndexFieldData = context.fieldData().getForField(context.smartNameFieldMapper(valueField)); FieldMapper fieldMapper = context.smartNameFieldMapper(valueField);
if (!(fieldMapper instanceof NumberFieldMapper)) {
throw new FacetPhaseExecutionException(facetName, "value_field [" + valueField + "] isn't a number field, but a " + fieldMapper.fieldDataType().getType());
}
valueIndexFieldData = context.fieldData().getForField(fieldMapper);
} else { } else {
valueScript = context.scriptService().search(context.lookup(), scriptLang, script, params); valueScript = context.scriptService().search(context.lookup(), scriptLang, script, params);
} }