SOLR-3087: CurrencyField now generates an appropriate error on schema init if it is configured as multiValued

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1383648 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2012-09-11 23:02:25 +00:00
parent ee1e747c29
commit 9561d856c5
9 changed files with 158 additions and 4 deletions

View File

@ -145,6 +145,11 @@ Bug Fixes
from 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
----------------------

View File

@ -81,6 +81,11 @@ public class CurrencyField extends FieldType implements SchemaAware, ResourceLoa
@Override
protected 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, ResourceLoa
return 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());
}
}
@Override
public 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;
}
}
}

View File

@ -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:
}
}

View File

@ -69,6 +69,8 @@ public final class SchemaField extends FieldProperties {
// initalize with the required property flag
required = (properties & REQUIRED) !=0;
type.checkSchemaField(this);
}
public String getName() { return name; }

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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" />

View File

@ -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");
}