Search in sources :

Example 1 with Fuzziness

use of org.elasticsearch.common.unit.Fuzziness in project elasticsearch by elastic.

the class QueryStringQueryBuilder method fromXContent.

public static QueryStringQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String currentFieldName = null;
    XContentParser.Token token;
    String queryString = null;
    String defaultField = null;
    String analyzer = null;
    String quoteAnalyzer = null;
    String queryName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    boolean autoGeneratePhraseQueries = QueryStringQueryBuilder.DEFAULT_AUTO_GENERATE_PHRASE_QUERIES;
    int maxDeterminizedStates = QueryStringQueryBuilder.DEFAULT_MAX_DETERMINED_STATES;
    boolean enablePositionIncrements = QueryStringQueryBuilder.DEFAULT_ENABLE_POSITION_INCREMENTS;
    boolean escape = QueryStringQueryBuilder.DEFAULT_ESCAPE;
    boolean useDisMax = QueryStringQueryBuilder.DEFAULT_USE_DIS_MAX;
    int fuzzyPrefixLength = QueryStringQueryBuilder.DEFAULT_FUZZY_PREFIX_LENGTH;
    int fuzzyMaxExpansions = QueryStringQueryBuilder.DEFAULT_FUZZY_MAX_EXPANSIONS;
    int phraseSlop = QueryStringQueryBuilder.DEFAULT_PHRASE_SLOP;
    float tieBreaker = QueryStringQueryBuilder.DEFAULT_TIE_BREAKER;
    Boolean analyzeWildcard = null;
    Boolean allowLeadingWildcard = null;
    String minimumShouldMatch = null;
    String quoteFieldSuffix = null;
    Boolean lenient = null;
    Operator defaultOperator = QueryStringQueryBuilder.DEFAULT_OPERATOR;
    String timeZone = null;
    Fuzziness fuzziness = QueryStringQueryBuilder.DEFAULT_FUZZINESS;
    String fuzzyRewrite = null;
    String rewrite = null;
    boolean splitOnWhitespace = DEFAULT_SPLIT_ON_WHITESPACE;
    Boolean useAllFields = null;
    Map<String, Float> fieldsAndWeights = new HashMap<>();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (FIELDS_FIELD.match(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String fField = null;
                    float fBoost = AbstractQueryBuilder.DEFAULT_BOOST;
                    char[] text = parser.textCharacters();
                    int end = parser.textOffset() + parser.textLength();
                    for (int i = parser.textOffset(); i < end; i++) {
                        if (text[i] == '^') {
                            int relativeLocation = i - parser.textOffset();
                            fField = new String(text, parser.textOffset(), relativeLocation);
                            fBoost = Float.parseFloat(new String(text, i + 1, parser.textLength() - relativeLocation - 1));
                            break;
                        }
                    }
                    if (fField == null) {
                        fField = parser.text();
                    }
                    fieldsAndWeights.put(fField, fBoost);
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + QueryStringQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (QUERY_FIELD.match(currentFieldName)) {
                queryString = parser.text();
            } else if (DEFAULT_FIELD_FIELD.match(currentFieldName)) {
                defaultField = parser.text();
            } else if (DEFAULT_OPERATOR_FIELD.match(currentFieldName)) {
                defaultOperator = Operator.fromString(parser.text());
            } else if (ANALYZER_FIELD.match(currentFieldName)) {
                analyzer = parser.text();
            } else if (QUOTE_ANALYZER_FIELD.match(currentFieldName)) {
                quoteAnalyzer = parser.text();
            } else if (ALLOW_LEADING_WILDCARD_FIELD.match(currentFieldName)) {
                allowLeadingWildcard = parser.booleanValue();
            } else if (AUTO_GENERATE_PHRASE_QUERIES_FIELD.match(currentFieldName)) {
                autoGeneratePhraseQueries = parser.booleanValue();
            } else if (MAX_DETERMINIZED_STATES_FIELD.match(currentFieldName)) {
                maxDeterminizedStates = parser.intValue();
            } else if (LOWERCASE_EXPANDED_TERMS_FIELD.match(currentFieldName)) {
            // ignore, deprecated setting
            } else if (ENABLE_POSITION_INCREMENTS_FIELD.match(currentFieldName)) {
                enablePositionIncrements = parser.booleanValue();
            } else if (ESCAPE_FIELD.match(currentFieldName)) {
                escape = parser.booleanValue();
            } else if (USE_DIS_MAX_FIELD.match(currentFieldName)) {
                useDisMax = parser.booleanValue();
            } else if (FUZZY_PREFIX_LENGTH_FIELD.match(currentFieldName)) {
                fuzzyPrefixLength = parser.intValue();
            } else if (FUZZY_MAX_EXPANSIONS_FIELD.match(currentFieldName)) {
                fuzzyMaxExpansions = parser.intValue();
            } else if (FUZZY_REWRITE_FIELD.match(currentFieldName)) {
                fuzzyRewrite = parser.textOrNull();
            } else if (PHRASE_SLOP_FIELD.match(currentFieldName)) {
                phraseSlop = parser.intValue();
            } else if (Fuzziness.FIELD.match(currentFieldName)) {
                fuzziness = Fuzziness.parse(parser);
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (TIE_BREAKER_FIELD.match(currentFieldName)) {
                tieBreaker = parser.floatValue();
            } else if (ANALYZE_WILDCARD_FIELD.match(currentFieldName)) {
                analyzeWildcard = parser.booleanValue();
            } else if (REWRITE_FIELD.match(currentFieldName)) {
                rewrite = parser.textOrNull();
            } else if (MINIMUM_SHOULD_MATCH_FIELD.match(currentFieldName)) {
                minimumShouldMatch = parser.textOrNull();
            } else if (QUOTE_FIELD_SUFFIX_FIELD.match(currentFieldName)) {
                quoteFieldSuffix = parser.textOrNull();
            } else if (LENIENT_FIELD.match(currentFieldName)) {
                lenient = parser.booleanValue();
            } else if (LOCALE_FIELD.match(currentFieldName)) {
            // ignore, deprecated setting
            } else if (ALL_FIELDS_FIELD.match(currentFieldName)) {
                useAllFields = parser.booleanValue();
            } else if (MAX_DETERMINIZED_STATES_FIELD.match(currentFieldName)) {
                maxDeterminizedStates = parser.intValue();
            } else if (TIME_ZONE_FIELD.match(currentFieldName)) {
                try {
                    timeZone = parser.text();
                } catch (IllegalArgumentException e) {
                    throw new ParsingException(parser.getTokenLocation(), "[" + QueryStringQueryBuilder.NAME + "] time_zone [" + parser.text() + "] is unknown");
                }
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (SPLIT_ON_WHITESPACE.match(currentFieldName)) {
                splitOnWhitespace = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + QueryStringQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + QueryStringQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
        }
    }
    if (queryString == null) {
        throw new ParsingException(parser.getTokenLocation(), "[" + QueryStringQueryBuilder.NAME + "] must be provided with a [query]");
    }
    if ((useAllFields != null && useAllFields) && (defaultField != null || fieldsAndWeights.size() != 0)) {
        throw new ParsingException(parser.getTokenLocation(), "cannot use [all_fields] parameter in conjunction with [default_field] or [fields]");
    }
    QueryStringQueryBuilder queryStringQuery = new QueryStringQueryBuilder(queryString);
    queryStringQuery.fields(fieldsAndWeights);
    queryStringQuery.defaultField(defaultField);
    queryStringQuery.defaultOperator(defaultOperator);
    queryStringQuery.analyzer(analyzer);
    queryStringQuery.quoteAnalyzer(quoteAnalyzer);
    queryStringQuery.allowLeadingWildcard(allowLeadingWildcard);
    queryStringQuery.autoGeneratePhraseQueries(autoGeneratePhraseQueries);
    queryStringQuery.maxDeterminizedStates(maxDeterminizedStates);
    queryStringQuery.enablePositionIncrements(enablePositionIncrements);
    queryStringQuery.escape(escape);
    queryStringQuery.useDisMax(useDisMax);
    queryStringQuery.fuzzyPrefixLength(fuzzyPrefixLength);
    queryStringQuery.fuzzyMaxExpansions(fuzzyMaxExpansions);
    queryStringQuery.fuzzyRewrite(fuzzyRewrite);
    queryStringQuery.phraseSlop(phraseSlop);
    queryStringQuery.fuzziness(fuzziness);
    queryStringQuery.tieBreaker(tieBreaker);
    queryStringQuery.analyzeWildcard(analyzeWildcard);
    queryStringQuery.rewrite(rewrite);
    queryStringQuery.minimumShouldMatch(minimumShouldMatch);
    queryStringQuery.quoteFieldSuffix(quoteFieldSuffix);
    queryStringQuery.lenient(lenient);
    queryStringQuery.timeZone(timeZone);
    queryStringQuery.boost(boost);
    queryStringQuery.queryName(queryName);
    queryStringQuery.splitOnWhitespace(splitOnWhitespace);
    queryStringQuery.useAllFields(useAllFields);
    return queryStringQuery;
}
Also used : HashMap(java.util.HashMap) Fuzziness(org.elasticsearch.common.unit.Fuzziness) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 2 with Fuzziness

use of org.elasticsearch.common.unit.Fuzziness in project elasticsearch by elastic.

the class MatchQueryBuilder method fromXContent.

public static MatchQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    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 matchQuery;
}
Also used : Fuzziness(org.elasticsearch.common.unit.Fuzziness) ZeroTermsQuery(org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery) ParsingException(org.elasticsearch.common.ParsingException) MatchQuery(org.elasticsearch.index.search.MatchQuery) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 3 with Fuzziness

use of org.elasticsearch.common.unit.Fuzziness in project elasticsearch by elastic.

the class MultiMatchQueryBuilder method fromXContent.

public static MultiMatchQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    Object value = null;
    Map<String, Float> fieldsBoosts = new HashMap<>();
    MultiMatchQueryBuilder.Type type = DEFAULT_TYPE;
    String analyzer = null;
    int slop = DEFAULT_PHRASE_SLOP;
    Fuzziness fuzziness = null;
    int prefixLength = DEFAULT_PREFIX_LENGTH;
    int maxExpansions = DEFAULT_MAX_EXPANSIONS;
    Operator operator = DEFAULT_OPERATOR;
    String minimumShouldMatch = null;
    String fuzzyRewrite = null;
    Boolean useDisMax = null;
    Float tieBreaker = null;
    Float cutoffFrequency = null;
    boolean lenient = DEFAULT_LENIENCY;
    MatchQuery.ZeroTermsQuery zeroTermsQuery = DEFAULT_ZERO_TERMS_QUERY;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    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 (FIELDS_FIELD.match(currentFieldName)) {
            if (token == XContentParser.Token.START_ARRAY) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    parseFieldAndBoost(parser, fieldsBoosts);
                }
            } else if (token.isValue()) {
                parseFieldAndBoost(parser, fieldsBoosts);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (QUERY_FIELD.match(currentFieldName)) {
                value = parser.objectText();
            } else if (TYPE_FIELD.match(currentFieldName)) {
                type = MultiMatchQueryBuilder.Type.parse(parser.text());
            } 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)) {
                maxExpansions = 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 (USE_DIS_MAX_FIELD.match(currentFieldName)) {
                useDisMax = parser.booleanValue();
            } else if (TIE_BREAKER_FIELD.match(currentFieldName)) {
                tieBreaker = parser.floatValue();
            } else if (CUTOFF_FREQUENCY_FIELD.match(currentFieldName)) {
                cutoffFrequency = parser.floatValue();
            } else if (LENIENT_FIELD.match(currentFieldName)) {
                lenient = parser.booleanValue();
            } 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 + "]");
        }
    }
    if (value == null) {
        throw new ParsingException(parser.getTokenLocation(), "No text specified for multi_match query");
    }
    if (fieldsBoosts.isEmpty()) {
        throw new ParsingException(parser.getTokenLocation(), "No fields specified for multi_match query");
    }
    if (fuzziness != null && (type == Type.CROSS_FIELDS || type == Type.PHRASE || type == Type.PHRASE_PREFIX)) {
        throw new ParsingException(parser.getTokenLocation(), "Fuzziness not allowed for type [" + type.parseField.getPreferredName() + "]");
    }
    return new MultiMatchQueryBuilder(value).fields(fieldsBoosts).type(type).analyzer(analyzer).cutoffFrequency(cutoffFrequency).fuzziness(fuzziness).fuzzyRewrite(fuzzyRewrite).useDisMax(useDisMax).lenient(lenient).maxExpansions(maxExpansions).minimumShouldMatch(minimumShouldMatch).operator(operator).prefixLength(prefixLength).slop(slop).tieBreaker(tieBreaker).zeroTermsQuery(zeroTermsQuery).boost(boost).queryName(queryName);
}
Also used : HashMap(java.util.HashMap) Fuzziness(org.elasticsearch.common.unit.Fuzziness) ParsingException(org.elasticsearch.common.ParsingException) MultiMatchQuery(org.elasticsearch.index.search.MultiMatchQuery) MatchQuery(org.elasticsearch.index.search.MatchQuery) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 4 with Fuzziness

