Search in sources :

Example 6 with MessageImpl

use of org.apache.lucene.queryparser.flexible.messages.MessageImpl in project lucene-solr by apache.

the class PointRangeQueryNodeBuilder method build.

@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
    PointRangeQueryNode numericRangeNode = (PointRangeQueryNode) queryNode;
    PointQueryNode lowerNumericNode = numericRangeNode.getLowerBound();
    PointQueryNode upperNumericNode = numericRangeNode.getUpperBound();
    Number lowerNumber = lowerNumericNode.getValue();
    Number upperNumber = upperNumericNode.getValue();
    PointsConfig pointsConfig = numericRangeNode.getPointsConfig();
    Class<? extends Number> numberType = pointsConfig.getType();
    String field = StringUtils.toString(numericRangeNode.getField());
    boolean minInclusive = numericRangeNode.isLowerInclusive();
    boolean maxInclusive = numericRangeNode.isUpperInclusive();
    // TODO: push down cleaning up of crazy nulls and inclusive/exclusive elsewhere
    if (Integer.class.equals(numberType)) {
        Integer lower = (Integer) lowerNumber;
        if (lower == null) {
            lower = Integer.MIN_VALUE;
        }
        if (minInclusive == false) {
            lower = lower + 1;
        }
        Integer upper = (Integer) upperNumber;
        if (upper == null) {
            upper = Integer.MAX_VALUE;
        }
        if (maxInclusive == false) {
            upper = upper - 1;
        }
        return IntPoint.newRangeQuery(field, lower, upper);
    } else if (Long.class.equals(numberType)) {
        Long lower = (Long) lowerNumber;
        if (lower == null) {
            lower = Long.MIN_VALUE;
        }
        if (minInclusive == false) {
            lower = lower + 1;
        }
        Long upper = (Long) upperNumber;
        if (upper == null) {
            upper = Long.MAX_VALUE;
        }
        if (maxInclusive == false) {
            upper = upper - 1;
        }
        return LongPoint.newRangeQuery(field, lower, upper);
    } else if (Float.class.equals(numberType)) {
        Float lower = (Float) lowerNumber;
        if (lower == null) {
            lower = Float.NEGATIVE_INFINITY;
        }
        if (minInclusive == false) {
            lower = Math.nextUp(lower);
        }
        Float upper = (Float) upperNumber;
        if (upper == null) {
            upper = Float.POSITIVE_INFINITY;
        }
        if (maxInclusive == false) {
            upper = Math.nextDown(upper);
        }
        return FloatPoint.newRangeQuery(field, lower, upper);
    } else if (Double.class.equals(numberType)) {
        Double lower = (Double) lowerNumber;
        if (lower == null) {
            lower = Double.NEGATIVE_INFINITY;
        }
        if (minInclusive == false) {
            lower = Math.nextUp(lower);
        }
        Double upper = (Double) upperNumber;
        if (upper == null) {
            upper = Double.POSITIVE_INFINITY;
        }
        if (maxInclusive == false) {
            upper = Math.nextDown(upper);
        }
        return DoublePoint.newRangeQuery(field, lower, upper);
    } else {
        throw new QueryNodeException(new MessageImpl(QueryParserMessages.UNSUPPORTED_NUMERIC_DATA_TYPE, numberType));
    }
}
Also used : QueryNodeException(org.apache.lucene.queryparser.flexible.core.QueryNodeException) PointsConfig(org.apache.lucene.queryparser.flexible.standard.config.PointsConfig) PointRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode) MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl) PointQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.PointQueryNode)

Example 7 with MessageImpl

use of org.apache.lucene.queryparser.flexible.messages.MessageImpl in project lucene-solr by apache.

the class QueryNodeParseException method setQuery.

public void setQuery(CharSequence query) {
    this.query = query;
    this.message = new MessageImpl(QueryParserMessages.INVALID_SYNTAX_CANNOT_PARSE, query, "");
}
Also used : MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl)

Example 8 with MessageImpl

use of org.apache.lucene.queryparser.flexible.messages.MessageImpl 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;
}
Also used : FieldConfig(org.apache.lucene.queryparser.flexible.core.config.FieldConfig) QueryConfigHandler(org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler) PointsConfig(org.apache.lucene.queryparser.flexible.standard.config.PointsConfig) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) RangeQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.RangeQueryNode) PointRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode) QueryNodeParseException(org.apache.lucene.queryparser.flexible.core.QueryNodeParseException) PointRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode) QueryNodeParseException(org.apache.lucene.queryparser.flexible.core.QueryNodeParseException) ParseException(java.text.ParseException) MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl) PointQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.PointQueryNode) NumberFormat(java.text.NumberFormat)

Aggregations

MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)8 QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)3 QueryNodeParseException (org.apache.lucene.queryparser.flexible.core.QueryNodeParseException)3 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)3 QueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)3 PointsConfig (org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)3 PointQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.PointQueryNode)3 PointRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode)3 NumberFormat (java.text.NumberFormat)2 ParseException (java.text.ParseException)2 QueryTreeBuilder (org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder)2 FieldConfig (org.apache.lucene.queryparser.flexible.core.config.FieldConfig)2 QueryConfigHandler (org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler)2 BooleanQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode)2 ModifierQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.ModifierQueryNode)2 TermRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 TooManyClauses (org.apache.lucene.search.BooleanQuery.TooManyClauses)2 Query (org.apache.lucene.search.Query)2 AndQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode)1