use of org.apache.lucene.queryparser.flexible.core.QueryNodeParseException in project lucene-solr by apache.
the class PointRangeQueryNodeProcessor method postProcessNode.
@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
if (node instanceof TermRangeQueryNode) {
QueryConfigHandler config = getQueryConfigHandler();
if (config != null) {
TermRangeQueryNode termRangeNode = (TermRangeQueryNode) node;
FieldConfig fieldConfig = config.getFieldConfig(StringUtils.toString(termRangeNode.getField()));
if (fieldConfig != null) {
PointsConfig numericConfig = fieldConfig.get(ConfigurationKeys.POINTS_CONFIG);
if (numericConfig != null) {
FieldQueryNode lower = termRangeNode.getLowerBound();
FieldQueryNode upper = termRangeNode.getUpperBound();
String lowerText = lower.getTextAsString();
String upperText = upper.getTextAsString();
NumberFormat numberFormat = numericConfig.getNumberFormat();
Number lowerNumber = null, upperNumber = null;
if (lowerText.length() > 0) {
try {
lowerNumber = numberFormat.parse(lowerText);
} catch (ParseException e) {
throw new QueryNodeParseException(new MessageImpl(QueryParserMessages.COULD_NOT_PARSE_NUMBER, lower.getTextAsString(), numberFormat.getClass().getCanonicalName()), e);
}
}
if (upperText.length() > 0) {
try {
upperNumber = numberFormat.parse(upperText);
} catch (ParseException e) {
throw new QueryNodeParseException(new MessageImpl(QueryParserMessages.COULD_NOT_PARSE_NUMBER, upper.getTextAsString(), numberFormat.getClass().getCanonicalName()), e);
}
}
if (Integer.class.equals(numericConfig.getType())) {
if (upperNumber != null)
upperNumber = upperNumber.intValue();
if (lowerNumber != null)
lowerNumber = lowerNumber.intValue();
} else if (Long.class.equals(numericConfig.getType())) {
if (upperNumber != null)
upperNumber = upperNumber.longValue();
if (lowerNumber != null)
lowerNumber = lowerNumber.longValue();
} else if (Double.class.equals(numericConfig.getType())) {
if (upperNumber != null)
upperNumber = upperNumber.doubleValue();
if (lowerNumber != null)
lowerNumber = lowerNumber.doubleValue();
} else if (Float.class.equals(numericConfig.getType())) {
if (upperNumber != null)
upperNumber = upperNumber.floatValue();
if (lowerNumber != null)
lowerNumber = lowerNumber.floatValue();
}
PointQueryNode lowerNode = new PointQueryNode(termRangeNode.getField(), lowerNumber, numberFormat);
PointQueryNode upperNode = new PointQueryNode(termRangeNode.getField(), upperNumber, numberFormat);
boolean lowerInclusive = termRangeNode.isLowerInclusive();
boolean upperInclusive = termRangeNode.isUpperInclusive();
return new PointRangeQueryNode(lowerNode, upperNode, lowerInclusive, upperInclusive, numericConfig);
}
}
}
}
return node;
}
use of org.apache.lucene.queryparser.flexible.core.QueryNodeParseException in project lucene-solr by apache.
the class PointQueryNodeProcessor method postProcessNode.
@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
if (node instanceof FieldQueryNode && !(node.getParent() instanceof RangeQueryNode)) {
QueryConfigHandler config = getQueryConfigHandler();
if (config != null) {
FieldQueryNode fieldNode = (FieldQueryNode) node;
FieldConfig fieldConfig = config.getFieldConfig(fieldNode.getFieldAsString());
if (fieldConfig != null) {
PointsConfig numericConfig = fieldConfig.get(ConfigurationKeys.POINTS_CONFIG);
if (numericConfig != null) {
NumberFormat numberFormat = numericConfig.getNumberFormat();
String text = fieldNode.getTextAsString();
Number number = null;
if (text.length() > 0) {
try {
number = numberFormat.parse(text);
} catch (ParseException e) {
throw new QueryNodeParseException(new MessageImpl(QueryParserMessages.COULD_NOT_PARSE_NUMBER, fieldNode.getTextAsString(), numberFormat.getClass().getCanonicalName()), e);
}
if (Integer.class.equals(numericConfig.getType())) {
number = number.intValue();
} else if (Long.class.equals(numericConfig.getType())) {
number = number.longValue();
} else if (Double.class.equals(numericConfig.getType())) {
number = number.doubleValue();
} else if (Float.class.equals(numericConfig.getType())) {
number = number.floatValue();
}
} else {
throw new QueryNodeParseException(new MessageImpl(QueryParserMessages.NUMERIC_CANNOT_BE_EMPTY, fieldNode.getFieldAsString()));
}
PointQueryNode lowerNode = new PointQueryNode(fieldNode.getField(), number, numberFormat);
PointQueryNode upperNode = new PointQueryNode(fieldNode.getField(), number, numberFormat);
return new PointRangeQueryNode(lowerNode, upperNode, true, true, numericConfig);
}
}
}
}
return node;
}
Aggregations