LUCENE-4567: avoid NPE if no suggestions were built

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1411796 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2012-11-20 19:00:38 +00:00
parent 84cdc246f4
commit ee55158d07
6 changed files with 44 additions and 2 deletions

View File

@ -147,6 +147,10 @@ Bug Fixes
allow 1+maxMergeCount merges threads to be created, instead of just
maxMergeCount (Radim Kolar, Mike McCandless)
* LUCENE-4567: Fixed NullPointerException in analzying, fuzzy, and
WFST suggesters when no suggestions were added (selckin via Mike
McCandless)
Optimizations
* LUCENE-2221: oal.util.BitUtil was modified to use Long.bitCount and

View File

@ -23,6 +23,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
@ -530,6 +531,10 @@ public class AnalyzingSuggester extends Lookup {
public boolean store(OutputStream output) throws IOException {
DataOutput dataOut = new OutputStreamDataOutput(output);
try {
if (fst == null) {
return false;
}
fst.save(dataOut);
dataOut.writeVInt(maxAnalyzedPathsForOneInput);
} finally {
@ -557,6 +562,9 @@ public class AnalyzingSuggester extends Lookup {
if (onlyMorePopular) {
throw new IllegalArgumentException("this suggester only works with onlyMorePopular=false");
}
if (fst == null) {
return Collections.emptyList();
}
//System.out.println("lookup key=" + key + " num=" + num);
final BytesRef utf8Key = new BytesRef(key);

View File

@ -147,6 +147,10 @@ public class WFSTCompletionLookup extends Lookup {
throw new IllegalArgumentException("this suggester only works with onlyMorePopular=false");
}
if (fst == null) {
return Collections.emptyList();
}
BytesRef scratch = new BytesRef(key);
int prefixLength = scratch.length;
Arc<Long> arc = new Arc<Long>();
@ -219,6 +223,9 @@ public class WFSTCompletionLookup extends Lookup {
* or null if it does not exist.
*/
public Object get(CharSequence key) {
if (fst == null) {
return null;
}
Arc<Long> arc = new Arc<Long>();
Long result = null;
try {

View File

@ -134,6 +134,15 @@ public class AnalyzingSuggesterTest extends LuceneTestCase {
assertEquals(50, results.get(0).value, 0.01F);
}
public void testEmpty() throws Exception {
Analyzer standard = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, true, MockTokenFilter.ENGLISH_STOPSET, false);
AnalyzingSuggester suggester = new AnalyzingSuggester(standard);
suggester.build(new TermFreqArrayIterator(new TermFreq[0]));
List<LookupResult> result = suggester.lookup("a", false, 20);
assertTrue(result.isEmpty());
}
public void testNoSeps() throws Exception {
TermFreq[] keys = new TermFreq[] {
new TermFreq("ab cd", 0),

View File

@ -263,8 +263,14 @@ public class FuzzySuggesterTest extends LuceneTestCase {
assertEquals("wi fi network is fast", results.get(1).key);
assertEquals(10, results.get(1).value);
}
public void testEmpty() throws Exception {
FuzzySuggester suggester = new FuzzySuggester(new MockAnalyzer(random(), MockTokenizer.KEYWORD, false));
suggester.build(new TermFreqArrayIterator(new TermFreq[0]));
List<LookupResult> result = suggester.lookup("a", false, 20);
assertTrue(result.isEmpty());
}
public void testInputPathRequired() throws Exception {

View File

@ -209,4 +209,12 @@ public class WFSTCompletionTest extends LuceneTestCase {
new TermFreq(key2, 50),
}));
}
public void testEmpty() throws Exception {
WFSTCompletionLookup suggester = new WFSTCompletionLookup(false);
suggester.build(new TermFreqArrayIterator(new TermFreq[0]));
List<LookupResult> result = suggester.lookup("a", false, 20);
assertTrue(result.isEmpty());
}
}