SOLR-2714: drop null field values in json update format

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1163370 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2011-08-30 20:46:24 +00:00
parent 47450677b9
commit e8072dd668
3 changed files with 43 additions and 8 deletions

View File

@ -382,6 +382,10 @@ New Features
specified when CREATEing a new SolrCore using property.* request specified when CREATEing a new SolrCore using property.* request
params. (Yury Kats, hossman) params. (Yury Kats, hossman)
* SOLR-2714: JSON update format - "null" field values are now dropped
instead of causing an exception. (Trygve Laugstøl, yonik)
Optimizations Optimizations
---------------------- ----------------------

View File

@ -132,6 +132,7 @@ class JsonLoader extends ContentStreamLoader {
case JSONParser.NUMBER: case JSONParser.NUMBER:
case JSONParser.BIGNUMBER: case JSONParser.BIGNUMBER:
case JSONParser.BOOLEAN: case JSONParser.BOOLEAN:
case JSONParser.NULL:
log.info( "can't have a value here! " log.info( "can't have a value here! "
+JSONParser.getEventString(ev)+" "+parser.getPosition() ); +JSONParser.getEventString(ev)+" "+parser.getPosition() );
@ -321,7 +322,7 @@ class JsonLoader extends ContentStreamLoader {
Stack<Object> stack = new Stack<Object>(); Stack<Object> stack = new Stack<Object>();
Object obj = null; Object obj = null;
boolean inArray = false; boolean inArray = false;
if( ev != JSONParser.OBJECT_START ) { if( ev != JSONParser.OBJECT_START ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "object should already be started" ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "object should already be started" );
} }
@ -343,7 +344,7 @@ class JsonLoader extends ContentStreamLoader {
ev != JSONParser.BIGNUMBER ) { ev != JSONParser.BIGNUMBER ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "boost should have number! "+JSONParser.getEventString(ev) ); 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 ) ) { else if( "value".equals( v ) ) {
// nothing special... // nothing special...
@ -380,7 +381,25 @@ class JsonLoader extends ContentStreamLoader {
case JSONParser.BOOLEAN: case JSONParser.BOOLEAN:
addValToField(stack, parser.getBoolean(),inArray, parser); addValToField(stack, parser.getBoolean(),inArray, parser);
break; 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: case JSONParser.OBJECT_START:
if( stack.isEmpty() ) { if( stack.isEmpty() ) {
stack.push( new SolrInputDocument() ); stack.push( new SolrInputDocument() );
@ -395,7 +414,6 @@ class JsonLoader extends ContentStreamLoader {
} }
} }
break; break;
case JSONParser.OBJECT_END: case JSONParser.OBJECT_END:
obj = stack.pop(); obj = stack.pop();
if( obj instanceof SolrInputDocument ) { if( obj instanceof SolrInputDocument ) {
@ -436,11 +454,13 @@ class JsonLoader extends ContentStreamLoader {
if( !(obj instanceof SolrInputField) ) { if( !(obj instanceof SolrInputField) ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "hymmm ["+parser.getPosition()+"]" ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "hymmm ["+parser.getPosition()+"]" );
} }
SolrInputField f = inArray SolrInputField f = inArray
? (SolrInputField)obj ? (SolrInputField)obj
: (SolrInputField)stack.pop(); : (SolrInputField)stack.pop();
if (val == null) return;
float boost = (f.getValue()==null)?f.getBoost():1.0f; float boost = (f.getValue()==null)?f.getBoost():1.0f;
f.addValue( val,boost ); f.addValue( val,boost );
} }

View File

@ -34,6 +34,7 @@ import org.apache.solr.update.DeleteUpdateCommand;
import org.apache.solr.update.RollbackUpdateCommand; import org.apache.solr.update.RollbackUpdateCommand;
import org.apache.solr.update.processor.UpdateRequestProcessor; import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test;
public class JsonLoaderTest extends SolrTestCaseJ4 { public class JsonLoaderTest extends SolrTestCaseJ4 {
@BeforeClass @BeforeClass
@ -64,7 +65,8 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {
" 'boost': 3.45,\n" + " 'boost': 3.45,\n" +
" 'doc': {\n" + " 'doc': {\n" +
" 'f1': 'v1',\n" + " 'f1': 'v1',\n" +
" 'f1': 'v2'\n" + " 'f1': 'v2',\n" +
" 'f2': null\n" +
" }\n" + " }\n" +
"},\n" + "},\n" +
"\n" + "\n" +
@ -102,7 +104,8 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {
assertEquals(2, f.getValues().size()); assertEquals(2, f.getValues().size());
assertEquals(3.45f, d.getDocumentBoost()); assertEquals(3.45f, d.getDocumentBoost());
assertEquals(false, add.overwrite); assertEquals(false, add.overwrite);
assertEquals(0, d.getField("f2").getValueCount());
// parse the commit commands // parse the commit commands
assertEquals( 2, p.commitCommands.size() ); assertEquals( 2, p.commitCommands.size() );
@ -188,6 +191,14 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {
req.close(); 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']}"
);
}
} }