diff --git a/modules/suggest/src/java/org/apache/lucene/search/spell/DirectSpellChecker.java b/modules/suggest/src/java/org/apache/lucene/search/spell/DirectSpellChecker.java index 8dd9fc1fbf9..36804eed92a 100644 --- a/modules/suggest/src/java/org/apache/lucene/search/spell/DirectSpellChecker.java +++ b/modules/suggest/src/java/org/apache/lucene/search/spell/DirectSpellChecker.java @@ -28,6 +28,7 @@ import java.util.PriorityQueue; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.MultiFields; import org.apache.lucene.index.Term; +import org.apache.lucene.index.Terms; import org.apache.lucene.search.FuzzyTermsEnum; import org.apache.lucene.search.BoostAttribute; import org.apache.lucene.search.MaxNonCompetitiveBoostAttribute; @@ -395,7 +396,11 @@ public class DirectSpellChecker { AttributeSource atts = new AttributeSource(); MaxNonCompetitiveBoostAttribute maxBoostAtt = atts.addAttribute(MaxNonCompetitiveBoostAttribute.class); - FuzzyTermsEnum e = new FuzzyTermsEnum(MultiFields.getTerms(ir, term.field()).iterator(), atts, term, editDistance, Math.max(minPrefix, editDistance-1)); + Terms terms = MultiFields.getTerms(ir, term.field()); + if (terms == null) { + return Collections.emptyList(); + } + FuzzyTermsEnum e = new FuzzyTermsEnum(terms.iterator(), atts, term, editDistance, Math.max(minPrefix, editDistance-1)); final PriorityQueue stQueue = new PriorityQueue(); BytesRef queryTerm = new BytesRef(term.text()); diff --git a/modules/suggest/src/test/org/apache/lucene/search/spell/TestDirectSpellChecker.java b/modules/suggest/src/test/org/apache/lucene/search/spell/TestDirectSpellChecker.java index 3de4a91959a..6685af41ba3 100644 --- a/modules/suggest/src/test/org/apache/lucene/search/spell/TestDirectSpellChecker.java +++ b/modules/suggest/src/test/org/apache/lucene/search/spell/TestDirectSpellChecker.java @@ -141,4 +141,25 @@ public class TestDirectSpellChecker extends LuceneTestCase { writer.close(); dir.close(); } + + public void testBogusField() throws Exception { + DirectSpellChecker spellChecker = new DirectSpellChecker(); + Directory dir = newDirectory(); + RandomIndexWriter writer = new RandomIndexWriter(random, dir, + new MockAnalyzer(random, MockTokenizer.SIMPLE, true)); + + for (int i = 0; i < 20; i++) { + Document doc = new Document(); + doc.add(newField("numbers", English.intToEnglish(i), Field.Store.NO, Field.Index.ANALYZED)); + writer.addDocument(doc); + } + + IndexReader ir = writer.getReader(); + + SuggestWord[] similar = spellChecker.suggestSimilar(new Term("bogusFieldBogusField", "fvie"), 2, ir, false); + assertEquals(0, similar.length); + ir.close(); + writer.close(); + dir.close(); + } } diff --git a/modules/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java b/modules/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java index 85313a0a811..8da6cd4466a 100755 --- a/modules/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java +++ b/modules/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java @@ -136,6 +136,20 @@ public class TestSpellChecker extends LuceneTestCase { compareSP.close(); compIdx.close(); } + + public void testBogusField() throws Exception { + IndexReader r = IndexReader.open(userindex, true); + Directory compIdx = newDirectory(); + SpellChecker compareSP = new SpellCheckerMock(compIdx, new LevensteinDistance(), new SuggestWordFrequencyComparator()); + addwords(r, compareSP, "field3"); + + String[] similar = compareSP.suggestSimilar("fvie", 2, r, "bogusFieldBogusField", false); + assertEquals(0, similar.length); + r.close(); + if (!compareSP.isClosed()) + compareSP.close(); + compIdx.close(); + } private void checkCommonSuggestions(IndexReader r) throws IOException { String[] similar = spellChecker.suggestSimilar("fvie", 2);