You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2011/11/16 03:18:15 UTC

svn commit: r1202510 - in /lucene/dev/branches/branch_3x/solr/core: ./ src/java/org/apache/solr/update/DocumentBuilder.java src/test/org/apache/solr/update/DocumentBuilderTest.java

Author: hossman
Date: Wed Nov 16 02:18:14 2011
New Revision: 1202510

URL: http://svn.apache.org/viewvc?rev=1202510&view=rev
Log:
SOLR-2402: merge 1078928 from trunk (Add more debug info for DocumentBuilder errors)

Modified:
    lucene/dev/branches/branch_3x/solr/core/   (props changed)
    lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java
    lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java

Modified: lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java?rev=1202510&r1=1202509&r2=1202510&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java (original)
+++ lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java Wed Nov 16 02:18:14 2011
@@ -205,6 +205,15 @@ public class DocumentBuilder {
     }
   }
   
+  private static String getID( SolrInputDocument doc, IndexSchema schema )
+  {
+    String id = "";
+    SchemaField sf = schema.getUniqueKeyField();
+    if( sf != null ) {
+      id = "[doc="+doc.getFieldValue( sf.getName() )+"] ";
+    }
+    return id;
+  }
 
   /**
    * Convert a SolrInputDocument to a lucene Document.
@@ -234,91 +243,93 @@ public class DocumentBuilder {
       
       // Make sure it has the correct number
       if( sfield!=null && !sfield.multiValued() && field.getValueCount() > 1 ) {
-        String id = "";
-        SchemaField sf = schema.getUniqueKeyField();
-        if( sf != null ) {
-          id = "["+doc.getFieldValue( sf.getName() )+"] ";
-        }
         throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
-            "ERROR: "+id+"multiple values encountered for non multiValued field " + 
+            "ERROR: "+getID(doc, schema)+"multiple values encountered for non multiValued field " + 
               sfield.getName() + ": " +field.getValue() );
       }
       
 
       // load each field value
       boolean hasField = false;
-      for( Object v : field ) {
-        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);
-          }
-          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);
-          }
-        }
-
-        // Check if we should copy this field to any other fields.
-        // This could happen whether it is explicit or not.
-        List<CopyField> copyFields = schema.getCopyFieldsList(name);
-        for (CopyField cf : copyFields) {
-          SchemaField destinationField = cf.getDestination();
-          // check if the copy field is a multivalued or not
-          if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
-            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
-                    "ERROR: multiple values encountered for non multiValued copy field " +
-                            destinationField.getName() + ": " + val);
-          }
-
-          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)};
+      try {
+        for( Object v : field ) {
+          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);
             }
+            used = true;
           } else {
-            fields = destinationField.createFields(cf.getLimitedValue(val), boost);
+            // 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);
+            }
           }
-          if (fields != null) { // null fields are not added
-            for (Fieldable f : fields) {
-              out.add(f);
+  
+          // Check if we should copy this field to any other fields.
+          // This could happen whether it is explicit or not.
+          List<CopyField> copyFields = schema.getCopyFieldsList(name);
+          for (CopyField cf : copyFields) {
+            SchemaField destinationField = cf.getDestination();
+            // check if the copy field is a multivalued or not
+            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);
+            }
+  
+            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);
+            }
+            if (fields != null) { // null fields are not added
+              for (Fieldable f : fields) {
+                out.add(f);
+              }
             }
           }
+          
+          // In lucene, the boost for a given field is the product of the 
+          // document boost and *all* boosts on values of that field. 
+          // For multi-valued fields, we only want to set the boost on the
+          // first field.
+          boost = 1.0f; 
         }
-        
-        // In lucene, the boost for a given field is the product of the 
-        // document boost and *all* boosts on values of that field. 
-        // For multi-valued fields, we only want to set the boost on the
-        // first field.
-        boost = 1.0f; 
+      }
+      catch( Exception ex ) {
+        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
+            "ERROR: "+getID(doc, schema)+"Error adding field '" + 
+              field.getName() + "'='" +field.getValue()+"'", ex );
       }
       
       // make sure the field was used somehow...
       if( !used && hasField ) {
-        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"ERROR:unknown field '" +
-                name + "'");
+        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
+            "ERROR: "+getID(doc, schema)+"unknown field '" +name + "'");
       }
     }
     
@@ -331,8 +342,7 @@ public class DocumentBuilder {
           addField(out, field, field.getDefaultValue(), 1.0f);
         } 
         else {
-          String id = schema.printableUniqueKey( out );
-          String msg = "Document ["+id+"] missing required field: " + field.getName();
+          String msg = getID(doc, schema) + "missing required field: " + field.getName();
           throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, msg );
         }
       }

Modified: lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java?rev=1202510&r1=1202509&r2=1202510&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java (original)
+++ lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java Wed Nov 16 02:18:14 2011
@@ -67,6 +67,40 @@ public class DocumentBuilderTest extends
   }
 
   @Test
+  public void testExceptions() 
+  {
+    SolrCore core = h.getCore();
+    
+    // make sure a null value is not indexed
+    SolrInputDocument doc = new SolrInputDocument();
+    doc.addField( "id", "123", 1.0f );
+    doc.addField( "unknown", "something", 1.0f );
+    try {
+      DocumentBuilder.toDocument( doc, core.getSchema() );
+      fail( "added an unknown field" );
+    }
+    catch( Exception ex ) {
+      assertTrue( "should have document ID", ex.getMessage().indexOf( "doc=123" ) > 0 );
+    }
+    doc.remove( "unknown" );
+    
+
+    doc.addField( "weight", "not a number", 1.0f );
+    try {
+      DocumentBuilder.toDocument( doc, core.getSchema() );
+      fail( "invalid 'float' field value" );
+    }
+    catch( Exception ex ) {
+      assertTrue( "should have document ID", ex.getMessage().indexOf( "doc=123" ) > 0 );
+      assertTrue( "cause is number format", ex.getCause() instanceof NumberFormatException );
+    }
+    
+    // now make sure it is OK
+    doc.setField( "weight", "1.34", 1.0f );
+    DocumentBuilder.toDocument( doc, core.getSchema() );
+  }
+
+  @Test
   public void testMultiField() throws Exception {
     SolrCore core = h.getCore();