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

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

Introduction

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

Prototype

public FuzzyQuery(Term term, int maxEdits, int prefixLength, int maxExpansions, boolean transpositions) 

Source Link

Document

Create a new FuzzyQuery that will match terms with an edit distance of at most maxEdits to term.

Usage

From source file:com.sindicetech.siren.search.node.TestNodeFuzzyQuery.java

License:Open Source License

/**
 * MultiTermQuery provides (via attribute) information about which values
 * must be competitive to enter the priority queue.
 *
 * FuzzyQuery optimizes itself around this information, if the attribute
 * is not implemented correctly, there will be problems!
 *//*from  ww w  .j a  v a  2  s  . c o  m*/
public void testTieBreaker() throws Exception {
    this.addDocument("<a123456>");
    this.addDocument("<c123456>");
    this.addDocument("<d123456>");
    this.addDocument("<e123456>");

    final Directory directory2 = newDirectory();
    final RandomIndexWriter writer2 = newRandomIndexWriter(directory2, analyzer, codec);
    addDocument(writer2, "<a123456>");
    addDocument(writer2, "<b123456>");
    addDocument(writer2, "<b123456>");
    addDocument(writer2, "<b123456>");
    addDocument(writer2, "<c123456>");
    addDocument(writer2, "<f123456>");

    final IndexReader ir1 = writer.getReader();
    final IndexReader ir2 = writer2.getReader();

    final MultiReader mr = new MultiReader(ir1, ir2);
    final IndexSearcher searcher = newSearcher(mr);
    final FuzzyQuery fq = new FuzzyQuery(new Term(DEFAULT_TEST_FIELD, "z123456"), 1, 0, 2, false);
    final TopDocs docs = searcher.search(fq, 2);
    assertEquals(5, docs.totalHits); // 5 docs, from the a and b's

    mr.close();
    ir2.close();
    writer2.close();
    directory2.close();
}

From source file:com.stratio.cassandra.index.query.FuzzyCondition.java

License:Apache License

/** {@inheritDoc} */
@Override/*from  w ww.ja v a2  s  . co m*/
public Query query(Schema schema) {

    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    if (value == null || value.trim().isEmpty()) {
        throw new IllegalArgumentException("Field value required");
    }
    if (maxEdits < 0 || maxEdits > 2) {
        throw new IllegalArgumentException("max_edits must be between 0 and 2");
    }
    if (prefixLength < 0) {
        throw new IllegalArgumentException("prefix_length must be positive.");
    }
    if (maxExpansions < 0) {
        throw new IllegalArgumentException("max_expansions must be positive.");
    }

    ColumnMapper<?> columnMapper = schema.getMapper(field);
    if (columnMapper == null) {
        throw new IllegalArgumentException("Not found mapper for field " + field);
    }
    Class<?> clazz = columnMapper.baseClass();
    if (clazz == String.class) {
        String analyzedValue = analyze(field, value, columnMapper);
        Term term = new Term(field, analyzedValue);
        Query query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
        query.setBoost(boost);
        return query;
    } else {
        String message = String.format("Fuzzy queries are not supported by %s mapper", clazz.getSimpleName());
        throw new UnsupportedOperationException(message);
    }
}

From source file:com.stratio.cassandra.lucene.search.condition.FuzzyCondition.java

License:Apache License

/** {@inheritDoc} */
@Override/*from  w  ww . j  a  v  a  2s .  c o m*/
public Query query(Schema schema) {
    SingleColumnMapper<?> columnMapper = getMapper(schema, field);
    Class<?> clazz = columnMapper.baseClass();
    if (clazz == String.class) {
        Term term = new Term(field, value);
        Query query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
        query.setBoost(boost);
        return query;
    } else {
        String message = String.format("Fuzzy queries are not supported by %s mapper", clazz.getSimpleName());
        throw new UnsupportedOperationException(message);
    }
}

From source file:com.tuplejump.stargate.lucene.query.FuzzyCondition.java

License:Apache License

/**
 * {@inheritDoc}//from w w  w.  j  a v  a  2  s.  c  om
 */
