Skip to content

Commit

Permalink
LUCENE-9365 FuzzyQuery false negative when prefix length == search te…
Browse files Browse the repository at this point in the history
…rm length (#1545)

Co-Authored-By: markharwood <[email protected]>
  • Loading branch information
madrob and markharwood authored Jun 3, 2020
1 parent 90039fc commit 45611d0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void visit(QueryVisitor visitor) {

@Override
protected TermsEnum getTermsEnum(Terms terms, AttributeSource atts) throws IOException {
if (maxEdits == 0 || prefixLength >= term.text().length()) { // can only match if it's exact
if (maxEdits == 0) { // can only match if it's exact
return new SingleTermsEnum(terms.iterator(), term.bytes());
}
return new FuzzyTermsEnum(terms, atts, getTerm(), maxEdits, prefixLength, transpositions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,46 @@ public void testFuzziness() throws Exception {
reader.close();
directory.close();
}


public void testPrefixLengthEqualStringLength() throws Exception {
Directory directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
addDoc("b*a", writer);
addDoc("b*ab", writer);
addDoc("b*abc", writer);
addDoc("b*abcd", writer);
String multibyte = "아프리카코끼리속";
addDoc(multibyte, writer);
IndexReader reader = writer.getReader();
IndexSearcher searcher = newSearcher(reader);
writer.close();

int maxEdits = 0;
int prefixLength = 3;
FuzzyQuery query = new FuzzyQuery(new Term("field", "b*a"), maxEdits, prefixLength);
ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs;
assertEquals(1, hits.length);

maxEdits = 1;
query = new FuzzyQuery(new Term("field", "b*a"), maxEdits, prefixLength);
hits = searcher.search(query, 1000).scoreDocs;
assertEquals(2, hits.length);

maxEdits = 2;
query = new FuzzyQuery(new Term("field", "b*a"), maxEdits, prefixLength);
hits = searcher.search(query, 1000).scoreDocs;
assertEquals(3, hits.length);

maxEdits = 1;
prefixLength = multibyte.length() - 1;
query = new FuzzyQuery(new Term("field", multibyte.substring(0, prefixLength)), maxEdits, prefixLength);
hits = searcher.search(query, 1000).scoreDocs;
assertEquals(1, hits.length);

reader.close();
directory.close();
}

public void test2() throws Exception {
Directory directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), directory, new MockAnalyzer(random(), MockTokenizer.KEYWORD, false));
Expand Down

0 comments on commit 45611d0

Please sign in to comment.