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
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
----------------------

View File

@ -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 );
}

View File

@ -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 commands
assertEquals( 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']}"
);
}
}