Search in sources :

Example 1 with MessageImpl

use of org.apache.lucene.queryparser.flexible.messages.MessageImpl 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 MessageImpl

use of org.apache.lucene.queryparser.flexible.messages.MessageImpl in project lucene-solr by apache.

the class StandardSyntaxParser method Term.

public final QueryNode Term(CharSequence field) throws ParseException {
    Token term, boost = null, fuzzySlop = null, goop1, goop2;
    boolean fuzzy = false;
    boolean regexp = false;
    boolean startInc = false;
    boolean endInc = false;
    QueryNode q = null;
    FieldQueryNode qLower, qUpper;
    float defaultMinSimilarity = org.apache.lucene.search.FuzzyQuery.defaultMinSimilarity;
    switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
        case TERM:
        case REGEXPTERM:
        case NUMBER:
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case TERM:
                    term = jj_consume_token(TERM);
                    q = new FieldQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(term.image), term.beginColumn, term.endColumn);
                    break;
                case REGEXPTERM:
                    term = jj_consume_token(REGEXPTERM);
                    regexp = true;
                    break;
                case NUMBER:
                    term = jj_consume_token(NUMBER);
                    break;
                default:
                    jj_la1[13] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
            }
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case FUZZY_SLOP:
                    fuzzySlop = jj_consume_token(FUZZY_SLOP);
                    fuzzy = true;
                    break;
                default:
                    jj_la1[14] = jj_gen;
                    ;
            }
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case CARAT:
                    jj_consume_token(CARAT);
                    boost = jj_consume_token(NUMBER);
                    switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                        case FUZZY_SLOP:
                            fuzzySlop = jj_consume_token(FUZZY_SLOP);
                            fuzzy = true;
                            break;
                        default:
                            jj_la1[15] = jj_gen;
                            ;
                    }
                    break;
                default:
                    jj_la1[16] = jj_gen;
                    ;
            }
            if (fuzzy) {
                float fms = defaultMinSimilarity;
                try {
                    fms = Float.parseFloat(fuzzySlop.image.substring(1));
                } catch (Exception ignored) {
                }
                if (fms < 0.0f) {
                    {
                        if (true)
                            throw new ParseException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX_FUZZY_LIMITS));
                    }
                } else if (fms >= 1.0f && fms != (int) fms) {
                    {
                        if (true)
                            throw new ParseException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX_FUZZY_EDITS));
                    }
                }
                q = new FuzzyQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(term.image), fms, term.beginColumn, term.endColumn);
            } else if (regexp) {
                String re = term.image.substring(1, term.image.length() - 1);
                q = new RegexpQueryNode(field, re, 0, re.length());
            }
            break;
        case RANGEIN_START:
        case RANGEEX_START:
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case RANGEIN_START:
                    jj_consume_token(RANGEIN_START);
                    startInc = true;
                    break;
                case RANGEEX_START:
                    jj_consume_token(RANGEEX_START);
                    break;
                default:
                    jj_la1[17] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
            }
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case RANGE_GOOP:
                    goop1 = jj_consume_token(RANGE_GOOP);
                    break;
                case RANGE_QUOTED:
                    goop1 = jj_consume_token(RANGE_QUOTED);
                    break;
                case RANGE_TO:
                    goop1 = jj_consume_token(RANGE_TO);
                    break;
                default:
                    jj_la1[18] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
            }
            jj_consume_token(RANGE_TO);
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case RANGE_GOOP:
                    goop2 = jj_consume_token(RANGE_GOOP);
                    break;
                case RANGE_QUOTED:
                    goop2 = jj_consume_token(RANGE_QUOTED);
                    break;
                case RANGE_TO:
                    goop2 = jj_consume_token(RANGE_TO);
                    break;
                default:
                    jj_la1[19] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
            }
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case RANGEIN_END:
                    jj_consume_token(RANGEIN_END);
                    endInc = true;
                    break;
                case RANGEEX_END:
                    jj_consume_token(RANGEEX_END);
                    break;
                default:
                    jj_la1[20] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
            }
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case CARAT:
                    jj_consume_token(CARAT);
                    boost = jj_consume_token(NUMBER);
                    break;
                default:
                    jj_la1[21] = jj_gen;
                    ;
            }
            if (goop1.kind == RANGE_QUOTED) {
                goop1.image = goop1.image.substring(1, goop1.image.length() - 1);
            }
            if (goop2.kind == RANGE_QUOTED) {
                goop2.image = goop2.image.substring(1, goop2.image.length() - 1);
            }
            qLower = new FieldQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(goop1.image), goop1.beginColumn, goop1.endColumn);
            qUpper = new FieldQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(goop2.image), goop2.beginColumn, goop2.endColumn);
            q = new TermRangeQueryNode(qLower, qUpper, startInc ? true : false, endInc ? true : false);
            break;
        case QUOTED:
            term = jj_consume_token(QUOTED);
            q = new QuotedFieldQueryNode(field, EscapeQuerySyntaxImpl.discardEscapeChar(term.image.substring(1, term.image.length() - 1)), term.beginColumn + 1, term.endColumn - 1);
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case FUZZY_SLOP:
                    fuzzySlop = jj_consume_token(FUZZY_SLOP);
                    break;
                default:
                    jj_la1[22] = jj_gen;
                    ;
            }
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case CARAT:
                    jj_consume_token(CARAT);
                    boost = jj_consume_token(NUMBER);
                    break;
                default:
                    jj_la1[23] = jj_gen;
                    ;
            }
            int phraseSlop = 0;
            if (fuzzySlop != null) {
                try {
                    phraseSlop = (int) Float.parseFloat(fuzzySlop.image.substring(1));
                    q = new SlopQueryNode(q, phraseSlop);
                } catch (Exception ignored) {
                /* Should this be handled somehow? (defaults to "no PhraseSlop", if
           * slop number is invalid)
           */
                }
            }
            break;
        default:
            jj_la1[24] = 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 (true)
            return q;
    }
    throw new Error("Missing return statement in function");
}
Also used : FuzzyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode) BoostQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode) SlopQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.SlopQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) RegexpQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.RegexpQueryNode) QueryNodeParseException(org.apache.lucene.queryparser.flexible.core.QueryNodeParseException) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) 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) MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl)

