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

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

Introduction

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

Prototype

boolean defaultTranspositions

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

Click Source Link

Usage

From source file:jp.scaleout.elasticsearch.plugins.queryparser.classic.MapperQueryParser.java

License:Apache License

private Query getFuzzyQuerySingle(String field, String termStr, String minSimilarity) throws ParseException {
    currentFieldType = parseContext.fieldMapper(field);
    if (currentFieldType != null) {
        try {/*from  w  w  w . j  av a 2 s . c  o m*/
            return currentFieldType.fuzzyQuery(termStr, Fuzziness.build(minSimilarity), fuzzyPrefixLength,
                    settings.fuzzyMaxExpansions(), FuzzyQuery.defaultTranspositions);
        } catch (RuntimeException e) {
            if (settings.lenient()) {
                return null;
            }
            throw e;
        }
    }
    return super.getFuzzyQuery(field, termStr, Float.parseFloat(minSimilarity));
}

From source file:jp.scaleout.elasticsearch.plugins.queryparser.classic.MapperQueryParser.java

License:Apache License

@Override
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength) {
    String text = term.text();/*from   w  ww  . ja v  a 2  s  .c o m*/
    int numEdits = FuzzyQuery.floatToEdits(minimumSimilarity, text.codePointCount(0, text.length()));
    FuzzyQuery query = new FuzzyQuery(term, numEdits, prefixLength, settings.fuzzyMaxExpansions(),
            FuzzyQuery.defaultTranspositions);
    QueryParsers.setRewriteMethod(query, settings.fuzzyRewriteMethod());
    return query;
}

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;//from ww w. j  av a 2  s .co  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.MatchQueryBuilder.java

License:Apache License

public static Optional<MatchQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;//from   w  w  w.j a va2 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);
}