Search in sources :

Example 1 with FuzzyQuery

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

the class MatchQuery method blendTermQuery.

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);
                }
                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);
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery)

Example 2 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 3 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 4 with FuzzyQuery

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

the class FuzzyQueryBuilderTests method testToQueryWithStringField.

public void testToQueryWithStringField() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    String query = "{\n" + "    \"fuzzy\":{\n" + "        \"" + STRING_FIELD_NAME + "\":{\n" + "            \"value\":\"sh\",\n" + "            \"fuzziness\": \"AUTO\",\n" + "            \"prefix_length\":1,\n" + "            \"boost\":2.0\n" + "        }\n" + "    }\n" + "}";
    Query parsedQuery = parseQuery(query).toQuery(createShardContext());
    assertThat(parsedQuery, instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) parsedQuery;
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
    assertThat(boostQuery.getQuery(), instanceOf(FuzzyQuery.class));
    FuzzyQuery fuzzyQuery = (FuzzyQuery) boostQuery.getQuery();
    assertThat(fuzzyQuery.getTerm(), equalTo(new Term(STRING_FIELD_NAME, "sh")));
    assertThat(fuzzyQuery.getMaxEdits(), equalTo(Fuzziness.AUTO.asDistance("sh")));
    assertThat(fuzzyQuery.getPrefixLength(), equalTo(1));
}
Also used : Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 5 with FuzzyQuery

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

the class TextFieldTypeTests method testFuzzyQuery.

public void testFuzzyQuery() {
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);
    assertEquals(new FuzzyQuery(new Term("field", "foo"), 2, 1, 50, true), ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true));
    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Aggregations

FuzzyQuery (org.apache.lucene.search.FuzzyQuery)38 Term (org.apache.lucene.index.Term)29 PrefixQuery (org.apache.lucene.search.PrefixQuery)22 BooleanQuery (org.apache.lucene.search.BooleanQuery)19 Query (org.apache.lucene.search.Query)19 BoostQuery (org.apache.lucene.search.BoostQuery)17 TermQuery (org.apache.lucene.search.TermQuery)15 WildcardQuery (org.apache.lucene.search.WildcardQuery)14 PhraseQuery (org.apache.lucene.search.PhraseQuery)10 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)9 RegexpQuery (org.apache.lucene.search.RegexpQuery)7 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)6 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)6 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)6 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)6 Document (org.apache.lucene.document.Document)5 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)5 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)5 SynonymQuery (org.apache.lucene.search.SynonymQuery)5 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)5