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 in the XMLResponseWriter was removed. XMLResponseWriter only
no longer work with values less then 2.2 (ryan) 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 Documentation
---------------------- ----------------------

View File

@ -54,6 +54,7 @@ public class BinaryField extends FieldType {
return ByteBuffer.wrap(f.getBinaryValue(), f.getBinaryOffset(), f.getBinaryLength() ) ; return ByteBuffer.wrap(f.getBinaryValue(), f.getBinaryOffset(), f.getBinaryLength() ) ;
} }
@Override
public Fieldable createField(SchemaField field, Object val, float boost) { public Fieldable createField(SchemaField field, Object val, float boost) {
if (val == null) return null; if (val == null) return null;
if (!field.stored()) { 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 (!field.indexed() && !field.stored()) {
if (log.isTraceEnabled()) if (log.isTraceEnabled())
log.trace("Ignoring unindexed/unstored field: " + field); log.trace("Ignoring unindexed/unstored field: " + field);
return null; return null;
} }
String val; String val;
try { try {
val = toInternal(externalVal); val = toInternal(value.toString());
} catch (RuntimeException e) { } 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; if (val==null) return null;
@ -276,11 +277,11 @@ public abstract class FieldType extends FieldProperties {
* @param boost The boost to apply * @param boost The boost to apply
* @return An array of {@link org.apache.lucene.document.Fieldable} * @return An array of {@link org.apache.lucene.document.Fieldable}
* *
* @see #createField(SchemaField, String, float) * @see #createField(SchemaField, Object, float)
* @see #isPolyField() * @see #isPolyField()
*/ */
public Fieldable[] createFields(SchemaField field, String externalVal, float boost) { public Fieldable[] createFields(SchemaField field, Object value, float boost) {
Fieldable f = createField( field, externalVal, boost); Fieldable f = createField( field, value, boost);
return f==null ? new Fieldable[]{} : new Fieldable[]{f}; return f==null ? new Fieldable[]{} : new Fieldable[]{f};
} }

View File

@ -54,7 +54,8 @@ public class LatLonType extends AbstractSubTypeFieldType implements SpatialQuery
} }
@Override @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) //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)]; Fieldable[] f = new Fieldable[(field.indexed() ? 2 : 0) + (field.stored() ? 1 : 0)];
if (field.indexed()) { 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 //It never makes sense to create a single field, so make it impossible to happen
@Override @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()); throw new UnsupportedOperationException("LatLonType uses multiple fields. field=" + field.getName());
} }

View File

@ -68,7 +68,8 @@ public class PointType extends CoordinateFieldType implements SpatialQueryable {
} }
@Override @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]; String[] point = new String[0];
try { try {
point = DistanceUtils.parsePoint(null, externalVal, dimension); point = DistanceUtils.parsePoint(null, externalVal, dimension);
@ -112,7 +113,7 @@ public class PointType extends CoordinateFieldType implements SpatialQueryable {
* *
*/ */
@Override @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()); 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; } 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); 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); return type.createFields(this,val,boost);
} }

View File

@ -168,7 +168,7 @@ public class TrieDateField extends DateField {
} }
@Override @Override
public Fieldable createField(SchemaField field, String externalVal, float boost) { public Fieldable createField(SchemaField field, Object value, float boost) {
boolean indexed = field.indexed(); boolean indexed = field.indexed();
boolean stored = field.stored(); boolean stored = field.stored();
@ -183,7 +183,10 @@ public class TrieDateField extends DateField {
byte[] arr=null; byte[] arr=null;
TokenStream ts=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 (stored) arr = TrieField.toArr(time);
if (indexed) ts = new NumericTokenStream(ps).setLongValue(time); if (indexed) ts = new NumericTokenStream(ps).setLongValue(time);

View File

@ -482,7 +482,7 @@ public class TrieField extends FieldType {
} }
@Override @Override
public Fieldable createField(SchemaField field, String externalVal, float boost) { public Fieldable createField(SchemaField field, Object value, float boost) {
boolean indexed = field.indexed(); boolean indexed = field.indexed();
boolean stored = field.stored(); boolean stored = field.stored();
@ -500,27 +500,37 @@ public class TrieField extends FieldType {
switch (type) { switch (type) {
case INTEGER: 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 (stored) arr = toArr(i);
if (indexed) ts = new NumericTokenStream(ps).setIntValue(i); if (indexed) ts = new NumericTokenStream(ps).setIntValue(i);
break; break;
case FLOAT: 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 (stored) arr = toArr(f);
if (indexed) ts = new NumericTokenStream(ps).setFloatValue(f); if (indexed) ts = new NumericTokenStream(ps).setFloatValue(f);
break; break;
case LONG: 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 (stored) arr = toArr(l);
if (indexed) ts = new NumericTokenStream(ps).setLongValue(l); if (indexed) ts = new NumericTokenStream(ps).setLongValue(l);
break; break;
case DOUBLE: 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 (stored) arr = toArr(d);
if (indexed) ts = new NumericTokenStream(ps).setDoubleValue(d); if (indexed) ts = new NumericTokenStream(ps).setDoubleValue(d);
break; break;
case DATE: 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 (stored) arr = toArr(time);
if (indexed) ts = new NumericTokenStream(ps).setLongValue(time); if (indexed) ts = new NumericTokenStream(ps).setLongValue(time);
break; 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()) { if (field.isPolyField()) {
Fieldable[] farr = field.getType().createFields(field, val, boost); Fieldable[] farr = field.getType().createFields(field, val, boost);
for (Fieldable f : farr) { for (Fieldable f : farr) {
@ -257,30 +257,10 @@ public class DocumentBuilder {
if( v == null ) { if( v == null ) {
continue; continue;
} }
String val = null;
hasField = true; 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);
}
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) { if (sfield != null) {
used = true; used = true;
addField(out, sfield, val, boost); addField(out, sfield, v, boost);
}
} }
// Check if we should copy this field to any other fields. // 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) { if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"ERROR: "+getID(doc, schema)+"multiple values encountered for non multiValued copy field " + "ERROR: "+getID(doc, schema)+"multiple values encountered for non multiValued copy field " +
destinationField.getName() + ": " + val); destinationField.getName() + ": " + v);
} }
used = true; used = true;
//Don't worry about poly fields here
Fieldable [] fields = null; // Perhaps trim the length of a copy field
if (isBinaryField) { Object val = v;
if (destinationField.getType() instanceof BinaryField) { if( val instanceof String && cf.getMaxChars() > 0 ) {
BinaryField binaryField = (BinaryField) destinationField.getType(); val = cf.getLimitedValue((String)val);
//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);
} }
Fieldable [] fields = destinationField.createFields(val, boost);
if (fields != null) { // null fields are not added if (fields != null) { // null fields are not added
for (Fieldable f : fields) { for (Fieldable f : fields) {
out.add(f); out.add(f);