diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index f0168d209e5..4358217495a 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -342,6 +342,8 @@ Bug Fixes could commit too frequently and could block adds until a new seaercher was registered. (yonik) +* SOLR-2726: Fixed NullPointerException when using spellcheck.q with Suggester. + (Bernd Fehling, valentin via rmuir) Other Changes ---------------------- diff --git a/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java index d835acca2e0..efd42b6ab25 100644 --- a/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java +++ b/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java @@ -68,14 +68,11 @@ public abstract class AbstractLuceneSpellChecker extends SolrSpellChecker { public static final String INDEX_DIR = "spellcheckIndexDir"; public static final String ACCURACY = "accuracy"; public static final String STRING_DISTANCE = "distanceMeasure"; - public static final String FIELD_TYPE = "fieldType"; public static final String COMPARATOR_CLASS = "comparatorClass"; public static final String SCORE_COMP = "score"; public static final String FREQ_COMP = "freq"; - protected String field; - protected String fieldTypeName; protected org.apache.lucene.search.spell.SpellChecker spellChecker; protected String sourceLocation; @@ -117,7 +114,6 @@ public abstract class AbstractLuceneSpellChecker extends SolrSpellChecker { } else { comp = SuggestWordQueue.DEFAULT_COMPARATOR; } - field = (String) config.get(FIELD); String strDistanceName = (String)config.get(STRING_DISTANCE); if (strDistanceName != null) { sd = (StringDistance) core.getResourceLoader().newInstance(strDistanceName); @@ -140,18 +136,6 @@ public abstract class AbstractLuceneSpellChecker extends SolrSpellChecker { "Unparseable accuracy given for dictionary: " + name, e); } } - if (field != null && core.getSchema().getFieldTypeNoEx(field) != null) { - analyzer = core.getSchema().getFieldType(field).getQueryAnalyzer(); - } - fieldTypeName = (String) config.get(FIELD_TYPE); - if (core.getSchema().getFieldTypes().containsKey(fieldTypeName)) { - FieldType fieldType = core.getSchema().getFieldTypes().get(fieldTypeName); - analyzer = fieldType.getQueryAnalyzer(); - } - if (analyzer == null) { - log.info("Using WhitespaceAnalzyer for dictionary: " + name); - analyzer = new WhitespaceAnalyzer(core.getSolrConfig().luceneMatchVersion); - } return name; } diff --git a/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java index f4bd5c5babb..b047522647d 100644 --- a/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java +++ b/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java @@ -67,8 +67,6 @@ public class DirectSolrSpellChecker extends SolrSpellChecker { public static final String COMPARATOR_CLASS = AbstractLuceneSpellChecker.COMPARATOR_CLASS; public static final String SCORE_COMP = AbstractLuceneSpellChecker.SCORE_COMP; public static final String FREQ_COMP = AbstractLuceneSpellChecker.FREQ_COMP; - public static final String FIELD = AbstractLuceneSpellChecker.FIELD; - public static final String FIELD_TYPE = AbstractLuceneSpellChecker.FIELD_TYPE; public static final String STRING_DISTANCE = AbstractLuceneSpellChecker.STRING_DISTANCE; public static final String ACCURACY = AbstractLuceneSpellChecker.ACCURACY; public static final String THRESHOLD_TOKEN_FREQUENCY = IndexBasedSpellChecker.THRESHOLD_TOKEN_FREQUENCY; @@ -94,8 +92,6 @@ public class DirectSolrSpellChecker extends SolrSpellChecker { public static final float DEFAULT_MAXQUERYFREQUENCY = 0.01f; private DirectSpellChecker checker = new DirectSpellChecker(); - private String field; - private String fieldTypeName; @Override public String init(NamedList config, SolrCore core) { @@ -118,21 +114,6 @@ public class DirectSolrSpellChecker extends SolrSpellChecker { if (distClass != null && !distClass.equalsIgnoreCase(INTERNAL_DISTANCE)) sd = (StringDistance) core.getResourceLoader().newInstance(distClass); - field = (String) config.get(FIELD); - // setup analyzer for field - if (field != null && core.getSchema().getFieldTypeNoEx(field) != null) { - analyzer = core.getSchema().getFieldType(field).getQueryAnalyzer(); - } - fieldTypeName = (String) config.get(FIELD_TYPE); - if (core.getSchema().getFieldTypes().containsKey(fieldTypeName)) { - FieldType fieldType = core.getSchema().getFieldTypes().get(fieldTypeName); - analyzer = fieldType.getQueryAnalyzer(); - } - if (analyzer == null) { - LOG.info("Using WhitespaceAnalyzer for dictionary: " + name); - analyzer = new WhitespaceAnalyzer(core.getSolrConfig().luceneMatchVersion); - } - float minAccuracy = DEFAULT_ACCURACY; Float accuracy = (Float) config.get(ACCURACY); if (accuracy != null) diff --git a/solr/core/src/java/org/apache/solr/spelling/SolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/SolrSpellChecker.java index 7df42b294dc..a115303d0e5 100644 --- a/solr/core/src/java/org/apache/solr/spelling/SolrSpellChecker.java +++ b/solr/core/src/java/org/apache/solr/spelling/SolrSpellChecker.java @@ -17,8 +17,10 @@ package org.apache.solr.spelling; */ import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrCore; +import org.apache.solr.schema.FieldType; import org.apache.solr.search.SolrIndexSearcher; import java.io.IOException; @@ -35,15 +37,31 @@ import java.io.IOException; public abstract class SolrSpellChecker { public static final String DICTIONARY_NAME = "name"; public static final String DEFAULT_DICTIONARY_NAME = "default"; + public static final String FIELD = "field"; + public static final String FIELD_TYPE = "fieldType"; /** Dictionary name */ protected String name; protected Analyzer analyzer; + protected String field; + protected String fieldTypeName; public String init(NamedList config, SolrCore core) { name = (String) config.get(DICTIONARY_NAME); if (name == null) { name = DEFAULT_DICTIONARY_NAME; } + field = (String)config.get(FIELD); + if (field != null && core.getSchema().getFieldTypeNoEx(field) != null) { + analyzer = core.getSchema().getFieldType(field).getQueryAnalyzer(); + } + fieldTypeName = (String) config.get(FIELD_TYPE); + if (core.getSchema().getFieldTypes().containsKey(fieldTypeName)) { + FieldType fieldType = core.getSchema().getFieldTypes().get(fieldTypeName); + analyzer = fieldType.getQueryAnalyzer(); + } + if (analyzer == null) { + analyzer = new WhitespaceAnalyzer(core.getSolrConfig().luceneMatchVersion); + } return name; } diff --git a/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java b/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java index 1b208f06300..c19b8d3cae9 100644 --- a/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java +++ b/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java @@ -51,8 +51,6 @@ public class Suggester extends SolrSpellChecker { * current IndexReader. */ public static final String LOCATION = "sourceLocation"; - /** Field to use as the source of terms if using IndexReader. */ - public static final String FIELD = "field"; /** Fully-qualified class of the {@link Lookup} implementation. */ public static final String LOOKUP_IMPL = "lookupImpl"; /** @@ -68,7 +66,6 @@ public class Suggester extends SolrSpellChecker { protected String sourceLocation; protected File storeDir; - protected String field; protected float threshold; protected Dictionary dictionary; protected IndexReader reader; @@ -83,7 +80,6 @@ public class Suggester extends SolrSpellChecker { threshold = config.get(THRESHOLD_TOKEN_FREQUENCY) == null ? 0.0f : (Float)config.get(THRESHOLD_TOKEN_FREQUENCY); sourceLocation = (String) config.get(LOCATION); - field = (String)config.get(FIELD); lookupImpl = (String)config.get(LOOKUP_IMPL); // support the old classnames without -Factory for config file backwards compatibility. diff --git a/solr/core/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java b/solr/core/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java index 804842f27fb..600c598a745 100644 --- a/solr/core/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java +++ b/solr/core/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java @@ -21,6 +21,7 @@ import java.io.File; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.params.SpellingParams; +import org.apache.solr.common.util.NamedList; import org.junit.BeforeClass; import org.junit.Test; @@ -95,4 +96,13 @@ public class SuggesterTest extends SolrTestCaseJ4 { assertQ(req("qt", requestUri, "q", "ac", SpellingParams.SPELLCHECK_COUNT, "2", SpellingParams.SPELLCHECK_ONLY_MORE_POPULAR, "true"), "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/int[@name='numFound'][.='2']"); } + + // SOLR-2726 + public void testAnalyzer() throws Exception { + Suggester suggester = new Suggester(); + NamedList params = new NamedList(); + params.add("lookupImpl", "org.apache.solr.spelling.suggest.tst.TSTLookupFactory"); + suggester.init(params, h.getCore()); + assertTrue(suggester.getQueryAnalyzer() != null); + } }