Search in sources :

Example 1 with FieldConfig

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

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

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

the class TermRangeQueryNodeProcessor method postProcessNode.

@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
    if (node instanceof TermRangeQueryNode) {
        TermRangeQueryNode termRangeNode = (TermRangeQueryNode) node;
        FieldQueryNode upper = termRangeNode.getUpperBound();
        FieldQueryNode lower = termRangeNode.getLowerBound();
        DateTools.Resolution dateRes = null;
        boolean inclusive = false;
        Locale locale = getQueryConfigHandler().get(ConfigurationKeys.LOCALE);
        if (locale == null) {
            locale = Locale.getDefault();
        }
        TimeZone timeZone = getQueryConfigHandler().get(ConfigurationKeys.TIMEZONE);
        if (timeZone == null) {
            timeZone = TimeZone.getDefault();
        }
        CharSequence field = termRangeNode.getField();
        String fieldStr = null;
        if (field != null) {
            fieldStr = field.toString();
        }
        FieldConfig fieldConfig = getQueryConfigHandler().getFieldConfig(fieldStr);
        if (fieldConfig != null) {
            dateRes = fieldConfig.get(ConfigurationKeys.DATE_RESOLUTION);
        }
        if (termRangeNode.isUpperInclusive()) {
            inclusive = true;
        }
        String part1 = lower.getTextAsString();
        String part2 = upper.getTextAsString();
        try {
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            df.setLenient(true);
            if (part1.length() > 0) {
                Date d1 = df.parse(part1);
                part1 = DateTools.dateToString(d1, dateRes);
                lower.setText(part1);
            }
            if (part2.length() > 0) {
                Date d2 = df.parse(part2);
                if (inclusive) {
                    // The user can only specify the date, not the time, so make sure
                    // the time is set to the latest possible time of that date to
                    // really
                    // include all documents:
                    Calendar cal = Calendar.getInstance(timeZone, locale);
                    cal.setTime(d2);
                    cal.set(Calendar.HOUR_OF_DAY, 23);
                    cal.set(Calendar.MINUTE, 59);
                    cal.set(Calendar.SECOND, 59);
                    cal.set(Calendar.MILLISECOND, 999);
                    d2 = cal.getTime();
                }
                part2 = DateTools.dateToString(d2, dateRes);
                upper.setText(part2);
            }
        } catch (Exception e) {
            // not a date
            Analyzer analyzer = getQueryConfigHandler().get(ConfigurationKeys.ANALYZER);
            if (analyzer != null) {
                // because we call utf8ToString, this will only work with the default TermToBytesRefAttribute
                part1 = analyzer.normalize(lower.getFieldAsString(), part1).utf8ToString();
                part2 = analyzer.normalize(lower.getFieldAsString(), part2).utf8ToString();
                lower.setText(part1);
                upper.setText(part2);
            }
        }
    }
    return node;
}
Also used : Locale(java.util.Locale) FieldConfig(org.apache.lucene.queryparser.flexible.core.config.FieldConfig) Calendar(java.util.Calendar) Analyzer(org.apache.lucene.analysis.Analyzer) Date(java.util.Date) QueryNodeException(org.apache.lucene.queryparser.flexible.core.QueryNodeException) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) DateTools(org.apache.lucene.document.DateTools) TimeZone(java.util.TimeZone) DateFormat(java.text.DateFormat) Resolution(org.apache.lucene.document.DateTools.Resolution) TermRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)

Example 4 with FieldConfig

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

Aggregations

FieldConfig (org.apache.lucene.queryparser.flexible.core.config.FieldConfig)4 QueryConfigHandler (org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler)3 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)3 NumberFormat (java.text.NumberFormat)2 ParseException (java.text.ParseException)2 QueryNodeParseException (org.apache.lucene.queryparser.flexible.core.QueryNodeParseException)2 MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)2 PointsConfig (org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)2 PointQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.PointQueryNode)2 PointRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode)2 TermRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)2 DateFormat (java.text.DateFormat)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Locale (java.util.Locale)1 TimeZone (java.util.TimeZone)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 DateTools (org.apache.lucene.document.DateTools)1 Resolution (org.apache.lucene.document.DateTools.Resolution)1 QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)1