From b3d70e8f6995f8d1666cd2ba534fd229547e37fb Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Thu, 20 Jun 2013 21:55:08 +0000 Subject: [PATCH] LUCENE-4971: fix NPE in AnalyzingSuggester when there are too many graph expansions git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1495206 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 3 ++ .../util/automaton/SpecialOperations.java | 6 ++-- .../analyzing/AnalyzingSuggesterTest.java | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 9c22802ec37..1c9c390deee 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -173,6 +173,9 @@ Bug Fixes when the sum of those shapes contain the query shape but none do individually. A flag was added to use the original faster algorithm. (David Smiley) +* LUCENE-4971: Fixed NPE in AnalyzingSuggester when there are too many + graph expansions. (Alexey Kudinov via Mike McCandless) + Optimizations * LUCENE-4936: Improve numeric doc values compression in case all values share diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/SpecialOperations.java b/lucene/core/src/java/org/apache/lucene/util/automaton/SpecialOperations.java index 0e6adf63556..d479cd78923 100644 --- a/lucene/core/src/java/org/apache/lucene/util/automaton/SpecialOperations.java +++ b/lucene/core/src/java/org/apache/lucene/util/automaton/SpecialOperations.java @@ -219,7 +219,7 @@ final public class SpecialOperations { /** * Returns the set of accepted strings, assuming that at most * limit strings are accepted. If more than limit - * strings are accepted, null is returned. If limit<0, then + * strings are accepted, the first limit strings found are returned. If limit<0, then * the limit is infinite. */ public static Set getFiniteStrings(Automaton a, int limit) { @@ -227,11 +227,9 @@ final public class SpecialOperations { if (a.isSingleton()) { if (limit > 0) { strings.add(Util.toUTF32(a.singleton, new IntsRef())); - } else { - return null; } } else if (!getFiniteStrings(a.initial, new HashSet(), strings, new IntsRef(), limit)) { - return null; + return strings; } return strings; } diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java index 155446985a5..803c5d91a30 100644 --- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java +++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java @@ -1164,4 +1164,33 @@ public class AnalyzingSuggesterTest extends LuceneTestCase { assertEquals("[isla de muerta/8, i love lucy/7]", suggester.lookup("i", false, 3).toString()); assertEquals("[i love lucy/7]", suggester.lookup("i ", false, 3).toString()); } + + public void testTooManyExpansions() throws Exception { + + final Analyzer a = new Analyzer() { + @Override + protected TokenStreamComponents createComponents(String fieldName, Reader reader) { + Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.SIMPLE, true); + + return new TokenStreamComponents(tokenizer) { + @Override + public TokenStream getTokenStream() { + Token a = new Token("a", 0, 1); + a.setPositionIncrement(1); + Token b = new Token("b", 0, 1); + b.setPositionIncrement(0); + return new CannedTokenStream(new Token[] {a, b}); + } + + @Override + protected void setReader(final Reader reader) throws IOException { + } + }; + } + }; + + AnalyzingSuggester suggester = new AnalyzingSuggester(a, a, 0, 256, 1); + suggester.build(new TermFreqArrayIterator(new TermFreq[] {new TermFreq("a", 1)})); + assertEquals("[a/1]", suggester.lookup("a", false, 1).toString()); + } }