Skip to content

Apache Lucene open-source search software

LUCENE-9427: Fuzzy query should always call consumeTermsMatching in visitor

Julie Tibshirani688583fc2d01
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 55dc5703f3e..f81a8603f54 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -191,7 +191,10 @@ Optimizations
Bug Fixes
---------------------
-(No changes)
+
+* LUCENE-9427: Fix a regression where the unified highlighter didn't produce
+ highlights on fuzzy queries that correspond to exact matches. (Julie Tibshirani)
+
Documentation
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/search/FuzzyQuery.java b/lucene/core/src/java/org/apache/lucene/search/FuzzyQuery.java
index 041f0ca180a..e1359ab3a9d 100644
--- a/lucene/core/src/java/org/apache/lucene/search/FuzzyQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/FuzzyQuery.java
@@ -159,11 +159,7 @@ public class FuzzyQuery extends MultiTermQuery {
@Override
public void visit(QueryVisitor visitor) {
if (visitor.acceptField(field)) {
- if (maxEdits == 0 || prefixLength >= term.text().length()) {
- visitor.consumeTerms(this, term);
- } else {
- visitor.consumeTermsMatching(this, term.field(), () -> getAutomata().runAutomaton);
- }
+ visitor.consumeTermsMatching(this, term.field(), () -> getAutomata().runAutomaton);
}
}
diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/uhighlight/TestUnifiedHighlighterMTQ.java b/lucene/highlighter/src/test/org/apache/lucene/search/uhighlight/TestUnifiedHighlighterMTQ.java
index 098a89bbf45..e0b5debf95d 100644
--- a/lucene/highlighter/src/test/org/apache/lucene/search/uhighlight/TestUnifiedHighlighterMTQ.java
+++ b/lucene/highlighter/src/test/org/apache/lucene/search/uhighlight/TestUnifiedHighlighterMTQ.java
@@ -240,7 +240,7 @@ public class TestUnifiedHighlighterMTQ extends LuceneTestCase {
ir.close();
}
- public void testOneFuzzy() throws Exception {
+ public void testFuzzy() throws Exception {
RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
Field body = new Field("body", "", fieldType);
@@ -274,6 +274,15 @@ public class TestUnifiedHighlighterMTQ extends LuceneTestCase {
assertEquals("This is a <b>test</b>.", snippets[0]);
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
+ // with zero max edits
+ query = new FuzzyQuery(new Term("body", "test"), 0, 2);
+ topDocs = searcher.search(query, 10, Sort.INDEXORDER);
+ assertEquals(2, topDocs.totalHits.value);
+ snippets = highlighter.highlight("body", query, topDocs);
+ assertEquals(2, snippets.length);
+ assertEquals("This is a <b>test</b>.", snippets[0]);
+ assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
+
// wrong field
highlighter.setFieldMatcher(null);//default
BooleanQuery bq = new BooleanQuery.Builder()