Search in sources :

Example 6 with QueryConfigHandler

use of org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler 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)

Example 7 with QueryConfigHandler

use of org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler in project lucene-solr by apache.

the class BoostQueryNodeProcessor method postProcessNode.

@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
    if (node instanceof FieldableNode && (node.getParent() == null || !(node.getParent() instanceof FieldableNode))) {
        FieldableNode fieldNode = (FieldableNode) node;
        QueryConfigHandler config = getQueryConfigHandler();
        if (config != null) {
            CharSequence field = fieldNode.getField();
            FieldConfig fieldConfig = config.getFieldConfig(StringUtils.toString(field));
            if (fieldConfig != null) {
                Float boost = fieldConfig.get(ConfigurationKeys.BOOST);
                if (boost != null) {
                    return new BoostQueryNode(node, boost);
                }
            }
        }
    }
    return node;
}
Also used : FieldableNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldableNode) BoostQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode) FieldConfig(org.apache.lucene.queryparser.flexible.core.config.FieldConfig) QueryConfigHandler(org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler)

Example 8 with QueryConfigHandler

use of org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler in project lucene-solr by apache.

the class FuzzyQueryNodeProcessor method preProcessNode.

@Override
protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
    if (node instanceof FuzzyQueryNode) {
        FuzzyQueryNode fuzzyNode = (FuzzyQueryNode) node;
        QueryConfigHandler config = getQueryConfigHandler();
        Analyzer analyzer = getQueryConfigHandler().get(ConfigurationKeys.ANALYZER);
        if (analyzer != null) {
            // because we call utf8ToString, this will only work with the default TermToBytesRefAttribute
            String text = fuzzyNode.getTextAsString();
            text = analyzer.normalize(fuzzyNode.getFieldAsString(), text).utf8ToString();
            fuzzyNode.setText(text);
        }
        FuzzyConfig fuzzyConfig = null;
        if ((fuzzyConfig = config.get(ConfigurationKeys.FUZZY_CONFIG)) != null) {
            fuzzyNode.setPrefixLength(fuzzyConfig.getPrefixLength());
            if (fuzzyNode.getSimilarity() < 0) {
                fuzzyNode.setSimilarity(fuzzyConfig.getMinSimilarity());
            }
        } else if (fuzzyNode.getSimilarity() < 0) {
            throw new IllegalArgumentException("No FUZZY_CONFIG set in the config");
        }
    }
    return node;
}
Also used : FuzzyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode) FuzzyConfig(org.apache.lucene.queryparser.flexible.standard.config.FuzzyConfig) QueryConfigHandler(org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler) Analyzer(org.apache.lucene.analysis.Analyzer)

Aggregations

QueryConfigHandler (org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler)8 FieldConfig (org.apache.lucene.queryparser.flexible.core.config.FieldConfig)3 FuzzyConfig (org.apache.lucene.queryparser.flexible.standard.config.FuzzyConfig)3 NumberFormat (java.text.NumberFormat)2 ParseException (java.text.ParseException)2 QueryNodeParseException (org.apache.lucene.queryparser.flexible.core.QueryNodeParseException)2 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)2 FieldableNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldableNode)2 MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)2 PointsConfig (org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)2 StandardQueryConfigHandler (org.apache.lucene.queryparser.flexible.standard.config.StandardQueryConfigHandler)2 PointQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.PointQueryNode)2 PointRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode)2 Analyzer (org.apache.lucene.analysis.Analyzer)1 BoostQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode)1 FuzzyQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode)1 OrQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.OrQueryNode)1 QueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)1 RangeQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.RangeQueryNode)1 SyntaxParser (org.apache.lucene.queryparser.flexible.core.parser.SyntaxParser)1