Search in sources :

Example 16 with FuzzyQuery

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

the class TestSimpleQueryParser method testFuzzy.

/** test a fuzzy query */
public void testFuzzy() throws Exception {
    Query regular = new TermQuery(new Term("field", "foobar"));
    Query expected = new FuzzyQuery(new Term("field", "foobar"), 2);
    assertEquals(expected, parse("foobar~2"));
    assertEquals(expected, parse("foobar~"));
    assertEquals(regular, parse("foobar~a"));
    assertEquals(regular, parse("foobar~1a"));
    BooleanQuery.Builder bool = new BooleanQuery.Builder();
    FuzzyQuery fuzzy = new FuzzyQuery(new Term("field", "foo"), LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE);
    bool.add(fuzzy, Occur.MUST);
    bool.add(new TermQuery(new Term("field", "bar")), Occur.MUST);
    assertEquals(bool.build(), parse("foo~" + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + 1 + " bar"));
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term)

Example 17 with FuzzyQuery

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

the class TestSpans method testSpanNotWithMultiterm.

public void testSpanNotWithMultiterm() throws Exception {
    SpanQuery q = spanNotQuery(spanTermQuery(field, "r1"), new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(field, "s1"))), 3, 3);
    checkHits(q, new int[] { 14 });
    q = spanNotQuery(spanTermQuery(field, "r1"), new SpanMultiTermQueryWrapper<>(new FuzzyQuery(new Term(field, "s12"), 1, 2)), 3, 3);
    checkHits(q, new int[] { 14 });
    q = spanNotQuery(new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(field, "r"))), spanTermQuery(field, "s21"), 3, 3);
    checkHits(q, new int[] { 13 });
}
Also used : PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term)

Example 18 with FuzzyQuery

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

the class QueryStringQueryBuilderTests method testToQueryFuzzyQueryAutoFuziness.

public void testToQueryFuzzyQueryAutoFuziness() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    int length = randomIntBetween(1, 10);
    StringBuilder queryString = new StringBuilder();
    for (int i = 0; i < length; i++) {
        queryString.append("a");
    }
    queryString.append("~");
    int expectedEdits;
    if (length <= 2) {
        expectedEdits = 0;
    } else if (3 <= length && length <= 5) {
        expectedEdits = 1;
    } else {
        expectedEdits = 2;
    }
    Query query = queryStringQuery(queryString.toString()).defaultField(STRING_FIELD_NAME).fuzziness(Fuzziness.AUTO).toQuery(createShardContext());
    assertThat(query, instanceOf(FuzzyQuery.class));
    FuzzyQuery fuzzyQuery = (FuzzyQuery) query;
    assertEquals(expectedEdits, fuzzyQuery.getMaxEdits());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery)

Example 19 with FuzzyQuery

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

the class QueryStringQueryBuilderTests method testExpandedTerms.

public void testExpandedTerms() throws Exception {
    // Prefix
    Query query = new QueryStringQueryBuilder("aBc*").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new PrefixQuery(new Term(STRING_FIELD_NAME, "aBc")), query);
    query = new QueryStringQueryBuilder("aBc*").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new PrefixQuery(new Term(STRING_FIELD_NAME, "abc")), query);
    // Wildcard
    query = new QueryStringQueryBuilder("aBc*D").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new WildcardQuery(new Term(STRING_FIELD_NAME, "aBc*D")), query);
    query = new QueryStringQueryBuilder("aBc*D").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new WildcardQuery(new Term(STRING_FIELD_NAME, "abc*d")), query);
    // Fuzzy
    query = new QueryStringQueryBuilder("aBc~1").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    FuzzyQuery fuzzyQuery = (FuzzyQuery) query;
    assertEquals(new Term(STRING_FIELD_NAME, "aBc"), fuzzyQuery.getTerm());
    query = new QueryStringQueryBuilder("aBc~1").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    fuzzyQuery = (FuzzyQuery) query;
    assertEquals(new Term(STRING_FIELD_NAME, "abc"), fuzzyQuery.getTerm());
    // Range
    query = new QueryStringQueryBuilder("[aBc TO BcD]").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new TermRangeQuery(STRING_FIELD_NAME, new BytesRef("aBc"), new BytesRef("BcD"), true, true), query);
    query = new QueryStringQueryBuilder("[aBc TO BcD]").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new TermRangeQuery(STRING_FIELD_NAME, new BytesRef("abc"), new BytesRef("bcd"), true, true), query);
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef)

Example 20 with FuzzyQuery

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

the class SimpleQueryStringBuilderTests method testExpandedTerms.

public void testExpandedTerms() throws Exception {
    // Prefix
    Query query = new SimpleQueryStringBuilder("aBc*").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new PrefixQuery(new Term(STRING_FIELD_NAME, "aBc")), query);
    query = new SimpleQueryStringBuilder("aBc*").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new PrefixQuery(new Term(STRING_FIELD_NAME, "abc")), query);
    // Fuzzy
    query = new SimpleQueryStringBuilder("aBc~1").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    FuzzyQuery expected = new FuzzyQuery(new Term(STRING_FIELD_NAME, "aBc"), 1);
    assertEquals(expected, query);
    query = new SimpleQueryStringBuilder("aBc~1").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    expected = new FuzzyQuery(new Term(STRING_FIELD_NAME, "abc"), 1);
    assertEquals(expected, query);
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term)

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