Apache Solr open-source search software
SOLR-2714: drop null field values in json update format
Yonik Seeleye8072dd6683d
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex d4672b67562..bd1a287c959 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -382,6 +382,10 @@ New Featuresspecified when CREATEing a new SolrCore using property.* requestparams. (Yury Kats, hossman)+* SOLR-2714: JSON update format - "null" field values are now dropped+ instead of causing an exception. (Trygve Laugstøl, yonik)++Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/handler/JsonLoader.java b/solr/core/src/java/org/apache/solr/handler/JsonLoader.javaindex acc0fe08434..68f487a845c 100644--- a/solr/core/src/java/org/apache/solr/handler/JsonLoader.java+++ b/solr/core/src/java/org/apache/solr/handler/JsonLoader.java@@ -132,6 +132,7 @@ class JsonLoader extends ContentStreamLoader {case JSONParser.NUMBER:case JSONParser.BIGNUMBER:case JSONParser.BOOLEAN:+ case JSONParser.NULL:log.info( "can't have a value here! "+JSONParser.getEventString(ev)+" "+parser.getPosition() );@@ -321,7 +322,7 @@ class JsonLoader extends ContentStreamLoader {Stack<Object> stack = new Stack<Object>();Object obj = null;boolean inArray = false;-+if( ev != JSONParser.OBJECT_START ) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "object should already be started" );}@@ -343,7 +344,7 @@ class JsonLoader extends ContentStreamLoader {ev != JSONParser.BIGNUMBER ) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "boost should have number! "+JSONParser.getEventString(ev) );}- field.setBoost( Float.valueOf( parser.getNumberChars().toString() ) );+ field.setBoost((float)parser.getDouble());}else if( "value".equals( v ) ) {// nothing special...@@ -380,7 +381,25 @@ class JsonLoader extends ContentStreamLoader {case JSONParser.BOOLEAN:addValToField(stack, parser.getBoolean(),inArray, parser);break;-++ case JSONParser.NULL:+ parser.getNull();+ /*** if we wanted to remove the field from the document now...+ if (!inArray) {+ Object o = stack.peek();+ // if null was only value in the field, then remove the field+ if (o instanceof SolrInputField) {+ SolrInputField sif = (SolrInputField)o;+ if (sif.getValueCount() == 0) {+ sdoc.remove(sif.getName());+ }+ }+ }+ ***/++ addValToField(stack, null, inArray, parser);+ break;+case JSONParser.OBJECT_START:if( stack.isEmpty() ) {stack.push( new SolrInputDocument() );@@ -395,7 +414,6 @@ class JsonLoader extends ContentStreamLoader {}}break;-case JSONParser.OBJECT_END:obj = stack.pop();if( obj instanceof SolrInputDocument ) {@@ -436,11 +454,13 @@ class JsonLoader extends ContentStreamLoader {if( !(obj instanceof SolrInputField) ) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "hymmm ["+parser.getPosition()+"]" );}-+SolrInputField f = inArray? (SolrInputField)obj: (SolrInputField)stack.pop();-++ if (val == null) return;+float boost = (f.getValue()==null)?f.getBoost():1.0f;f.addValue( val,boost );}diff --git a/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.javaindex 8053418f7ac..a16a965a3a2 100644--- a/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java+++ b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java@@ -34,6 +34,7 @@ import org.apache.solr.update.DeleteUpdateCommand;import org.apache.solr.update.RollbackUpdateCommand;import org.apache.solr.update.processor.UpdateRequestProcessor;import org.junit.BeforeClass;+import org.junit.Test;public class JsonLoaderTest extends SolrTestCaseJ4 {@BeforeClass@@ -64,7 +65,8 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {" 'boost': 3.45,\n" +" 'doc': {\n" +" 'f1': 'v1',\n" +- " 'f1': 'v2'\n" ++ " 'f1': 'v2',\n" ++ " 'f2': null\n" +" }\n" +"},\n" +"\n" +@@ -102,7 +104,8 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {assertEquals(2, f.getValues().size());assertEquals(3.45f, d.getDocumentBoost());assertEquals(false, add.overwrite);-++ assertEquals(0, d.getField("f2").getValueCount());// parse the commit commandsassertEquals( 2, p.commitCommands.size() );@@ -188,6 +191,14 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {req.close();}+ @Test+ public void testNullValues() throws Exception {+ updateJ("[{'id':'10','foo_s':null,'foo2_s':['hi',null,'there']}]".replace('\'', '"'), params("commit","true"));+ assertJQ(req("q","id:10", "fl","foo_s,foo2_s")+ ,"/response/docs/[0]=={'foo2_s':['hi','there']}"+ );+ }+}