Example usage for org.apache.lucene.search FuzzyQuery defaultMaxExpansions

List of usage examples for org.apache.lucene.search FuzzyQuery defaultMaxExpansions

Introduction

In this page you can find the example usage for org.apache.lucene.search FuzzyQuery defaultMaxExpansions.

Prototype

int defaultMaxExpansions

To view the source code for org.apache.lucene.search FuzzyQuery defaultMaxExpansions.

Click Source Link

Usage

From source file:io.crate.lucene.match.OptionParser.java

License:Apache License

public static ParsedOptions parse(MultiMatchQueryBuilder.Type matchType, @Nullable Map options)
        throws IllegalArgumentException {
    if (options == null) {
        options = Collections.emptyMap();
    } else {//from   ww w.  ja va2 s  .c  om
        // need a copy. Otherwise manipulations on a shared option will lead to strange race conditions.
        options = new HashMap(options);
    }
    ParsedOptions parsedOptions = new ParsedOptions(floatValue(options, OPTIONS.BOOST, null),
            analyzer(options.remove(OPTIONS.ANALYZER)),
            zeroTermsQuery(options.remove(OPTIONS.ZERO_TERMS_QUERY)),
            intValue(options, OPTIONS.MAX_EXPANSIONS, FuzzyQuery.defaultMaxExpansions),
            fuzziness(options.remove(OPTIONS.FUZZINESS)),
            intValue(options, OPTIONS.PREFIX_LENGTH, FuzzyQuery.defaultPrefixLength),
            transpositions(options.remove(OPTIONS.FUZZY_TRANSPOSITIONS)));

    switch (matchType.matchQueryType()) {
    case BOOLEAN:
        parsedOptions.commonTermsCutoff(floatValue(options, OPTIONS.CUTOFF_FREQUENCY, null));
        parsedOptions.operator(operator(options.remove(OPTIONS.OPERATOR)));
        parsedOptions.minimumShouldMatch(minimumShouldMatch(options.remove(OPTIONS.MINIMUM_SHOULD_MATCH)));
        break;
    case PHRASE:
        parsedOptions.phraseSlop(intValue(options, OPTIONS.SLOP, 0));
        parsedOptions.tieBreaker(floatValue(options, OPTIONS.TIE_BREAKER, null));
        break;
    case PHRASE_PREFIX:
        parsedOptions.phraseSlop(intValue(options, OPTIONS.SLOP, 0));
        parsedOptions.tieBreaker(floatValue(options, OPTIONS.TIE_BREAKER, null));
        parsedOptions.rewrite(rewrite(options.remove(OPTIONS.REWRITE)));
        break;
    }
    if (!options.isEmpty()) {
        raiseIllegalOptions(matchType, options);
    }
    return parsedOptions;
}

From source file:org.codelibs.elasticsearch.index.query.MatchPhrasePrefixQueryBuilder.java

License:Apache License

public static Optional<MatchPhrasePrefixQueryBuilder> fromXContent(QueryParseContext parseContext)
        throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;//from   www .ja va  2s . c o  m
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    int maxExpansion = FuzzyQuery.defaultMaxExpansions;
    String queryName = null;
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (MatchQueryBuilder.QUERY_FIELD.match(currentFieldName)) {
                        value = parser.objectText();
                    } else if (MatchQueryBuilder.ANALYZER_FIELD.match(currentFieldName)) {
                        analyzer = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (MatchPhraseQueryBuilder.SLOP_FIELD.match(currentFieldName)) {
                        slop = parser.intValue();
                    } else if (MAX_EXPANSIONS_FIELD.match(currentFieldName)) {
                        maxExpansion = parser.intValue();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(),
                                "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(),
                            "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName,
                    parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }

    MatchPhrasePrefixQueryBuilder matchQuery = new MatchPhrasePrefixQueryBuilder(fieldName, value);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.maxExpansions(maxExpansion);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    return Optional.of(matchQuery);
}

From source file:org.codelibs.elasticsearch.index.query.MatchQueryBuilder.java

License:Apache License

