Search in sources :

Example 1 with PointsConfig

use of org.apache.lucene.queryparser.flexible.standard.config.PointsConfig 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;
}
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) QueryNodeParseException(org.apache.lucene.queryparser.flexible.core.QueryNodeParseException) PointRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode) TermRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode) 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)

Example 2 with PointsConfig

use of org.apache.lucene.queryparser.flexible.standard.config.PointsConfig 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 3 with PointsConfig

use of org.apache.lucene.queryparser.flexible.standard.config.PointsConfig in project lucene-solr by apache.

the class TestPointQueryParser method testDoubles.

public void testDoubles() throws Exception {
    StandardQueryParser parser = new StandardQueryParser();
    Map<String, PointsConfig> pointsConfig = new HashMap<>();
    pointsConfig.put("doubleField", new PointsConfig(NumberFormat.getNumberInstance(Locale.ROOT), Double.class));
    parser.setPointsConfigMap(pointsConfig);
    assertEquals(DoublePoint.newRangeQuery("doubleField", 1.5D, 3.6D), parser.parse("doubleField:[1.5 TO 3.6]", "body"));
    assertEquals(DoublePoint.newRangeQuery("doubleField", 1.5D, 1.5D), parser.parse("doubleField:1.5", "body"));
}
Also used : HashMap(java.util.HashMap) PointsConfig(org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)

Example 4 with PointsConfig

use of org.apache.lucene.queryparser.flexible.standard.config.PointsConfig in project lucene-solr by apache.

the class TestPointQueryParser method testLongs.

public void testLongs() throws Exception {
    StandardQueryParser parser = new StandardQueryParser();
    Map<String, PointsConfig> pointsConfig = new HashMap<>();
    pointsConfig.put("longField", new PointsConfig(NumberFormat.getIntegerInstance(Locale.ROOT), Long.class));
    parser.setPointsConfigMap(pointsConfig);
    assertEquals(LongPoint.newRangeQuery("longField", 1, 3), parser.parse("longField:[1 TO 3]", "body"));
    assertEquals(LongPoint.newRangeQuery("longField", 1, 1), parser.parse("longField:1", "body"));
}
Also used : HashMap(java.util.HashMap) PointsConfig(org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)

Example 5 with PointsConfig

use of org.apache.lucene.queryparser.flexible.standard.config.PointsConfig in project lucene-solr by apache.

the class TestPointQueryParser method testFloats.

public void testFloats() throws Exception {
    StandardQueryParser parser = new StandardQueryParser();
    Map<String, PointsConfig> pointsConfig = new HashMap<>();
    pointsConfig.put("floatField", new PointsConfig(NumberFormat.getNumberInstance(Locale.ROOT), Float.class));
    parser.setPointsConfigMap(pointsConfig);
    assertEquals(FloatPoint.newRangeQuery("floatField", 1.5F, 3.6F), parser.parse("floatField:[1.5 TO 3.6]", "body"));
    assertEquals(FloatPoint.newRangeQuery("floatField", 1.5F, 1.5F), parser.parse("floatField:1.5", "body"));
}
Also used : HashMap(java.util.HashMap) PointsConfig(org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)

Aggregations

PointsConfig (org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)7 HashMap (java.util.HashMap)4 MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)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 QueryNodeParseException (org.apache.lucene.queryparser.flexible.core.QueryNodeParseException)2 FieldConfig (org.apache.lucene.queryparser.flexible.core.config.FieldConfig)2 QueryConfigHandler (org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler)2 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)2 QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)1 RangeQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.RangeQueryNode)1 TermRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)1