SOLR-2423: FieldType argument changed from String to Object Conversion from SolrInputDocument > Object > Fieldable is now managed by FieldType rather then DocumentBuilder.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1082638 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2011-03-17 18:53:48 +00:00
parent 22ec0cc7df
commit e80cfecd28
9 changed files with 52 additions and 54 deletions

View File

@ -189,6 +189,10 @@ Other Changes
in the XMLResponseWriter was removed. XMLResponseWriter only
no longer work with values less then 2.2 (ryan)
* SOLR-2423: FieldType argument changed from String to Object
Conversion from SolrInputDocument > Object > Fieldable is now managed
by FieldType rather then DocumentBuilder. (ryan)
Documentation
----------------------

View File

@ -54,6 +54,7 @@ public class BinaryField extends FieldType {
return ByteBuffer.wrap(f.getBinaryValue(), f.getBinaryOffset(), f.getBinaryLength() ) ;
}
@Override
public Fieldable createField(SchemaField field, Object val, float boost) {
if (val == null) return null;
if (!field.stored()) {

View File

@ -223,17 +223,18 @@ public abstract class FieldType extends FieldProperties {
*
*
*/
public Fieldable createField(SchemaField field, String externalVal, float boost) {
public Fieldable createField(SchemaField field, Object value, float boost) {
if (!field.indexed() && !field.stored()) {
if (log.isTraceEnabled())
log.trace("Ignoring unindexed/unstored field: " + field);
return null;
}
String val;
try {
val = toInternal(externalVal);
val = toInternal(value.toString());
} catch (RuntimeException e) {
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Error while creating field '" + field + "' from value '" + externalVal + "'", e, false);
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Error while creating field '" + field + "' from value '" + value + "'", e, false);
}
if (val==null) return null;
@ -276,11 +277,11 @@ public abstract class FieldType extends FieldProperties {
* @param boost The boost to apply
* @return An array of {@link org.apache.lucene.document.Fieldable}
*
* @see #createField(SchemaField, String, float)
* @see #createField(SchemaField, Object, float)
* @see #isPolyField()
*/
public Fieldable[] createFields(SchemaField field, String externalVal, float boost) {
Fieldable f = createField( field, externalVal, boost);
public Fieldable[] createFields(SchemaField field, Object value, float boost) {
Fieldable f = createField( field, value, boost);
return f==null ? new Fieldable[]{} : new Fieldable[]{f};
}

View File

@ -54,7 +54,8 @@ public class LatLonType extends AbstractSubTypeFieldType implements SpatialQuery
}
@Override
public Fieldable[] createFields(SchemaField field, String externalVal, float boost) {
public Fieldable[] createFields(SchemaField field, Object value, float boost) {
String externalVal = value.toString();
//we could have tileDiff + 3 fields (two for the lat/lon, one for storage)
Fieldable[] f = new Fieldable[(field.indexed() ? 2 : 0) + (field.stored() ? 1 : 0)];
if (field.indexed()) {
@ -280,7 +281,7 @@ public class LatLonType extends AbstractSubTypeFieldType implements SpatialQuery
//It never makes sense to create a single field, so make it impossible to happen
@Override
public Fieldable createField(SchemaField field, String externalVal, float boost) {
public Fieldable createField(SchemaField field, Object value, float boost) {
throw new UnsupportedOperationException("LatLonType uses multiple fields. field=" + field.getName());
}

View File

@ -68,7 +68,8 @@ public class PointType extends CoordinateFieldType implements SpatialQueryable {
}
@Override
public Fieldable[] createFields(SchemaField field, String externalVal, float boost) {
public Fieldable[] createFields(SchemaField field, Object value, float boost) {
String externalVal = value.toString();
String[] point = new String[0];
try {
point = DistanceUtils.parsePoint(null, externalVal, dimension);
@ -112,7 +113,7 @@ public class PointType extends CoordinateFieldType implements SpatialQueryable {
*
*/
@Override
public Fieldable createField(SchemaField field, String externalVal, float boost) {
public Fieldable createField(SchemaField field, Object value, float boost) {
throw new UnsupportedOperationException("PointType uses multiple fields. field=" + field.getName());
}

View File

@ -93,11 +93,11 @@ public final class SchemaField extends FieldProperties {
boolean isBinary() { return (properties & BINARY)!=0; }
public Fieldable createField(String val, float boost) {
public Fieldable createField(Object val, float boost) {
return type.createField(this,val,boost);
}
public Fieldable[] createFields(String val, float boost) {
public Fieldable[] createFields(Object val, float boost) {
return type.createFields(this,val,boost);
}

View File

@ -168,7 +168,7 @@ public class TrieDateField extends DateField {
}
@Override
public Fieldable createField(SchemaField field, String externalVal, float boost) {
public Fieldable createField(SchemaField field, Object value, float boost) {
boolean indexed = field.indexed();
boolean stored = field.stored();
@ -183,7 +183,10 @@ public class TrieDateField extends DateField {
byte[] arr=null;
TokenStream ts=null;
long time = super.parseMath(null, externalVal).getTime();
long time = (value instanceof Date)
? ((Date)value).getTime()
: super.parseMath(null, value.toString()).getTime();
if (stored) arr = TrieField.toArr(time);
if (indexed) ts = new NumericTokenStream(ps).setLongValue(time);

View File

@ -482,7 +482,7 @@ public class TrieField extends FieldType {
}
@Override
public Fieldable createField(SchemaField field, String externalVal, float boost) {
public Fieldable createField(SchemaField field, Object value, float boost) {
boolean indexed = field.indexed();
boolean stored = field.stored();
@ -500,27 +500,37 @@ public class TrieField extends FieldType {
switch (type) {
case INTEGER:
int i = Integer.parseInt(externalVal);
int i = (value instanceof Number)
? ((Number)value).intValue()
: Integer.parseInt(value.toString());
if (stored) arr = toArr(i);
if (indexed) ts = new NumericTokenStream(ps).setIntValue(i);
break;
case FLOAT:
float f = Float.parseFloat(externalVal);
float f = (value instanceof Number)
? ((Number)value).floatValue()
: Float.parseFloat(value.toString());
if (stored) arr = toArr(f);
if (indexed) ts = new NumericTokenStream(ps).setFloatValue(f);
break;
case LONG:
long l = Long.parseLong(externalVal);
long l = (value instanceof Number)
? ((Number)value).longValue()
: Long.parseLong(value.toString());
if (stored) arr = toArr(l);
if (indexed) ts = new NumericTokenStream(ps).setLongValue(l);
break;
case DOUBLE:
double d = Double.parseDouble(externalVal);
double d = (value instanceof Number)
? ((Number)value).doubleValue()
: Double.parseDouble(value.toString());
if (stored) arr = toArr(d);
if (indexed) ts = new NumericTokenStream(ps).setDoubleValue(d);
break;
case DATE:
long time = dateField.parseMath(null, externalVal).getTime();
long time = (value instanceof Date)
? ((Date)value).getTime()
: dateField.parseMath(null, value.toString()).getTime();
if (stored) arr = toArr(time);
if (indexed) ts = new NumericTokenStream(ps).setLongValue(time);
break;

View File

@ -194,7 +194,7 @@ public class DocumentBuilder {
}
private static void addField(Document doc, SchemaField field, String val, float boost) {
private static void addField(Document doc, SchemaField field, Object val, float boost) {
if (field.isPolyField()) {
Fieldable[] farr = field.getType().createFields(field, val, boost);
for (Fieldable f : farr) {
@ -257,30 +257,10 @@ public class DocumentBuilder {
if( v == null ) {
continue;
}
String val = null;
hasField = true;
boolean isBinaryField = false;
if (sfield != null && sfield.getType() instanceof BinaryField) {
isBinaryField = true;
BinaryField binaryField = (BinaryField) sfield.getType();
Fieldable f = binaryField.createField(sfield,v,boost);
if(f != null){
out.add(f);
}
if (sfield != null) {
used = true;
} else {
// TODO!!! HACK -- date conversion
if (sfield != null && v instanceof Date && sfield.getType() instanceof DateField) {
DateField df = (DateField) sfield.getType();
val = df.toInternal((Date) v) + 'Z';
} else if (v != null) {
val = v.toString();
}
if (sfield != null) {
used = true;
addField(out, sfield, val, boost);
}
addField(out, sfield, v, boost);
}
// Check if we should copy this field to any other fields.
@ -292,21 +272,18 @@ public class DocumentBuilder {
if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"ERROR: "+getID(doc, schema)+"multiple values encountered for non multiValued copy field " +
destinationField.getName() + ": " + val);
destinationField.getName() + ": " + v);
}
used = true;
//Don't worry about poly fields here
Fieldable [] fields = null;
if (isBinaryField) {
if (destinationField.getType() instanceof BinaryField) {
BinaryField binaryField = (BinaryField) destinationField.getType();
//TODO: safe to assume that binary fields only create one?
fields = new Fieldable[]{binaryField.createField(destinationField, v, boost)};
}
} else {
fields = destinationField.createFields(cf.getLimitedValue(val), boost);
// Perhaps trim the length of a copy field
Object val = v;
if( val instanceof String && cf.getMaxChars() > 0 ) {
val = cf.getLimitedValue((String)val);
}
Fieldable [] fields = destinationField.createFields(val, boost);
if (fields != null) { // null fields are not added
for (Fieldable f : fields) {
out.add(f);