public static Optional<MatchQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;/*  w w w  . j  av a  2  s .c o  m*/
    MatchQuery.Type type = MatchQuery.Type.BOOLEAN;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String minimumShouldMatch = null;
    String analyzer = null;
    Operator operator = MatchQueryBuilder.DEFAULT_OPERATOR;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    Fuzziness fuzziness = null;
    int prefixLength = FuzzyQuery.defaultPrefixLength;
    int maxExpansion = FuzzyQuery.defaultMaxExpansions;
    boolean fuzzyTranspositions = FuzzyQuery.defaultTranspositions;
    String fuzzyRewrite = null;
    boolean lenient = MatchQuery.DEFAULT_LENIENCY;
    Float cutOffFrequency = null;
    ZeroTermsQuery zeroTermsQuery = MatchQuery.DEFAULT_ZERO_TERMS_QUERY;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (QUERY_FIELD.match(currentFieldName)) {
                        value = parser.objectText();
                    } else if (TYPE_FIELD.match(currentFieldName)) {
                        String tStr = parser.text();
                        if ("boolean".equals(tStr)) {
                            type = MatchQuery.Type.BOOLEAN;
                        } else if ("phrase".equals(tStr)) {
                            type = MatchQuery.Type.PHRASE;
                        } else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) {
                            type = MatchQuery.Type.PHRASE_PREFIX;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(),
                                    "[" + NAME + "] query does not support type " + tStr);
                        }
                    } else if (ANALYZER_FIELD.match(currentFieldName)) {
                        analyzer = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (SLOP_FIELD.match(currentFieldName)) {
                        slop = parser.intValue();
                    } else if (Fuzziness.FIELD.match(currentFieldName)) {
                        fuzziness = Fuzziness.parse(parser);
                    } else if (PREFIX_LENGTH_FIELD.match(currentFieldName)) {
                        prefixLength = parser.intValue();
                    } else if (MAX_EXPANSIONS_FIELD.match(currentFieldName)) {
                        maxExpansion = parser.intValue();
                    } else if (OPERATOR_FIELD.match(currentFieldName)) {
                        operator = Operator.fromString(parser.text());
                    } else if (MINIMUM_SHOULD_MATCH_FIELD.match(currentFieldName)) {
                        minimumShouldMatch = parser.textOrNull();
                    } else if (FUZZY_REWRITE_FIELD.match(currentFieldName)) {
                        fuzzyRewrite = parser.textOrNull();
                    } else if (FUZZY_TRANSPOSITIONS_FIELD.match(currentFieldName)) {
                        fuzzyTranspositions = parser.booleanValue();
                    } else if (LENIENT_FIELD.match(currentFieldName)) {
                        lenient = parser.booleanValue();
                    } else if (CUTOFF_FREQUENCY_FIELD.match(currentFieldName)) {
                        cutOffFrequency = parser.floatValue();
                    } else if (ZERO_TERMS_QUERY_FIELD.match(currentFieldName)) {
                        String zeroTermsDocs = parser.text();
                        if ("none".equalsIgnoreCase(zeroTermsDocs)) {
                            zeroTermsQuery = MatchQuery.ZeroTermsQuery.NONE;
                        } else if ("all".equalsIgnoreCase(zeroTermsDocs)) {
                            zeroTermsQuery = MatchQuery.ZeroTermsQuery.ALL;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(),
                                    "Unsupported zero_terms_docs value [" + zeroTermsDocs + "]");
                        }
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(),
                                "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(),
                            "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName,
                    parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }

    if (value == null) {
        throw new ParsingException(parser.getTokenLocation(), "No text specified for text query");
    }

    MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
    matchQuery.operator(operator);
    matchQuery.type(type);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.minimumShouldMatch(minimumShouldMatch);
    if (fuzziness != null) {
        matchQuery.fuzziness(fuzziness);
    }
    matchQuery.fuzzyRewrite(fuzzyRewrite);
    matchQuery.prefixLength(prefixLength);
    matchQuery.fuzzyTranspositions(fuzzyTranspositions);
    matchQuery.maxExpansions(maxExpansion);
    matchQuery.lenient(lenient);
    if (cutOffFrequency != null) {
        matchQuery.cutoffFrequency(cutOffFrequency);
    }
    matchQuery.zeroTermsQuery(zeroTermsQuery);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    return Optional.of(matchQuery);
}

From source file:org.elasticsearch.index.query.FuzzyQueryParser.java

License:Apache License

