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"));
}
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 });
}
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());
}
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);
}
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);
}
Aggregations