Search in sources :

Example 6 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project elasticsearch by elastic.

the class SimpleQueryParser method newFuzzyQuery.

/**
     * Dispatches to Lucene's SimpleQueryParser's newFuzzyQuery, optionally
     * lowercasing the term first
     */
@Override
public Query newFuzzyQuery(String text, int fuzziness) {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.setDisableCoord(true);
    for (Map.Entry<String, Float> entry : weights.entrySet()) {
        final String fieldName = entry.getKey();
        try {
            final BytesRef term = getAnalyzer().normalize(fieldName, text);
            Query query = new FuzzyQuery(new Term(fieldName, term), fuzziness);
            bq.add(wrapWithBoost(query, entry.getValue()), BooleanClause.Occur.SHOULD);
        } catch (RuntimeException e) {
            rethrowUnlessLenient(e);
        }
    }
    return super.simplify(bq.build());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Example 7 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project elasticsearch by elastic.

the class FuzzyQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query query = null;
    String rewrite = this.rewrite;
    if (rewrite == null && context.isFilter()) {
        rewrite = QueryParsers.CONSTANT_SCORE.getPreferredName();
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        query = fieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions);
    }
    if (query == null) {
        int maxEdits = fuzziness.asDistance(BytesRefs.toString(value));
        query = new FuzzyQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), maxEdits, prefixLength, maxExpansions, transpositions);
    }
    if (query instanceof MultiTermQuery) {
        MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(rewrite, null);
        QueryParsers.setRewriteMethod((MultiTermQuery) query, rewriteMethod);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 8 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project stargate-core by tuplejump.

the class FuzzyCondition method query.

/**
     * {@inheritDoc}
     */
@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;
    Type fieldType = properties != null ? properties.getType() : Type.text;
    if (fieldType == Type.string || fieldType == Type.text) {
        String analyzedValue = analyze(field, value, schema.analyzer);
        Term term = new Term(field, analyzedValue);
        Query query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
        return query;
    }
    message = String.format("Fuzzy queries cannot be supported for field type %s", fieldType);
    throw new UnsupportedOperationException(message);
}
Also used : Type(com.tuplejump.stargate.lucene.Type) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term) Properties(com.tuplejump.stargate.lucene.Properties)

Example 9 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.

the class TestExtendedDismaxParser method containsClause.

private boolean containsClause(Query query, String field, String value, int boost, boolean fuzzy) {
    float queryBoost = 1f;
    if (query instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) query;
        query = bq.getQuery();
        queryBoost = bq.getBoost();
    }
    if (query instanceof BooleanQuery) {
        return containsClause((BooleanQuery) query, field, value, boost, fuzzy);
    }
    if (query instanceof DisjunctionMaxQuery) {
        return containsClause((DisjunctionMaxQuery) query, field, value, boost, fuzzy);
    }
    if (boost != queryBoost) {
        return false;
    }
    if (query instanceof TermQuery && !fuzzy) {
        return containsClause((TermQuery) query, field, value);
    }
    if (query instanceof FuzzyQuery && fuzzy) {
        return containsClause((FuzzyQuery) query, field, value);
    }
    return false;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 10 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.

the class TestQueryWrapperFilter method testBasic.

public void testBasic() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(newTextField("field", "value", Field.Store.NO));
    writer.addDocument(doc);
    IndexReader reader = writer.getReader();
    writer.close();
    TermQuery termQuery = new TermQuery(new Term("field", "value"));
    // should not throw exception with primitive query
    QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery);
    IndexSearcher searcher = newSearcher(reader);
    TopDocs hits = searcher.search(qwf, 10);
    assertEquals(1, hits.totalHits);
    hits = searcher.search(new FilterWrapper(qwf), 10);
    assertEquals(1, hits.totalHits);
    // should not throw exception with complex primitive query
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    booleanQuery.add(termQuery, Occur.MUST);
    booleanQuery.add(new TermQuery(new Term("field", "missing")), Occur.MUST_NOT);
    qwf = new QueryWrapperFilter(termQuery);
    hits = searcher.search(qwf, 10);
    assertEquals(1, hits.totalHits);
    hits = searcher.search(new FilterWrapper(qwf), 10);
    assertEquals(1, hits.totalHits);
    // should not throw exception with non primitive Query (doesn't implement
    // Query#createWeight)
    qwf = new QueryWrapperFilter(new FuzzyQuery(new Term("field", "valu")));
    hits = searcher.search(qwf, 10);
    assertEquals(1, hits.totalHits);
    hits = searcher.search(new FilterWrapper(qwf), 10);
    assertEquals(1, hits.totalHits);
    // test a query with no hits
    termQuery = new TermQuery(new Term("field", "not_exist"));
    qwf = new QueryWrapperFilter(termQuery);
    hits = searcher.search(qwf, 10);
    assertEquals(0, hits.totalHits);
    hits = searcher.search(new FilterWrapper(qwf), 10);
    assertEquals(0, hits.totalHits);
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) TopDocs(org.apache.lucene.search.TopDocs) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

FuzzyQuery (org.apache.lucene.search.FuzzyQuery)34 Term (org.apache.lucene.index.Term)26 PrefixQuery (org.apache.lucene.search.PrefixQuery)20 BooleanQuery (org.apache.lucene.search.BooleanQuery)17 BoostQuery (org.apache.lucene.search.BoostQuery)16 Query (org.apache.lucene.search.Query)16 TermQuery (org.apache.lucene.search.TermQuery)12 WildcardQuery (org.apache.lucene.search.WildcardQuery)12 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)8 PhraseQuery (org.apache.lucene.search.PhraseQuery)8 RegexpQuery (org.apache.lucene.search.RegexpQuery)7 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)6 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)6 BooleanClause (org.apache.lucene.search.BooleanClause)5 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)5 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)5 TopDocs (org.apache.lucene.search.TopDocs)5 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)5 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)5 Map (java.util.Map)4