From e80cfecd28cabed256a51540a397205ef72461f2 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Thu, 17 Mar 2011 18:53:48 +0000 Subject: [PATCH] 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 --- solr/CHANGES.txt | 4 ++ .../org/apache/solr/schema/BinaryField.java | 1 + .../org/apache/solr/schema/FieldType.java | 13 +++--- .../org/apache/solr/schema/LatLonType.java | 5 ++- .../org/apache/solr/schema/PointType.java | 5 ++- .../org/apache/solr/schema/SchemaField.java | 4 +- .../org/apache/solr/schema/TrieDateField.java | 7 ++- .../org/apache/solr/schema/TrieField.java | 22 ++++++--- .../apache/solr/update/DocumentBuilder.java | 45 +++++-------------- 9 files changed, 52 insertions(+), 54 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index f0ac685667a..df105735945 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -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 ---------------------- diff --git a/solr/src/java/org/apache/solr/schema/BinaryField.java b/solr/src/java/org/apache/solr/schema/BinaryField.java index c9c22461d4d..97567660bfc 100644 --- a/solr/src/java/org/apache/solr/schema/BinaryField.java +++ b/solr/src/java/org/apache/solr/schema/BinaryField.java @@ -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()) { diff --git a/solr/src/java/org/apache/solr/schema/FieldType.java b/solr/src/java/org/apache/solr/schema/FieldType.java index da8b5e747d9..d4dc8bc952e 100644 --- a/solr/src/java/org/apache/solr/schema/FieldType.java +++ b/solr/src/java/org/apache/solr/schema/FieldType.java @@ -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}; } diff --git a/solr/src/java/org/apache/solr/schema/LatLonType.java b/solr/src/java/org/apache/solr/schema/LatLonType.java index b1f95348a53..75dbd65fde4 100644 --- a/solr/src/java/org/apache/solr/schema/LatLonType.java +++ b/solr/src/java/org/apache/solr/schema/LatLonType.java @@ -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()); } diff --git a/solr/src/java/org/apache/solr/schema/PointType.java b/solr/src/java/org/apache/solr/schema/PointType.java index 4a93333292f..06c0a361ef9 100644 --- a/solr/src/java/org/apache/solr/schema/PointType.java +++ b/solr/src/java/org/apache/solr/schema/PointType.java @@ -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()); } diff --git a/solr/src/java/org/apache/solr/schema/SchemaField.java b/solr/src/java/org/apache/solr/schema/SchemaField.java index 9d625bd4968..5829776a486 100644 --- a/solr/src/java/org/apache/solr/schema/SchemaField.java +++ b/solr/src/java/org/apache/solr/schema/SchemaField.java @@ -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); } diff --git a/solr/src/java/org/apache/solr/schema/TrieDateField.java b/solr/src/java/org/apache/solr/schema/TrieDateField.java index 604d510b025..bba4d0ae69e 100755 --- a/solr/src/java/org/apache/solr/schema/TrieDateField.java +++ b/solr/src/java/org/apache/solr/schema/TrieDateField.java @@ -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); diff --git a/solr/src/java/org/apache/solr/schema/TrieField.java b/solr/src/java/org/apache/solr/schema/TrieField.java index 8d3c5bdb859..cd7d953c4ad 100644 --- a/solr/src/java/org/apache/solr/schema/TrieField.java +++ b/solr/src/java/org/apache/solr/schema/TrieField.java @@ -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; diff --git a/solr/src/java/org/apache/solr/update/DocumentBuilder.java b/solr/src/java/org/apache/solr/update/DocumentBuilder.java index 132b74c823c..8b099733d92 100644 --- a/solr/src/java/org/apache/solr/update/DocumentBuilder.java +++ b/solr/src/java/org/apache/solr/update/DocumentBuilder.java @@ -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);