Search in sources :

Example 6 with QueryNodeException

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

Aggregations

QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)6 MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)3 QueryTreeBuilder (org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder)2 QueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)2 StandardQueryParser (org.apache.lucene.queryparser.flexible.standard.StandardQueryParser)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 TooManyClauses (org.apache.lucene.search.BooleanQuery.TooManyClauses)2 Query (org.apache.lucene.search.Query)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 LuceneQueryException (org.apache.geode.cache.lucene.LuceneQueryException)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 DateTools (org.apache.lucene.document.DateTools)1 Resolution (org.apache.lucene.document.DateTools.Resolution)1 FieldConfig (org.apache.lucene.queryparser.flexible.core.config.FieldConfig)1 AnyQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.AnyQueryNode)1 BooleanQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode)1