@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    XContentParser.Token token = parser.nextToken();
    if (token != XContentParser.Token.FIELD_NAME) {
        throw new QueryParsingException(parseContext.index(), "[fuzzy] query malformed, no field");
    }//from   ww w .  j av  a 2 s  .  com
    String fieldName = parser.currentName();

    String value = null;
    float boost = 1.0f;
    Fuzziness fuzziness = DEFAULT_FUZZINESS;
    int prefixLength = FuzzyQuery.defaultPrefixLength;
    int maxExpansions = FuzzyQuery.defaultMaxExpansions;
    boolean transpositions = false;
    String queryName = null;
    MultiTermQuery.RewriteMethod rewriteMethod = null;
    token = parser.nextToken();
    if (token == XContentParser.Token.START_OBJECT) {
        String currentFieldName = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else {
                if ("term".equals(currentFieldName)) {
                    value = parser.text();
                } else if ("value".equals(currentFieldName)) {
                    value = parser.text();
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                } else if (FUZZINESS.match(currentFieldName, parseContext.parseFlags())) {
                    fuzziness = Fuzziness.parse(parser);
                } else if ("prefix_length".equals(currentFieldName)
                        || "prefixLength".equals(currentFieldName)) {
                    prefixLength = parser.intValue();
                } else if ("max_expansions".equals(currentFieldName)
                        || "maxExpansions".equals(currentFieldName)) {
                    maxExpansions = parser.intValue();
                } else if ("transpositions".equals(currentFieldName)) {
                    transpositions = parser.booleanValue();
                } else if ("rewrite".equals(currentFieldName)) {
                    rewriteMethod = QueryParsers.parseRewriteMethod(parser.textOrNull(), null);
                } else if ("_name".equals(currentFieldName)) {
                    queryName = parser.text();
                } else {
                    throw new QueryParsingException(parseContext.index(),
                            "[fuzzy] query does not support [" + currentFieldName + "]");
                }
            }
        }
        parser.nextToken();
    } else {
        value = parser.text();
        // move to the next token
        parser.nextToken();
    }

    if (value == null) {
        throw new QueryParsingException(parseContext.index(), "No value specified for fuzzy query");
    }

    Query query = null;
    MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
    if (smartNameFieldMappers != null) {
        if (smartNameFieldMappers.hasMapper()) {
            query = smartNameFieldMappers.mapper().fuzzyQuery(value, fuzziness, prefixLength, maxExpansions,
                    transpositions);
        }
    }
    if (query == null) {
        query = new FuzzyQuery(new Term(fieldName, value), fuzziness.asDistance(value), prefixLength,
                maxExpansions, transpositions);
    }
    if (query instanceof MultiTermQuery) {
        QueryParsers.setRewriteMethod((MultiTermQuery) query, rewriteMethod);
    }
    query.setBoost(boost);

    query = wrapSmartNameQuery(query, smartNameFieldMappers, parseContext);
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, query);
    }
    return query;
}

From source file:org.elasticsearch.index.query.MatchPhrasePrefixQueryBuilder.java

License:Apache License

public static Optional<MatchPhrasePrefixQueryBuilder> fromXContent(QueryParseContext parseContext)
        throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;//from  www  . j a  va  2s  .  c o  m
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    int maxExpansion = FuzzyQuery.defaultMaxExpansions;
    String queryName = null;
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            MatchQueryBuilder.QUERY_FIELD)) {
                        value = parser.objectText();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            MatchQueryBuilder.ANALYZER_FIELD)) {
                        analyzer = parser.text();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            AbstractQueryBuilder.BOOST_FIELD)) {
                        boost = parser.floatValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            MatchPhraseQueryBuilder.SLOP_FIELD)) {
                        slop = parser.intValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            MAX_EXPANSIONS_FIELD)) {
                        maxExpansion = parser.intValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            AbstractQueryBuilder.NAME_FIELD)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(),
                                "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(),
                            "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName,
                    parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }

    MatchPhrasePrefixQueryBuilder matchQuery = new MatchPhrasePrefixQueryBuilder(fieldName, value);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.maxExpansions(maxExpansion);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    return Optional.of(matchQuery);
}

From source file:org.elasticsearch.index.query.MatchQueryBuilder.java

License:Apache License

