Apache Lucene open-source search software
SOLR-309: Fix FieldType so setting an analyzer on a FieldType that doesn't expect it will generate an error
Chris M. Hostetterfea853a3d2e6
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 3586481b094..c2a2d07448e 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -153,6 +153,11 @@ Bug Fixes* SOLR-2275: fix DisMax 'mm' parsing to be tolerant of whitespace(Erick Erickson via hossman)+* SOLR-309: Fix FieldType so setting an analyzer on a FieldType that+ doesn't expect it will generate an error. Practically speaking this+ means that Solr will now correctly generate an error on+ initialization if the schema.xml contains an analyzer configuration+ for a fieldType that does not use TextField. (hossman)Other Changes----------------------diff --git a/solr/src/java/org/apache/solr/schema/FieldType.java b/solr/src/java/org/apache/solr/schema/FieldType.javaindex c3daa183ae5..da8b5e747d9 100644--- a/solr/src/java/org/apache/solr/schema/FieldType.java+++ b/solr/src/java/org/apache/solr/schema/FieldType.java@@ -33,6 +33,7 @@ import org.apache.lucene.util.UnicodeUtil;import org.apache.noggit.CharArr;import org.apache.solr.analysis.SolrAnalyzer;import org.apache.solr.common.SolrException;+import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.response.TextResponseWriter;import org.apache.solr.search.QParser;import org.apache.solr.search.Sorting;@@ -418,6 +419,7 @@ public abstract class FieldType extends FieldProperties {* of this type, subclasses can set analyzer themselves or override* getAnalyzer()* @see #getAnalyzer+ * @see #setAnalyzer*/protected Analyzer analyzer=new DefaultAnalyzer(256);@@ -426,6 +428,7 @@ public abstract class FieldType extends FieldProperties {* of this type, subclasses can set analyzer themselves or override* getAnalyzer()* @see #getQueryAnalyzer+ * @see #setQueryAnalyzer*/protected Analyzer queryAnalyzer=analyzer;@@ -451,22 +454,52 @@ public abstract class FieldType extends FieldProperties {return queryAnalyzer;}+ private final String analyzerError =+ "FieldType: " + this.getClass().getSimpleName() ++ " (" + typeName + ") does not support specifying an analyzer";+/*** Sets the Analyzer to be used when indexing fields of this type.+ *+ * <p>+ * The default implementation throws a SolrException.+ * Subclasses that override this method need to ensure the behavior+ * of the analyzer is consistent with the implementation of toInternal.+ * </p>+ *+ * @see #toInternal+ * @see #setQueryAnalyzer* @see #getAnalyzer*/public void setAnalyzer(Analyzer analyzer) {- this.analyzer = analyzer;- log.trace("FieldType: " + typeName + ".setAnalyzer(" + analyzer.getClass().getName() + ")" );+ SolrException e = new SolrException+ (ErrorCode.SERVER_ERROR,+ "FieldType: " + this.getClass().getSimpleName() ++ " (" + typeName + ") does not support specifying an analyzer");+ SolrException.logOnce(log,null,e);+ throw e;}/*** Sets the Analyzer to be used when querying fields of this type.+ *+ * <p>+ * The default implementation throws a SolrException.+ * Subclasses that override this method need to ensure the behavior+ * of the analyzer is consistent with the implementation of toInternal.+ * </p>+ *+ * @see #toInternal+ * @see #setAnalyzer* @see #getQueryAnalyzer*/public void setQueryAnalyzer(Analyzer analyzer) {- this.queryAnalyzer = analyzer;- log.trace("FieldType: " + typeName + ".setQueryAnalyzer(" + analyzer.getClass().getName() + ")" );+ SolrException e = new SolrException+ (ErrorCode.SERVER_ERROR,+ "FieldType: " + this.getClass().getSimpleName() ++ " (" + typeName + ") does not support specifying an analyzer");+ SolrException.logOnce(log,null,e);+ throw e;}/**diff --git a/solr/src/java/org/apache/solr/schema/TextField.java b/solr/src/java/org/apache/solr/schema/TextField.javaindex a30bbfe2a54..21409733dec 100644--- a/solr/src/java/org/apache/solr/schema/TextField.java+++ b/solr/src/java/org/apache/solr/schema/TextField.java@@ -84,6 +84,15 @@ public class TextField extends FieldType {return ByteUtils.UTF8toUTF16(term);}+ @Override+ public void setAnalyzer(Analyzer analyzer) {+ this.analyzer = analyzer;+ }++ @Override+ public void setQueryAnalyzer(Analyzer analyzer) {+ this.queryAnalyzer = analyzer;+ }static Query parseFieldQuery(QParser parser, Analyzer analyzer, String field, String queryText) {int phraseSlop = 0;diff --git a/solr/src/test-files/solr/conf/bad-schema-nontext-analyzer.xml b/solr/src/test-files/solr/conf/bad-schema-nontext-analyzer.xmlnew file mode 100644index 00000000000..06a689a8298--- /dev/null+++ b/solr/src/test-files/solr/conf/bad-schema-nontext-analyzer.xml@@ -0,0 +1,39 @@+<?xml version="1.0" ?>+<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF licenses this file to You under the Apache License, Version 2.0+ (the "License"); you may not use this file except in compliance with+ the License. You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+-->++<schema name="bad-schema-nontext-analyzer" version="1.0">+ <types>+ <!-- BEGIN BAD STUFF -->+ <fieldType name="bad_type" class="solr.StrField">+ <analyzer>+ <tokenizer class="solr.WhitespaceTokenizerFactory"/>+ </analyzer>+ </fieldType>+ <!-- END BAD STUFF -->+ </types>+++ <fields>+ <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="false"/>++ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>diff --git a/solr/src/test/org/apache/solr/schema/BadIndexSchemaTest.java b/solr/src/test/org/apache/solr/schema/BadIndexSchemaTest.javaindex d8ae588cd77..0525f2ba6ff 100644--- a/solr/src/test/org/apache/solr/schema/BadIndexSchemaTest.java+++ b/solr/src/test/org/apache/solr/schema/BadIndexSchemaTest.java@@ -22,6 +22,8 @@ import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.core.SolrConfig;+import java.util.regex.Pattern;+import org.junit.Test;public class BadIndexSchemaTest extends SolrTestCaseJ4 {@@ -29,7 +31,7 @@ public class BadIndexSchemaTest extends SolrTestCaseJ4 {private void doTest(final String schema, final String errString)throws Exception {- ignoreException(errString);+ ignoreException(Pattern.quote(errString));try {initCore( "solrconfig.xml", schema );} catch (SolrException e) {@@ -60,4 +62,9 @@ public class BadIndexSchemaTest extends SolrTestCaseJ4 {public void testSevereErrorsForDuplicateFieldType() throws Exception {doTest("bad-schema-dup-fieldType.xml", "ftAgain");}++ @Test+ public void testSevereErrorsForUnexpectedAnalyzer() throws Exception {+ doTest("bad-schema-nontext-analyzer.xml", "StrField (bad_type)");+ }}