@Override
public Query query(Options schema) {

    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    if (value == null || value.trim().isEmpty()) {
        throw new IllegalArgumentException("Field value required");
    }
    if (maxEdits < 0 || maxEdits > 2) {
        throw new IllegalArgumentException("max_edits must be between 0 and 2");
    }
    if (prefixLength < 0) {
        throw new IllegalArgumentException("prefix_length must be positive.");
    }
    if (maxExpansions < 0) {
        throw new IllegalArgumentException("max_expansions must be positive.");
    }

    Properties properties = schema.getProperties(field);
    String message;
    Properties.Type fieldType = properties != null ? properties.getType() : Properties.Type.text;
    if (fieldType == Properties.Type.string || fieldType == Properties.Type.text) {
        String analyzedValue = analyze(field, value, schema.analyzer);
        Term term = new Term(field, analyzedValue);
        Query query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
        query.setBoost(boost);
        return query;
    }
    message = String.format("Fuzzy queries cannot be supported for field type %s", fieldType);
    throw new UnsupportedOperationException(message);
}

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

License:Apache License

protected Query blendTermQuery(Term term, FieldMapper mapper) {
    Fuzziness fuzziness = options.fuzziness();
    if (fuzziness != null) {
        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;
    }/*from   ww  w  . j av a  2 s.c  om*/
    if (mapper != null) {
        Query termQuery = mapper.queryStringTermQuery(term);
        if (termQuery != null) {
            return termQuery;
        }
    }
    return new TermQuery(term);
}

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();/* w ww.j  a  v a  2 s  .  c  om*/
    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.apache.blur.lucene.serializer.FuzzyQueryWritable.java

License:Apache License

@Override
public void readFields(DataInput in) throws IOException {
    float boost = in.readFloat();
    TermWritable termWritable = new TermWritable();
    termWritable.readFields(in);/*from w  w  w .j  ava  2 s . c om*/
    Term term = termWritable.getTerm();
    int maxEdits = in.readInt();
    int prefixLength = in.readInt();
    int maxExpansions = in.readInt();
    boolean transpositions = in.readBoolean();
    query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
    query.setBoost(boost);
}

From source file:org.codelibs.elasticsearch.index.mapper.StringFieldType.java

License:Apache License

@Override
public final Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions,
        boolean transpositions) {
    failIfNotIndexed();//w  w w.  j av a2 s  .  com
    return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)),
            fuzziness.asDistance(BytesRefs.toString(value)), prefixLength, maxExpansions, transpositions);
}

From source file:org.codelibs.elasticsearch.index.search.MatchQuery.java

License:Apache License

protected Query blendTermQuery(Term term, MappedFieldType fieldType) {
    if (fuzziness != null) {
        if (fieldType != null) {
            try {
                Query query = fieldType.fuzzyQuery(term.text(), fuzziness, fuzzyPrefixLength, maxExpansions,
                        transpositions);
                if (query instanceof FuzzyQuery) {
                    QueryParsers.setRewriteMethod((FuzzyQuery) query, fuzzyRewriteMethod);
                }/*from   w  w  w. j av a 2s .c  o m*/
                return query;
            } catch (RuntimeException e) {
                if (lenient) {
                    return new TermQuery(term);
                } else {
                    throw e;
                }
            }
        }
        int edits = fuzziness.asDistance(term.text());
        FuzzyQuery query = new FuzzyQuery(term, edits, fuzzyPrefixLength, maxExpansions, transpositions);
        QueryParsers.setRewriteMethod(query, fuzzyRewriteMethod);
        return query;
    }
    if (fieldType != null) {
        Query query = termQuery(fieldType, term.bytes(), lenient);
        if (query != null) {
            return query;
        }
    }
    return new TermQuery(term);
}

From source file:org.elasticsearch.index.mapper.core.AbstractFieldMapper.java

License:Apache License

@Override
public Query fuzzyQuery(String value, Fuzziness fuzziness, int prefixLength, int maxExpansions,
        boolean transpositions) {
    return new FuzzyQuery(names.createIndexNameTerm(indexedValueForSearch(value)), fuzziness.asDistance(value),
            prefixLength, maxExpansions, transpositions);
}