public static Optional<MatchQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;//  w  w w .j a v  a 2 s .c  o  m
    MatchQuery.Type type = MatchQuery.Type.BOOLEAN;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String minimumShouldMatch = null;
    String analyzer = null;
    Operator operator = MatchQueryBuilder.DEFAULT_OPERATOR;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    Fuzziness fuzziness = null;
    int prefixLength = FuzzyQuery.defaultPrefixLength;
    int maxExpansion = FuzzyQuery.defaultMaxExpansions;
    boolean fuzzyTranspositions = FuzzyQuery.defaultTranspositions;
    String fuzzyRewrite = null;
    boolean lenient = MatchQuery.DEFAULT_LENIENCY;
    Float cutOffFrequency = null;
    ZeroTermsQuery zeroTermsQuery = MatchQuery.DEFAULT_ZERO_TERMS_QUERY;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (parseContext.getParseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
                        value = parser.objectText();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName, TYPE_FIELD)) {
                        String tStr = parser.text();
                        if ("boolean".equals(tStr)) {
                            type = MatchQuery.Type.BOOLEAN;
                        } else if ("phrase".equals(tStr)) {
                            type = MatchQuery.Type.PHRASE;
                        } else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) {
                            type = MatchQuery.Type.PHRASE_PREFIX;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(),
                                    "[" + NAME + "] query does not support type " + tStr);
                        }
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName, ANALYZER_FIELD)) {
                        analyzer = parser.text();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            AbstractQueryBuilder.BOOST_FIELD)) {
                        boost = parser.floatValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName, SLOP_FIELD)) {
                        slop = parser.intValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName, Fuzziness.FIELD)) {
                        fuzziness = Fuzziness.parse(parser);
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            PREFIX_LENGTH_FIELD)) {
                        prefixLength = parser.intValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            MAX_EXPANSIONS_FIELD)) {
                        maxExpansion = parser.intValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName, OPERATOR_FIELD)) {
                        operator = Operator.fromString(parser.text());
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            MINIMUM_SHOULD_MATCH_FIELD)) {
                        minimumShouldMatch = parser.textOrNull();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            FUZZY_REWRITE_FIELD)) {
                        fuzzyRewrite = parser.textOrNull();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            FUZZY_TRANSPOSITIONS_FIELD)) {
                        fuzzyTranspositions = parser.booleanValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName, LENIENT_FIELD)) {
                        lenient = parser.booleanValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            CUTOFF_FREQUENCY_FIELD)) {
                        cutOffFrequency = parser.floatValue();
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            ZERO_TERMS_QUERY_FIELD)) {
                        String zeroTermsDocs = parser.text();
                        if ("none".equalsIgnoreCase(zeroTermsDocs)) {
                            zeroTermsQuery = MatchQuery.ZeroTermsQuery.NONE;
                        } else if ("all".equalsIgnoreCase(zeroTermsDocs)) {
                            zeroTermsQuery = MatchQuery.ZeroTermsQuery.ALL;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(),
                                    "Unsupported zero_terms_docs value [" + zeroTermsDocs + "]");
                        }
                    } else if (parseContext.getParseFieldMatcher().match(currentFieldName,
                            AbstractQueryBuilder.NAME_FIELD)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(),
                                "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(),
                            "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName,
                    parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }

    if (value == null) {
        throw new ParsingException(parser.getTokenLocation(), "No text specified for text query");
    }

    MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
    matchQuery.operator(operator);
    matchQuery.type(type);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.minimumShouldMatch(minimumShouldMatch);
    if (fuzziness != null) {
        matchQuery.fuzziness(fuzziness);
    }
    matchQuery.fuzzyRewrite(fuzzyRewrite);
    matchQuery.prefixLength(prefixLength);
    matchQuery.fuzzyTranspositions(fuzzyTranspositions);
    matchQuery.maxExpansions(maxExpansion);
    matchQuery.lenient(lenient);
    if (cutOffFrequency != null) {
        matchQuery.cutoffFrequency(cutOffFrequency);
    }
    matchQuery.zeroTermsQuery(zeroTermsQuery);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    return Optional.of(matchQuery);
}

From source file:org.elasticsearch.index.query.TextQueryParser.java

License:Apache License