Example 3 with MessageImpl

use of org.apache.lucene.queryparser.flexible.messages.MessageImpl in project lucene-solr by apache.

the class EscapeQuerySyntaxImpl method discardEscapeChar.

/**
   * Returns a String where the escape char has been removed, or kept only once
   * if there was a double escape.
   * 
   * Supports escaped unicode characters, e. g. translates <code>A</code> to
   * <code>A</code>.
   * 
   */
public static UnescapedCharSequence discardEscapeChar(CharSequence input) throws ParseException {
    // Create char array to hold unescaped char sequence
    char[] output = new char[input.length()];
    boolean[] wasEscaped = new boolean[input.length()];
    // The length of the output can be less than the input
    // due to discarded escape chars. This variable holds
    // the actual length of the output
    int length = 0;
    // We remember whether the last processed character was
    // an escape character
    boolean lastCharWasEscapeChar = false;
    // The multiplier the current unicode digit must be multiplied with.
    // E. g. the first digit must be multiplied with 16^3, the second with
    // 16^2...
    int codePointMultiplier = 0;
    // Used to calculate the codepoint of the escaped unicode character
    int codePoint = 0;
    for (int i = 0; i < input.length(); i++) {
        char curChar = input.charAt(i);
        if (codePointMultiplier > 0) {
            codePoint += hexToInt(curChar) * codePointMultiplier;
            codePointMultiplier >>>= 4;
            if (codePointMultiplier == 0) {
                output[length++] = (char) codePoint;
                codePoint = 0;
            }
        } else if (lastCharWasEscapeChar) {
            if (curChar == 'u') {
                // found an escaped unicode character
                codePointMultiplier = 16 * 16 * 16;
            } else {
                // this character was escaped
                output[length] = curChar;
                wasEscaped[length] = true;
                length++;
            }
            lastCharWasEscapeChar = false;
        } else {
            if (curChar == '\\') {
                lastCharWasEscapeChar = true;
            } else {
                output[length] = curChar;
                length++;
            }
        }
    }
    if (codePointMultiplier > 0) {
        throw new ParseException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION));
    }
    if (lastCharWasEscapeChar) {
        throw new ParseException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX_ESCAPE_CHARACTER));
    }
    return new UnescapedCharSequence(output, wasEscaped, 0, length);
}
Also used : UnescapedCharSequence(org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence) MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl)

