Skip to content

Apache Lucene open-source search software

Hunspell suggestions: Ensure candidate roots are not worse before updating (#15085)

ilia-permiashkin1d51cc94f464
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index a25e1ceac8c..e843976c7f6 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -65,6 +65,8 @@ Optimizations
* GITHUB#15043: Vectorize L2 normalize function. (Ramakrishna Chilaka)
+* GITHUB#15085: Hunspell suggestions: Ensure candidate roots are not worse before updating. (Ilia Permiashkin)
+
Bug Fixes
---------------------
* GITHUB#14049: Randomize KNN codec params in RandomCodec. Fixes scalar quantization div-by-zero
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java
index 4df7610bde8..4e733635098 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java
@@ -92,14 +92,17 @@ class GeneratingSuggester {
speller.checkCanceled.run();
- String root = rootChars.toString();
IntsRef forms = entry.forms();
for (int i = 0; i < forms.length; i++) {
- if (isSuggestible.test(forms.ints[forms.offset + i])) {
- roots.add(new Weighted<>(new Root<>(root, forms.ints[forms.offset + i]), sc));
- if (roots.size() == MAX_ROOTS) {
- roots.poll();
- }
+ int form = forms.ints[forms.offset + i];
+ if (!isSuggestible.test(form)
+ || roots.size() == MAX_ROOTS && isWorseThan(sc, rootChars, roots.peek())) {
+ continue;
+ }
+
+ roots.add(new Weighted<>(new Root<>(rootChars.toString(), form), sc));
+ if (roots.size() > MAX_ROOTS) {
+ roots.poll();
}
}
});