LUCENE-6093: don't throw NPE when BlendedInfixSuggester.lookup doesn't have a prefix token

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1656173 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2015-01-31 09:41:49 +00:00
parent a705371bfc
commit b5db48c783
4 changed files with 38 additions and 3 deletions

View File

@ -484,6 +484,10 @@ Bug Fixes
high frequency terms in extremely large indices (Robert Muir, Mike
McCandless)
* LUCENE-6093: Don't throw NullPointerException from
BlendedInfixSuggester for lookups that do not end in a prefix
token. (jane chang via Mike McCandless)
Documentation
* LUCENE-5392: Add/improve analysis package documentation to reflect

View File

@ -576,7 +576,11 @@ public class AnalyzingInfixSuggester extends Lookup implements Closeable {
/**
* Create the results based on the search hits.
* Can be overridden by subclass to add particular behavior (e.g. weight transformation)
* Can be overridden by subclass to add particular behavior (e.g. weight transformation).
* Note that there is no prefix toke (the {@code prefixToken} argument will
* be null) whenever the final token in the incoming request was in fact finished
* (had trailing characters, such as white-space).
*
* @throws IOException If there are problems reading fields from the underlying Lucene index.
*/
protected List<LookupResult> createResults(IndexSearcher searcher, TopFieldDocs hits, int num,

View File

@ -261,8 +261,8 @@ public class BlendedInfixSuggester extends AnalyzingInfixSuggester {
String docTerm = term.utf8ToString();
if (matchedTokens.contains(docTerm) || docTerm.startsWith(prefixToken)) {
if (matchedTokens.contains(docTerm) || (prefixToken != null && docTerm.startsWith(prefixToken))) {
DocsAndPositionsEnum docPosEnum = it.docsAndPositions(null, null, DocsAndPositionsEnum.FLAG_OFFSETS);
docPosEnum.nextDoc();

View File

@ -165,6 +165,33 @@ public class BlendedInfixSuggesterTest extends LuceneTestCase {
suggester.close();
}
/**
* Handle trailing spaces that result in no prefix token LUCENE-6093
*/
public void testNullPrefixToken() throws IOException {
BytesRef payload = new BytesRef("lake");
Input keys[] = new Input[]{
new Input("top of the lake", 8, payload)
};
Path tempDir = createTempDir("BlendedInfixSuggesterTest");
Analyzer a = new StandardAnalyzer(CharArraySet.EMPTY_SET);
BlendedInfixSuggester suggester = new BlendedInfixSuggester(newFSDirectory(tempDir), a, a,
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS,
BlendedInfixSuggester.BlenderType.POSITION_LINEAR,
BlendedInfixSuggester.DEFAULT_NUM_FACTOR, false);
suggester.build(new InputArrayIterator(keys));
getInResults(suggester, "of ", payload, 1);
getInResults(suggester, "the ", payload, 1);
getInResults(suggester, "lake ", payload, 1);
suggester.close();
}
public void /*testT*/rying() throws IOException {
BytesRef lake = new BytesRef("lake");