@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    org.elasticsearch.index.search.TextQueryParser.Type type = org.elasticsearch.index.search.TextQueryParser.Type.BOOLEAN;
    if ("text_phrase".equals(parser.currentName()) || "textPhrase".equals(parser.currentName())) {
        type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE;
    } else if ("text_phrase_prefix".equals(parser.currentName())
            || "textPhrasePrefix".equals(parser.currentName())) {
        type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE_PREFIX;
    }//from ww w .j  ava2s  .  co m

    XContentParser.Token token = parser.nextToken();
    assert token == XContentParser.Token.FIELD_NAME;
    String fieldName = parser.currentName();

    String text = null;
    float boost = 1.0f;
    int phraseSlop = 0;
    String analyzer = null;
    String fuzziness = null;
    int prefixLength = FuzzyQuery.defaultPrefixLength;
    int maxExpansions = FuzzyQuery.defaultMaxExpansions;
    BooleanClause.Occur occur = BooleanClause.Occur.SHOULD;

    token = parser.nextToken();
    if (token == XContentParser.Token.START_OBJECT) {
        String currentFieldName = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token.isValue()) {
                if ("query".equals(currentFieldName)) {
                    text = parser.text();
                } else if ("type".equals(currentFieldName)) {
                    String tStr = parser.text();
                    if ("boolean".equals(tStr)) {
                        type = org.elasticsearch.index.search.TextQueryParser.Type.BOOLEAN;
                    } else if ("phrase".equals(tStr)) {
                        type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE;
                    } else if ("phrase_prefix".equals(tStr) || "phrasePrefix".equals(currentFieldName)) {
                        type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE_PREFIX;
                    }
                } else if ("analyzer".equals(currentFieldName)) {
                    analyzer = parser.textOrNull();
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                } else if ("slop".equals(currentFieldName) || "phrase_slop".equals(currentFieldName)
                        || "phraseSlop".equals(currentFieldName)) {
                    phraseSlop = parser.intValue();
                } else if ("fuzziness".equals(currentFieldName)) {
                    fuzziness = parser.textOrNull();
                } else if ("prefix_length".equals(currentFieldName)
                        || "prefixLength".equals(currentFieldName)) {
                    prefixLength = parser.intValue();
                } else if ("max_expansions".equals(currentFieldName)
                        || "maxExpansions".equals(currentFieldName)) {
                    maxExpansions = parser.intValue();
                } else if ("operator".equals(currentFieldName)) {
                    String op = parser.text();
                    if ("or".equalsIgnoreCase(op)) {
                        occur = BooleanClause.Occur.SHOULD;
                    } else if ("and".equalsIgnoreCase(op)) {
                        occur = BooleanClause.Occur.MUST;
                    } else {
                        throw new QueryParsingException(parseContext.index(),
                                "text query requires operator to be either 'and' or 'or', not [" + op + "]");
                    }
                }
            }
        }
        parser.nextToken();
    } else {
        text = parser.text();
        // move to the next token
        parser.nextToken();
    }

    if (text == null) {
        throw new QueryParsingException(parseContext.index(), "No text specified for text query");
    }

    org.elasticsearch.index.search.TextQueryParser tQP = new org.elasticsearch.index.search.TextQueryParser(
            parseContext, fieldName, text);
    tQP.setPhraseSlop(phraseSlop);
    tQP.setAnalyzer(analyzer);
    tQP.setFuzziness(fuzziness);
    tQP.setFuzzyPrefixLength(prefixLength);
    tQP.setMaxExpansions(maxExpansions);
    tQP.setOccur(occur);

    Query query = tQP.parse(type);
    query.setBoost(boost);
    return query;
}

From source file:org.elasticsearch.index.query.xcontent.FuzzyQueryParser.java

License:Apache License

@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    XContentParser.Token token = parser.nextToken();
    assert token == XContentParser.Token.FIELD_NAME;
    String fieldName = parser.currentName();

    String value = null;//w  ww .  j av a2 s.  c  o  m
    float boost = 1.0f;
    String minSimilarity = "0.5";
    int prefixLength = FuzzyQuery.defaultPrefixLength;
    int maxExpansions = FuzzyQuery.defaultMaxExpansions;
    token = parser.nextToken();
    if (token == XContentParser.Token.START_OBJECT) {
        String currentFieldName = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else {
                if ("term".equals(currentFieldName)) {
                    value = parser.text();
                } else if ("value".equals(currentFieldName)) {
                    value = parser.text();
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                } else if ("min_similarity".equals(currentFieldName)
                        || "minSimilarity".equals(currentFieldName)) {
                    minSimilarity = parser.text();
                } else if ("prefix_length".equals(currentFieldName)
                        || "prefixLength".equals(currentFieldName)) {
                    prefixLength = parser.intValue();
                } else if ("max_expansions".equals(currentFieldName)
                        || "maxExpansions".equals(currentFieldName)) {
                    maxExpansions = parser.intValue();
                }
            }
        }
        parser.nextToken();
    } else {
        value = parser.text();
        // move to the next token
        parser.nextToken();
    }

    if (value == null) {
        throw new QueryParsingException(index, "No value specified for fuzzy query");
    }

    Query query = null;
    MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
    if (smartNameFieldMappers != null) {
        if (smartNameFieldMappers.hasMapper()) {
            query = smartNameFieldMappers.mapper().fuzzyQuery(value, minSimilarity, prefixLength,
                    maxExpansions);
        }
    }
    if (query == null) {
        query = new FuzzyQuery(new Term(fieldName, value), Float.parseFloat(minSimilarity), prefixLength,
                maxExpansions);
    }
    query.setBoost(boost);

    return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext);
}

