Apache Solr open-source search software
SOLR-5090: SpellCheckComponent sometimes throws NPE when "spellcheck.alternativeTermCount" is set to zero
James Dyer14d72257a656
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex ab411d83130..fa519e94857 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -134,6 +134,9 @@ Bug Fixes* SOLR-6023: FieldAnalysisRequestHandler throws NPE if no parameters are supplied.(shalin)++* SOLR-5090: SpellCheckComponent sometimes throws NPE if+ "spellcheck.alternativeTermCount" is set to zero (James Dyer).Other Changes---------------------diff --git a/solr/core/src/java/org/apache/solr/handler/component/SpellCheckComponent.java b/solr/core/src/java/org/apache/solr/handler/component/SpellCheckComponent.javaindex 3544630c134..77de7ecf5cc 100644--- a/solr/core/src/java/org/apache/solr/handler/component/SpellCheckComponent.java+++ b/solr/core/src/java/org/apache/solr/handler/component/SpellCheckComponent.java@@ -158,7 +158,7 @@ public class SpellCheckComponent extends SearchComponent implements SolrCoreAwarboolean extendedResults = params.getBool(SPELLCHECK_EXTENDED_RESULTS, false);boolean collate = params.getBool(SPELLCHECK_COLLATE, false);float accuracy = params.getFloat(SPELLCHECK_ACCURACY, Float.MIN_VALUE);- Integer alternativeTermCount = params.getInt(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT);+ int alternativeTermCount = params.getInt(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT, 0);Integer maxResultsForSuggest = params.getInt(SpellingParams.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST);ModifiableSolrParams customParams = new ModifiableSolrParams();for (String checkerName : getDictionaryNames(params)) {@@ -177,7 +177,7 @@ public class SpellCheckComponent extends SearchComponent implements SolrCoreAwarSuggestMode suggestMode = SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX;if (onlyMorePopular) {suggestMode = SuggestMode.SUGGEST_MORE_POPULAR;- } else if (alternativeTermCount != null) {+ } else if (alternativeTermCount > 0) {suggestMode = SuggestMode.SUGGEST_ALWAYS;}diff --git a/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.javaindex 238673e7a34..41ba2ecbabd 100644--- a/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java+++ b/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java@@ -156,17 +156,17 @@ public abstract class AbstractLuceneSpellChecker extends SolrSpellChecker {docFreq = reader.docFreq(term);}String[] suggestions = spellChecker.suggestSimilar(tokenText,- ((options.alternativeTermCount == null || docFreq == 0) ? count+ ((options.alternativeTermCount == 0 || docFreq == 0) ? count: options.alternativeTermCount), field != null ? reader : null, // workaround LUCENE-1295field, options.suggestMode, theAccuracy);if (suggestions.length == 1 && suggestions[0].equals(tokenText)- && options.alternativeTermCount == null) {+ && options.alternativeTermCount == 0) {// These are spelled the same, continue oncontinue;}// If considering alternatives to "correctly-spelled" terms, then add the// original as a viable suggestion.- if (options.alternativeTermCount != null && docFreq > 0) {+ if (options.alternativeTermCount > 0 && docFreq > 0) {boolean foundOriginal = false;String[] suggestionsWithOrig = new String[suggestions.length + 1];for (int i = 0; i < suggestions.length; i++) {diff --git a/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.javaindex 1884da74eff..14e26f1cbfe 100644--- a/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java+++ b/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java@@ -185,13 +185,13 @@ public class DirectSolrSpellChecker extends SolrSpellChecker {String tokenText = token.toString();Term term = new Term(field, tokenText);int freq = options.reader.docFreq(term);- int count = (options.alternativeTermCount != null && freq > 0) ? options.alternativeTermCount: options.count;+ int count = (options.alternativeTermCount > 0 && freq > 0) ? options.alternativeTermCount: options.count;SuggestWord[] suggestions = checker.suggestSimilar(term, count,options.reader, options.suggestMode, accuracy);result.addFrequency(token, freq);// If considering alternatives to "correctly-spelled" terms, then add the// original as a viable suggestion.- if (options.alternativeTermCount != null && freq > 0) {+ if (options.alternativeTermCount > 0 && freq > 0) {boolean foundOriginal = false;SuggestWord[] suggestionsWithOrig = new SuggestWord[suggestions.length + 1];for (int i = 0; i < suggestions.length; i++) {diff --git a/solr/core/src/java/org/apache/solr/spelling/SpellingOptions.java b/solr/core/src/java/org/apache/solr/spelling/SpellingOptions.javaindex cf12f4c985e..d0d211e42fb 100644--- a/solr/core/src/java/org/apache/solr/spelling/SpellingOptions.java+++ b/solr/core/src/java/org/apache/solr/spelling/SpellingOptions.java@@ -43,7 +43,7 @@ public class SpellingOptions {*/public int count = 1;- public Integer alternativeTermCount = null;+ public int alternativeTermCount = 0;public SuggestMode suggestMode = SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX;/**@@ -95,7 +95,7 @@ public class SpellingOptions {}public SpellingOptions(Collection<Token> tokens, IndexReader reader,- int count, Integer alternativeTermCount, SuggestMode suggestMode,+ int count, int alternativeTermCount, SuggestMode suggestMode,boolean extendedResults, float accuracy, SolrParams customParams) {this.tokens = tokens;this.reader = reader;diff --git a/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java b/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.javaindex f1b3651909d..8e0063bd095 100644--- a/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java+++ b/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java@@ -463,6 +463,14 @@ public class SpellCheckCollatorTest extends SolrTestCaseJ4 {"//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='collation']/int[@name='hits']=1","//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='collation']/lst[@name='misspellingsAndCorrections']/str[@name='june']='jane'");+ //SOLR-5090, alternativeTermCount==0 was being evaluated, sometimes would throw NPE+ assertQ(req("q", "teststop:(june customs)", "mm", "2", "qt",+ "spellCheckCompRH", "indent", "true",+ SpellCheckComponent.COMPONENT_NAME, "true",+ SpellCheckComponent.SPELLCHECK_DICT, dictionary[i],+ SpellCheckComponent.SPELLCHECK_COUNT, "10",+ SpellCheckComponent.SPELLCHECK_ALTERNATIVE_TERM_COUNT, "0",+ SpellCheckComponent.SPELLCHECK_COLLATE, "true"));}}