Apache Solr open-source search software
SOLR-16168: Spellcheck NPE if invalid dictionary name is provided (#821)
Kevin Risden7f2d4f7368a6
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 271956509e1..859b669fb98 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -65,6 +65,8 @@ Bug Fixes* SOLR-13044: Fix NPE during core close racing with index replication check. (David Smiley, hossman)+* SOLR-16168: Spellcheck NPE if invalid dictionary name is provided (Kevin Risden)+Other Changes---------------------* SOLR-15897: Remove <jmx/> from all unit test solrconfig.xml files. (Eric Pugh)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 0406243cbef..4457d773f87 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@@ -131,23 +131,23 @@ public class SpellCheckComponent extends SearchComponent implements SolrCoreAwarreturn;}boolean shardRequest = "true".equals(params.get(ShardParams.IS_SHARD));- String q = params.get(SPELLCHECK_Q);- SolrSpellChecker spellChecker = getSpellChecker(params);- Collection<Token> tokens = null;- if (q != null) {- // we have a spell check param, tokenize it with the query analyzer applicable for this- // spellchecker- tokens = getTokens(q, spellChecker.getQueryAnalyzer());- } else {- q = rb.getQueryString();- if (q == null) {- q = params.get(CommonParams.Q);+ SolrSpellChecker spellChecker = getSpellChecker(params);+ if (spellChecker != null) {+ Collection<Token> tokens;+ String q = params.get(SPELLCHECK_Q);+ if (q != null) {+ // we have a spell check param, tokenize it with the query analyzer applicable for this+ // spellchecker+ tokens = getTokens(q, spellChecker.getQueryAnalyzer());+ } else {+ q = rb.getQueryString();+ if (q == null) {+ q = params.get(CommonParams.Q);+ }+ tokens = queryConverter.convert(q);}- tokens = queryConverter.convert(q);- }- if (tokens != null && tokens.isEmpty() == false) {- if (spellChecker != null) {+ if (tokens != null && tokens.isEmpty() == false) {int count = params.getInt(SPELLCHECK_COUNT, 1);boolean onlyMorePopular =params.getBool(SPELLCHECK_ONLY_MORE_POPULAR, DEFAULT_ONLY_MORE_POPULAR);@@ -215,13 +215,12 @@ public class SpellCheckComponent extends SearchComponent implements SolrCoreAwar}rb.rsp.add("spellcheck", response);-- } else {- throw new SolrException(- SolrException.ErrorCode.NOT_FOUND,- "Specified dictionaries do not exist: "- + getDictionaryNameAsSingleString(getDictionaryNames(params)));}+ } else {+ throw new SolrException(+ SolrException.ErrorCode.NOT_FOUND,+ "Specified dictionaries do not exist: "+ + getDictionaryNameAsSingleString(getDictionaryNames(params)));}}diff --git a/solr/core/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.javaindex 24de8bbb109..543fe94de7a 100644--- a/solr/core/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java+++ b/solr/core/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java@@ -21,6 +21,7 @@ import java.util.*;import org.apache.lucene.util.LuceneTestCase.Slow;import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.common.SolrException;import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.params.SpellingParams;@@ -289,6 +290,43 @@ public class SpellCheckComponentTest extends SolrTestCaseJ4 {"/spellcheck/suggestions/foo=={'numFound':1, 'startOffset':2, 'endOffset':3, 'suggestion':['bar']}");}+ @Test+ public void testInvalidDictionary() throws Exception {+ assertQEx(+ "Invalid specified dictionary should throw exception",+ "Specified dictionaries do not exist: INVALID",+ req(+ "json.nl",+ "map",+ "qt",+ rh,+ SpellCheckComponent.COMPONENT_NAME,+ "true",+ "q",+ "documemt",+ SpellingParams.SPELLCHECK_DICT,+ "INVALID"),+ SolrException.ErrorCode.NOT_FOUND);++ assertQEx(+ "Invalid specified dictionary should throw exception",+ "Specified dictionaries do not exist: INVALID2",+ req(+ "json.nl",+ "map",+ "qt",+ rh,+ SpellCheckComponent.COMPONENT_NAME,+ "true",+ "q",+ "test",+ SpellingParams.SPELLCHECK_Q,+ "documemt",+ SpellingParams.SPELLCHECK_DICT,+ "INVALID2"),+ SolrException.ErrorCode.NOT_FOUND);+ }+@Testpublic void testCollate() throws Exception {assertJQ(