Apache Lucene open-source search software
SOLR-3087: CurrencyField now generates an appropriate error on schema init if it is configured as multiValued
Chris M. Hostetter9561d856c5b5
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 62770b86a7d..aeedf6e47f6 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -145,6 +145,11 @@ Bug Fixesfrom Collections passed to addValue/addField(Tom Switzer via hossman)+* SOLR-3087: CurrencyField now generates an appropriate error on schema init+ if it is configured as multiValued - this has never been properly supported,+ but previously failed silently in odd ways. (hossman)++Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java b/solr/core/src/java/org/apache/solr/schema/CurrencyField.javaindex 0a1b94f2bcd..9cfe5fda53c 100644--- a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java+++ b/solr/core/src/java/org/apache/solr/schema/CurrencyField.java@@ -81,6 +81,11 @@ public class CurrencyField extends FieldType implements SchemaAware, ResourceLoa@Overrideprotected void init(IndexSchema schema, Map<String, String> args) {super.init(schema, args);+ if (this.isMultiValued()) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,+ "CurrencyField types can not be multiValued: " ++ this.typeName);+ }this.schema = schema;this.exchangeRateProviderClass = args.get(PARAM_RATE_PROVIDER_CLASS);this.defaultCurrency = args.get(PARAM_DEFAULT_CURRENCY);@@ -132,6 +137,16 @@ public class CurrencyField extends FieldType implements SchemaAware, ResourceLoareturn true;}+ @Override+ public void checkSchemaField(final SchemaField field) throws SolrException {+ super.checkSchemaField(field);+ if (field.multiValued()) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,+ "CurrencyFields can not be multiValued: " ++ field.getName());+ }+ }+@Overridepublic StorableField[] createFields(SchemaField field, Object externalVal, float boost) {CurrencyValue value = CurrencyValue.parse(externalVal.toString(), defaultCurrency);@@ -763,4 +778,4 @@ class CurrencyValue {public String toString() {return String.valueOf(amount) + "," + currencyCode;}-}\ No newline at end of file+}diff --git a/solr/core/src/java/org/apache/solr/schema/FieldType.java b/solr/core/src/java/org/apache/solr/schema/FieldType.javaindex d7c54caee52..58f1a29dc6d 100644--- a/solr/core/src/java/org/apache/solr/schema/FieldType.java+++ b/solr/core/src/java/org/apache/solr/schema/FieldType.java@@ -599,4 +599,20 @@ public abstract class FieldType extends FieldProperties {readableToIndexed(externalVal, br);return new TermQuery(new Term(field.getName(), br));}++ /**+ * Check's {@link org.apache.solr.schema.SchemaField} instances constructed+ * using this field type to ensure that they are valid.+ *+ * <p>+ * This method is called by the <code>SchemaField</code> constructor to+ * check that it's initialization does not violate any fundemental+ * requirements of the <code>FieldType</code>. The default implementation+ * does nothing, but subclasses may chose to throw a {@link SolrException}+ * if invariants are violated by the <code>SchemaField.+ * </p>+ */+ public void checkSchemaField(final SchemaField field) throws SolrException {+ // :NOOP:+ }}diff --git a/solr/core/src/java/org/apache/solr/schema/SchemaField.java b/solr/core/src/java/org/apache/solr/schema/SchemaField.javaindex 93a0eab0f5b..6626fa6a9bc 100644--- a/solr/core/src/java/org/apache/solr/schema/SchemaField.java+++ b/solr/core/src/java/org/apache/solr/schema/SchemaField.java@@ -69,6 +69,8 @@ public final class SchemaField extends FieldProperties {// initalize with the required property flagrequired = (properties & REQUIRED) !=0;++ type.checkSchemaField(this);}public String getName() { return name; }diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-dynamic-multivalued.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-dynamic-multivalued.xmlnew file mode 100644index 00000000000..a71b361c956--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-dynamic-multivalued.xml@@ -0,0 +1,36 @@+<?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-currency-ft-multivalued" version="1.4">+ <types>+ <fieldType name="string" class="solr.StrField" multiValued="true"/>+ <fieldType name="currency" class="solr.CurrencyField" currencyConfig="currency.xml" multiValued="false" />++ </types>++ <fields>+ <field name="id" type="string" indexed="true" stored="true" multiValued="false"/>+ <!-- BEGIN BAD STUFF: multiValued -->+ <dynamicField name="*_c" type="currency" indexed="true" stored="true" multiValued="true" />+ <!-- END BAD STUFF -->+ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-ft-multivalued.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-ft-multivalued.xmlnew file mode 100644index 00000000000..a1b788e628e--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-ft-multivalued.xml@@ -0,0 +1,34 @@+<?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-currency-ft-multivalued" version="1.4">+ <types>+ <fieldType name="string" class="solr.StrField" multiValued="true"/>+ <!-- BEGIN BAD STUFF: multiValued -->+ <fieldType name="currency" class="solr.CurrencyField" currencyConfig="currency.xml" multiValued="true" />+ </types>++ <fields>+ <field name="id" type="string" indexed="true" stored="true" multiValued="false"/>+ <field name="money" type="currency" indexed="true" stored="true" />+ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-multivalued.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-multivalued.xmlnew file mode 100644index 00000000000..84bfaea141d--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-currency-multivalued.xml@@ -0,0 +1,35 @@+<?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-currency-multivalued" version="1.4">+ <types>+ <fieldType name="string" class="solr.StrField" multiValued="true"/>+ <fieldType name="currency" class="solr.CurrencyField" currencyConfig="currency.xml"/>+ </types>++ <fields>+ <field name="id" type="string" indexed="true" stored="true" multiValued="false"/>+ <!-- BEGIN BAD STUFF: multiValued -->+ <field name="money" type="currency" indexed="true" stored="true" multiValued="true" />+ <!-- END BAD STUFF -->+ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>diff --git a/solr/core/src/test-files/solr/collection1/conf/schema.xml b/solr/core/src/test-files/solr/collection1/conf/schema.xmlindex e21b4396803..6b67f500c72 100644--- a/solr/core/src/test-files/solr/collection1/conf/schema.xml+++ b/solr/core/src/test-files/solr/collection1/conf/schema.xml@@ -399,9 +399,11 @@<fieldType name="latLon" class="solr.LatLonType" subFieldType="double"/><!-- Currency type -->- <fieldType name="currency" class="solr.CurrencyField" currencyConfig="currency.xml"/>- <fieldType name="mock_currency" class="solr.CurrencyField" providerClass="solr.MockExchangeRateProvider" foo="bar" />- <fieldType name="openexchangeratesorg_currency" class="solr.CurrencyField"+ <fieldType name="currency" class="solr.CurrencyField" currencyConfig="currency.xml" multiValued="false" />+ <fieldType name="mock_currency" class="solr.CurrencyField" providerClass="solr.MockExchangeRateProvider" foo="bar" multiValued="false" />+ <fieldType name="openexchangeratesorg_currency"+ class="solr.CurrencyField"+ multiValued="false"providerClass="solr.OpenExchangeRatesOrgProvider"ratesFileLocation="open-exchange-rates.json" />diff --git a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.javaindex 91803f3de55..6c9e6fbf18a 100644--- a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java+++ b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java@@ -66,6 +66,15 @@ public class BadIndexSchemaTest extends AbstractBadConfigTestBase {"can not be configured to be multivalued");}+ public void testMultivaluedCurrency() throws Exception {+ doTest("bad-schema-currency-ft-multivalued.xml",+ "types can not be multiValued: currency");+ doTest("bad-schema-currency-multivalued.xml",+ "Fields can not be multiValued: money");+ doTest("bad-schema-currency-dynamic-multivalued.xml",+ "Fields can not be multiValued: *_c");+ }+public void testPerFieldtypeSimButNoSchemaSimFactory() throws Exception {doTest("bad-schema-sim-global-vs-ft-mismatch.xml", "global similarity does not support it");}