Skip to content

Apache Lucene open-source search software

LUCENE-1299: Properly handle when IndexReader is not null and Field is in the spell checker

Grant Ingersoll8c97e9b87ba2
diff --git a/CHANGES.txt b/CHANGES.txt
index 28b93ad8b48..b7315ab9c0c 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -117,6 +117,8 @@ Bug fixes
11. LUCENE-1189: Fixed the QueryParser to handle escaped characters within
quoted terms correctly. (Tomer Gabel via Michael Busch)
+12. LUCENE-1299: Fixed NPE in SpellChecker when IndexReader is not null and field is (Grant Ingersoll)
+
New features
1. LUCENE-1137: Added Token.set/getFlags() accessors for passing more information about a Token through the analysis
diff --git a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java
index 9274b26c2cf..05972215aac 100755
--- a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java
+++ b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java
@@ -224,7 +224,7 @@ public class SpellChecker {
continue;
}
- if (ir != null) { // use the user index
+ if (ir != null && field != null) { // use the user index
sugWord.freq = ir.docFreq(new Term(field, sugWord.string)); // freq in the index
// don't suggest a word that is not present in the field
if ((morePopular && goalFreq > sugWord.freq) || sugWord.freq < 1) {
diff --git a/contrib/spellchecker/src/test/org/apache/lucene/search/spell/TestSpellChecker.java b/contrib/spellchecker/src/test/org/apache/lucene/search/spell/TestSpellChecker.java
index 169905fae97..763c76a3201 100755
--- a/contrib/spellchecker/src/test/org/apache/lucene/search/spell/TestSpellChecker.java
+++ b/contrib/spellchecker/src/test/org/apache/lucene/search/spell/TestSpellChecker.java
@@ -109,6 +109,14 @@ public class TestSpellChecker extends TestCase {
similar = spellChecker.suggestSimilar("tousand", 10, r, "field2", false);
assertEquals(1, similar.length); // there is the term thousand in the field field2
+
+ try {
+ similar = spellChecker.suggestSimilar("tousand", 10, r, null, false);
+ } catch (NullPointerException e) {
+ assertTrue("threw an NPE, and it shouldn't have", false);
+ }
+
+
}
private void addwords(IndexReader r, String field) throws IOException {