use of org.elasticsearch.common.unit.Fuzziness in project crate by crate.

the class MatchQueryBuilder method blendTermQuery.

protected Query blendTermQuery(Term term, MappedFieldType fieldType) {
    Fuzziness fuzziness = options.fuzziness();
    if (fuzziness != null) {
        if (fieldType != null) {
            Query query = fieldType.fuzzyQuery(term.text(), fuzziness, options.prefixLength(), options.maxExpansions(), options.transpositions());
            if (query instanceof FuzzyQuery) {
                QueryParsers.setRewriteMethod(((FuzzyQuery) query), options.rewriteMethod());
            }
            return query;
        }
        int edits = fuzziness.asDistance(term.text());
        FuzzyQuery query = new FuzzyQuery(term, edits, options.prefixLength(), options.maxExpansions(), options.transpositions());
        QueryParsers.setRewriteMethod(query, options.rewriteMethod());
        return query;
    }
    if (fieldType != null) {
        Query termQuery = fieldType.queryStringTermQuery(term);
        if (termQuery != null) {
            return termQuery;
        }
    }
    return new TermQuery(term);
}
Also used : Fuzziness(org.elasticsearch.common.unit.Fuzziness) MatchQuery(org.elasticsearch.index.search.MatchQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery)

Example 5 with Fuzziness

