Skip to content

Commit

Permalink
FuzzyQuery produces a wrong result when prefix is equal to the term l…
Browse files Browse the repository at this point in the history
…ength (#1002)

* FuzzyQuery produces a wrong result when prefix is equal to the term
length
#941

* Add LUCENENET-specific backport comment, fix test code style

---------

Co-authored-by: tohidemyname <tohidemyname>
  • Loading branch information
paulirwin authored Nov 4, 2024
1 parent ca3bdac commit 98af2bb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
44 changes: 43 additions & 1 deletion src/Lucene.Net.Tests/Search/TestFuzzyQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,48 @@ public virtual void TestFuzziness()
directory.Dispose();
}

// LUCENENET-specific: backported fix from Lucene 9.0.0 (lucene@45611d0, LUCENE-9365)
[Test]
public void TestPrefixLengthEqualStringLength()
{
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);
const string multibyte = "아프리카코끼리속"; // LUCENENET-specific: made const
AddDoc(multibyte, writer);
IndexReader reader = writer.GetReader();
IndexSearcher searcher = NewSearcher(reader);
writer.Dispose();

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.Dispose();
directory.Dispose();
}

[Test]
public virtual void Test2()
{
Expand Down Expand Up @@ -384,4 +426,4 @@ private void AddDoc(string text, RandomIndexWriter writer)
writer.AddDocument(doc);
}
}
}
}
5 changes: 3 additions & 2 deletions src/Lucene.Net/Search/FuzzyQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public FuzzyQuery(Term term)

protected override TermsEnum GetTermsEnum(Terms terms, AttributeSource atts)
{
if (maxEdits == 0 || prefixLength >= term.Text.Length) // can only match if it's exact
// LUCENENET-specific: backported fix from Lucene 9.0.0 (lucene@45611d0, LUCENE-9365)
if (maxEdits == 0) // can only match if it's exact
{
return new SingleTermsEnum(terms.GetEnumerator(), term.Bytes);
}
Expand Down Expand Up @@ -262,4 +263,4 @@ public static int SingleToEdits(float minimumSimilarity, int termLen)
}
}
}
}
}

0 comments on commit 98af2bb

Please sign in to comment.