Search in sources :

Example 6 with TermRangeQueryNode

use of org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode in project lucene-solr by apache.

the class StandardSyntaxParser method Clause.

public final QueryNode Clause(CharSequence field) throws ParseException {
    QueryNode q;
    Token fieldToken = null, boost = null, operator = null, term = null;
    FieldQueryNode qLower, qUpper;
    boolean lowerInclusive, upperInclusive;
    boolean group = false;
    if (jj_2_2(3)) {
        fieldToken = jj_consume_token(TERM);
        switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
            case OP_COLON:
            case OP_EQUAL:
                switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                    case OP_COLON:
                        jj_consume_token(OP_COLON);
                        break;
                    case OP_EQUAL:
                        jj_consume_token(OP_EQUAL);
                        break;
                    default:
                        jj_la1[5] = jj_gen;
                        jj_consume_token(-1);
                        throw new ParseException();
                }
                field = EscapeQuerySyntaxImpl.discardEscapeChar(fieldToken.image);
                q = Term(field);
                break;
            case OP_LESSTHAN:
            case OP_LESSTHANEQ:
            case OP_MORETHAN:
            case OP_MORETHANEQ:
                switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                    case OP_LESSTHAN:
                        operator = jj_consume_token(OP_LESSTHAN);
                        break;
                    case OP_LESSTHANEQ:
                        operator = jj_consume_token(OP_LESSTHANEQ);
                        break;
                    case OP_MORETHAN:
                        operator = jj_consume_token(OP_MORETHAN);
                        break;
                    case OP_MORETHANEQ:
                        operator = jj_consume_token(OP_MORETHANEQ);
                        break;
                    default:
                        jj_la1[6] = jj_gen;
                        jj_consume_token(-1);
                        throw new ParseException();
                }
                field = EscapeQuerySyntaxImpl.discardEscapeChar(fieldToken.image);
                switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                    case TERM:
                        term = jj_consume_token(TERM);
                        break;
                    case QUOTED:
                        term = jj_consume_token(QUOTED);
                        break;
                    case NUMBER:
                        term = jj_consume_token(NUMBER);
                        break;
                    default:
                        jj_la1[7] = jj_gen;
                        jj_consume_token(-1);
                        throw new ParseException();
                }
                if (term.kind == QUOTED) {
                    term.image = term.image.substring(1, term.image.length() - 1);
                }
                switch(operator.kind) {
                    case OP_LESSTHAN:
                        lowerInclusive = true;
                        upperInclusive = false;
                        qLower = new FieldQueryNode(field, "*", term.beginColumn, term.endColumn);
                        qUpper = new FieldQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(term.image), term.beginColumn, term.endColumn);
                        break;
                    case OP_LESSTHANEQ:
                        lowerInclusive = true;
                        upperInclusive = true;
                        qLower = new FieldQueryNode(field, "*", term.beginColumn, term.endColumn);
                        qUpper = new FieldQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(term.image), term.beginColumn, term.endColumn);
                        break;
                    case OP_MORETHAN:
                        lowerInclusive = false;
                        upperInclusive = true;
                        qLower = new FieldQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(term.image), term.beginColumn, term.endColumn);
                        qUpper = new FieldQueryNode(field, "*", term.beginColumn, term.endColumn);
                        break;
                    case OP_MORETHANEQ:
                        lowerInclusive = true;
                        upperInclusive = true;
                        qLower = new FieldQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(term.image), term.beginColumn, term.endColumn);
                        qUpper = new FieldQueryNode(field, "*", term.beginColumn, term.endColumn);
                        break;
                    default:
                        {
                            if (true)
                                throw new Error("Unhandled case: operator=" + operator.toString());
                        }
                }
                q = new TermRangeQueryNode(qLower, qUpper, lowerInclusive, upperInclusive);
                break;
            default:
                jj_la1[8] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
    } else {
        switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
            case LPAREN:
            case QUOTED:
            case TERM:
            case REGEXPTERM:
            case RANGEIN_START:
            case RANGEEX_START:
            case NUMBER:
                if (jj_2_1(2)) {
                    fieldToken = jj_consume_token(TERM);
                    switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                        case OP_COLON:
                            jj_consume_token(OP_COLON);
                            break;
                        case OP_EQUAL:
                            jj_consume_token(OP_EQUAL);
                            break;
                        default:
                            jj_la1[9] = jj_gen;
                            jj_consume_token(-1);
                            throw new ParseException();
                    }
                    field = EscapeQuerySyntaxImpl.discardEscapeChar(fieldToken.image);
                } else {
                    ;
                }
                switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                    case QUOTED:
                    case TERM:
                    case REGEXPTERM:
                    case RANGEIN_START:
                    case RANGEEX_START:
                    case NUMBER:
                        q = Term(field);
                        break;
                    case LPAREN:
                        jj_consume_token(LPAREN);
                        q = Query(field);
                        jj_consume_token(RPAREN);
                        switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                            case CARAT:
                                jj_consume_token(CARAT);
                                boost = jj_consume_token(NUMBER);
                                break;
                            default:
                                jj_la1[10] = jj_gen;
                                ;
                        }
                        group = true;
                        break;
                    default:
                        jj_la1[11] = jj_gen;
                        jj_consume_token(-1);
                        throw new ParseException();
                }
                break;
            default:
                jj_la1[12] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
    }
    if (boost != null) {
        float f = (float) 1.0;
        try {
            f = Float.parseFloat(boost.image);
            // avoid boosting null queries, such as those caused by stop words
            if (q != null) {
                q = new BoostQueryNode(q, f);
            }
        } catch (Exception ignored) {
        /* Should this be handled somehow? (defaults to "no boost", if
             * boost number is invalid)
             */
        }
    }
    if (group) {
        q = new GroupQueryNode(q);
    }
    {
        if (true)
            return q;
    }
    throw new Error("Missing return statement in function");
}
Also used : FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) BoostQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode) RegexpQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.RegexpQueryNode) GroupQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.GroupQueryNode) AndQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) TermRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode) BooleanQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode) BoostQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode) FuzzyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode) ModifierQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.ModifierQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) SlopQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.SlopQueryNode) OrQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.OrQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) TermRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode) QueryNodeParseException(org.apache.lucene.queryparser.flexible.core.QueryNodeParseException) GroupQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.GroupQueryNode) QueryNodeParseException(org.apache.lucene.queryparser.flexible.core.QueryNodeParseException)

Example 7 with TermRangeQueryNode

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

FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)7 TermRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)7 QueryNodeParseException (org.apache.lucene.queryparser.flexible.core.QueryNodeParseException)3 FuzzyQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode)3 QuotedFieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode)3 Analyzer (org.apache.lucene.analysis.Analyzer)2 FieldConfig (org.apache.lucene.queryparser.flexible.core.config.FieldConfig)2 AndQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode)2 BooleanQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode)2 BoostQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode)2 GroupQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.GroupQueryNode)2 ModifierQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.ModifierQueryNode)2 OrQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.OrQueryNode)2 QueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)2 SlopQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.SlopQueryNode)2 UnescapedCharSequence (org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence)2 MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)2 RegexpQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.RegexpQueryNode)2 DateFormat (java.text.DateFormat)1 NumberFormat (java.text.NumberFormat)1