Skip to content

Apache Lucene open-source search software

DocumentBuilder needs to skip null values. This adds a test to make sure that happens.

Ryan McKinley06b56541a614
diff --git a/src/java/org/apache/solr/update/DocumentBuilder.java b/src/java/org/apache/solr/update/DocumentBuilder.java
index 14dc78d2501..f9d94d8538a 100644
--- a/src/java/org/apache/solr/update/DocumentBuilder.java
+++ b/src/java/org/apache/solr/update/DocumentBuilder.java
@@ -188,8 +188,13 @@ public class DocumentBuilder {
SchemaField[] destArr = schema.getCopyFields(name);
// load each field value
+ boolean hasField = false;
for( Object v : field ) {
+ if( v == null ) {
+ continue;
+ }
String val = null;
+ hasField = true;
// TODO!!! HACK -- date conversion
if( sfield != null && v instanceof Date && sfield.getType() instanceof DateField ) {
@@ -232,7 +237,7 @@ public class DocumentBuilder {
}
// make sure the field was used somehow...
- if( !used ) {
+ if( !used && hasField ) {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"ERROR:unknown field '" + name + "'");
}
}
diff --git a/src/test/org/apache/solr/update/DocumentBuilderTest.java b/src/test/org/apache/solr/update/DocumentBuilderTest.java
index 4152cb34cf9..04dabf060a1 100644
--- a/src/test/org/apache/solr/update/DocumentBuilderTest.java
+++ b/src/test/org/apache/solr/update/DocumentBuilderTest.java
@@ -17,6 +17,7 @@
package org.apache.solr.update;
+import org.apache.lucene.document.Document;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.core.SolrCore;
@@ -47,4 +48,15 @@ public class DocumentBuilderTest extends AbstractSolrTestCase {
assertEquals( "should be bad request", 400, ex.code() );
}
}
+
+ public void testNullField()
+ {
+ SolrCore core = SolrCore.getSolrCore();
+
+ // make sure a null value is not indexed
+ SolrInputDocument doc = new SolrInputDocument();
+ doc.addField( "name", null, 1.0f );
+ Document out = DocumentBuilder.toDocument( doc, core.getSchema() );
+ assertNull( out.get( "name" ) );
+ }
}