Search in sources :

Example 6 with PointsConfig

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

the class TestPointQueryParser method testIntegers.

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

Example 7 with PointsConfig

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

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