Example 4 with MessageImpl

use of org.apache.lucene.queryparser.flexible.messages.MessageImpl in project lucene-solr by apache.

the class AnyQueryNodeBuilder method build.

@Override
public BooleanQuery build(QueryNode queryNode) throws QueryNodeException {
    AnyQueryNode andNode = (AnyQueryNode) queryNode;
    BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
    List<QueryNode> children = andNode.getChildren();
    if (children != null) {
        for (QueryNode child : children) {
            Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
            if (obj != null) {
                Query query = (Query) obj;
                try {
                    bQuery.add(query, BooleanClause.Occur.SHOULD);
                } catch (TooManyClauses ex) {
                    throw new QueryNodeException(new MessageImpl(/*
             * IQQQ.Q0028E_TOO_MANY_BOOLEAN_CLAUSES,
             * BooleanQuery.getMaxClauseCount()
             */
                    QueryParserMessages.EMPTY_MESSAGE), ex);
                }
            }
        }
    }
    bQuery.setMinimumNumberShouldMatch(andNode.getMinimumMatchingElements());
    return bQuery.build();
}
Also used : QueryNodeException(org.apache.lucene.queryparser.flexible.core.QueryNodeException) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) AnyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.AnyQueryNode) QueryTreeBuilder(org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder) TooManyClauses(org.apache.lucene.search.BooleanQuery.TooManyClauses) AnyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.AnyQueryNode) MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl)

Example 5 with MessageImpl

use of org.apache.lucene.queryparser.flexible.messages.MessageImpl in project lucene-solr by apache.

the class BooleanQueryNodeBuilder method build.

@Override
public BooleanQuery build(QueryNode queryNode) throws QueryNodeException {
    BooleanQueryNode booleanNode = (BooleanQueryNode) queryNode;
    BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
    List<QueryNode> children = booleanNode.getChildren();
    if (children != null) {
        for (QueryNode child : children) {
            Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
            if (obj != null) {
                Query query = (Query) obj;
                try {
                    bQuery.add(query, getModifierValue(child));
                } catch (TooManyClauses ex) {
                    throw new QueryNodeException(new MessageImpl(QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery.getMaxClauseCount(), queryNode.toQueryString(new EscapeQuerySyntaxImpl())), ex);
                }
            }
        }
    }
    return bQuery.build();
}
Also used : QueryNodeException(org.apache.lucene.queryparser.flexible.core.QueryNodeException) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) BooleanQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode) ModifierQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.ModifierQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) QueryTreeBuilder(org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder) BooleanQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode) TooManyClauses(org.apache.lucene.search.BooleanQuery.TooManyClauses) MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl) EscapeQuerySyntaxImpl(org.apache.lucene.queryparser.flexible.standard.parser.EscapeQuerySyntaxImpl)

Aggregations

MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)8 QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)3 QueryNodeParseException (org.apache.lucene.queryparser.flexible.core.QueryNodeParseException)3 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)3 QueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)3 PointsConfig (org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)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 QueryTreeBuilder (org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder)2 FieldConfig (org.apache.lucene.queryparser.flexible.core.config.FieldConfig)2 QueryConfigHandler (org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler)2 BooleanQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode)2 ModifierQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.ModifierQueryNode)2 TermRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 TooManyClauses (org.apache.lucene.search.BooleanQuery.TooManyClauses)2 Query (org.apache.lucene.search.Query)2 AndQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode)1