Fixes for SOLR-2971, valType now optional for

ExternalFileFields and, if present, types float, pfloat, tfloat 
all accepable.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1215503 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erick Erickson 2011-12-17 14:53:36 +00:00
parent 9b938f0dcd
commit 1ff2096aef
4 changed files with 68 additions and 9 deletions

View File

@ -34,7 +34,9 @@ import java.io.IOException;
* <li>It's OK to have some documents without a keyField in the file (defVal is used as the default)</li>
* <li>It's OK for a keyField value to point to multiple documents (no uniqueness requirement)</li>
* </ul>
* <code>valType</code> is a reference to another fieldType to define the value type of this field (must currently be FloatField (float))
* <code>valType</code> is a reference to another fieldType to define the value type of this field
* (must currently be TrieFloatField or FloatField (valType="pfloat|float|tfloat") if used).
* This parameter has never been implemented. As of Solr 3.6/4.0 it is optional and can be omitted.
*
* The format of the external file is simply newline separated keyFieldValue=floatValue.
* <br/>Example:
@ -60,18 +62,21 @@ public class ExternalFileField extends FieldType {
private float defVal;
@Override
protected void init(IndexSchema schema, Map<String,String> args) {
protected void init(IndexSchema schema, Map<String, String> args) {
restrictProps(SORT_MISSING_FIRST | SORT_MISSING_LAST);
String ftypeS = getArg("valType", args);
if (ftypeS!=null) {
// valType has never been used for anything except to throw an error, so make it optional since the
// code (see getValueSource) gives you a FileFloatSource.
String ftypeS = args.remove("valType");
if (ftypeS != null) {
ftype = schema.getFieldTypes().get(ftypeS);
if (ftype==null || !(ftype instanceof FloatField)) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Only float (FloatField) is currently supported as external field type. got " + ftypeS);
if (ftype != null && !(ftype instanceof FloatField) && !(ftype instanceof TrieFloatField)) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
"Only float and pfloat (Trie|Float)Field are currently supported as external field type. Got " + ftypeS);
}
}
}
keyFieldName = args.remove("keyField");
String defValS = args.remove("defVal");
defVal = defValS==null ? 0 : Float.parseFloat(defValS);
defVal = defValS == null ? 0 : Float.parseFloat(defValS);
this.schema = schema;
}

View File

@ -0,0 +1,27 @@
<!--
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-external-filefield" version="1.0">
<types>
<fieldType name="tint" class="solr.TrieIntField" omitNorms="true" positionIncrementGap="0"/>
<fieldType name="eff_none" keyField="id" defVal="0"
stored="false" indexed="true"
class="solr.ExternalFileField" valType="tint"/>
</types>
</schema>

View File

@ -267,6 +267,24 @@
valued. -->
<fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>
<!-- These should pass right through and insure that we can declare external field types -->
<fieldType name="pfloat" class="solr.FloatField" omitNorms="true"/>
<fieldType name="eff_float" keyField="id" defVal="0"
stored="false" indexed="true"
class="solr.ExternalFileField" valType="float"/>
<fieldType name="eff_tfloat" keyField="id" defVal="0"
stored="false" indexed="true"
class="solr.ExternalFileField" valType="tfloat"/>
<fieldType name="eff_pfloat" keyField="id" defVal="0"
stored="false" indexed="true"
class="solr.ExternalFileField" valType="pfloat"/>
<!-- Be sure that the valType can be optional Since valType has done nothing up until now, this is preferred -->
<fieldType name="eff_none" keyField="id" defVal="0"
stored="false" indexed="true"
class="solr.ExternalFileField"/>
</types>

View File

@ -35,8 +35,10 @@ public class BadIndexSchemaTest extends SolrTestCaseJ4 {
try {
initCore( "solrconfig.xml", schema );
} catch (SolrException e) {
// short circut out if we found what we expected
// short circuit out if we found what we expected
if (-1 != e.getMessage().indexOf(errString)) return;
// Test the cause too in case the expected error is wrapped
if (-1 != e.getCause().getMessage().indexOf(errString)) return;
// otherwise, rethrow it, possibly completley unrelated
throw new SolrException
@ -44,6 +46,7 @@ public class BadIndexSchemaTest extends SolrTestCaseJ4 {
"Unexpected error, expected error matching: " + errString, e);
} finally {
SolrConfig.severeErrors.clear();
deleteCore();
}
fail("Did not encounter any exception from: " + schema);
}
@ -75,4 +78,10 @@ public class BadIndexSchemaTest extends SolrTestCaseJ4 {
public void testSevereErrorsForUnexpectedAnalyzer() throws Exception {
doTest("bad-schema-nontext-analyzer.xml", "StrField (bad_type)");
}
@Test
public void testBadExternalFileField() throws Exception {
doTest("bad-schema-external-filefield.xml",
"Only float and pfloat");
}
}