From source file:org.elasticsearch.index.query.xcontent.TextQueryParser.java

License:Apache License

@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    org.elasticsearch.index.search.TextQueryParser.Type type = org.elasticsearch.index.search.TextQueryParser.Type.BOOLEAN;
    if ("text_phrase".equals(parser.currentName()) || "textPhrase".equals(parser.currentName())) {
        type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE;
    } else if ("text_phrase_prefix".equals(parser.currentName())
            || "textPhrasePrefix".equals(parser.currentName())) {
        type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE_PREFIX;
    }//from  w  w  w  .  j  a  va 2  s  . c  om

    XContentParser.Token token = parser.nextToken();
    assert token == XContentParser.Token.FIELD_NAME;
    String fieldName = parser.currentName();

    String text = null;
    float boost = 1.0f;
    int phraseSlop = 0;
    String analyzer = null;
    String fuzziness = null;
    int prefixLength = FuzzyQuery.defaultPrefixLength;
    int maxExpansions = FuzzyQuery.defaultMaxExpansions;
    BooleanClause.Occur occur = BooleanClause.Occur.SHOULD;

    token = parser.nextToken();
    if (token == XContentParser.Token.START_OBJECT) {
        String currentFieldName = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token.isValue()) {
                if ("query".equals(currentFieldName)) {
                    text = parser.text();
                } else if ("type".equals(currentFieldName)) {
                    String tStr = parser.text();
                    if ("boolean".equals(tStr)) {
                        type = org.elasticsearch.index.search.TextQueryParser.Type.BOOLEAN;
                    } else if ("phrase".equals(tStr)) {
                        type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE;
                    } else if ("phrase_prefix".equals(tStr) || "phrasePrefix".equals(currentFieldName)) {
                        type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE_PREFIX;
                    }
                } else if ("analyzer".equals(currentFieldName)) {
                    analyzer = parser.textOrNull();
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                } else if ("slop".equals(currentFieldName) || "phrase_slop".equals(currentFieldName)
                        || "phraseSlop".equals(currentFieldName)) {
                    phraseSlop = parser.intValue();
                } else if ("fuzziness".equals(currentFieldName)) {
                    fuzziness = parser.textOrNull();
                } else if ("prefix_length".equals(currentFieldName)
                        || "prefixLength".equals(currentFieldName)) {
                    prefixLength = parser.intValue();
                } else if ("max_expansions".equals(currentFieldName)
                        || "maxExpansions".equals(currentFieldName)) {
                    maxExpansions = parser.intValue();
                } else if ("operator".equals(currentFieldName)) {
                    String op = parser.text();
                    if ("or".equalsIgnoreCase(op)) {
                        occur = BooleanClause.Occur.SHOULD;
                    } else if ("and".equalsIgnoreCase(op)) {
                        occur = BooleanClause.Occur.MUST;
                    } else {
                        throw new QueryParsingException(index,
                                "text query requires operator to be either 'and' or 'or', not [" + op + "]");
                    }
                }
            }
        }
        parser.nextToken();
    } else {
        text = parser.text();
        // move to the next token
        parser.nextToken();
    }

    if (text == null) {
        throw new QueryParsingException(index, "No text specified for text query");
    }

    org.elasticsearch.index.search.TextQueryParser tQP = new org.elasticsearch.index.search.TextQueryParser(
            parseContext, fieldName, text);
    tQP.setPhraseSlop(phraseSlop);
    tQP.setAnalyzer(analyzer);
    tQP.setFuzziness(fuzziness);
    tQP.setFuzzyPrefixLength(prefixLength);
    tQP.setMaxExpansions(maxExpansions);
    tQP.setOccur(occur);

    Query query = tQP.parse(type);
    query.setBoost(boost);
    return query;
}