use of org.elasticsearch.common.unit.Fuzziness in project elasticsearch by elastic.

the class FuzzyQueryBuilder method fromXContent.

public static FuzzyQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    Object value = null;
    Fuzziness fuzziness = FuzzyQueryBuilder.DEFAULT_FUZZINESS;
    int prefixLength = FuzzyQueryBuilder.DEFAULT_PREFIX_LENGTH;
    int maxExpansions = FuzzyQueryBuilder.DEFAULT_MAX_EXPANSIONS;
    boolean transpositions = FuzzyQueryBuilder.DEFAULT_TRANSPOSITIONS;
    String rewrite = null;
    String queryName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    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 (TERM_FIELD.match(currentFieldName)) {
                        value = parser.objectBytes();
                    } else if (VALUE_FIELD.match(currentFieldName)) {
                        value = parser.objectBytes();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } 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)) {
                        maxExpansions = parser.intValue();
                    } else if (TRANSPOSITIONS_FIELD.match(currentFieldName)) {
                        transpositions = parser.booleanValue();
                    } else if (REWRITE_FIELD.match(currentFieldName)) {
                        rewrite = parser.textOrNull();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[fuzzy] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectBytes();
        }
    }
    return new FuzzyQueryBuilder(fieldName, value).fuzziness(fuzziness).prefixLength(prefixLength).maxExpansions(maxExpansions).transpositions(transpositions).rewrite(rewrite).boost(boost).queryName(queryName);
}
Also used : Fuzziness(org.elasticsearch.common.unit.Fuzziness) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

Fuzziness (org.elasticsearch.common.unit.Fuzziness)6 HashMap (java.util.HashMap)3 ParsingException (org.elasticsearch.common.ParsingException)3 XContentParser (org.elasticsearch.common.xcontent.XContentParser)3 MatchQuery (org.elasticsearch.index.search.MatchQuery)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Consumer (java.util.function.Consumer)1 Stream (java.util.stream.Stream)1 PostConstruct (javax.annotation.PostConstruct)1 Resource (javax.annotation.Resource)1 Term (org.apache.lucene.index.Term)1 ExtendedCommonTermsQuery (org.apache.lucene.queries.ExtendedCommonTermsQuery)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)1