diff --git a/solr/core/src/java/org/apache/solr/update/processor/DistributedUpdateProcessor.java b/solr/core/src/java/org/apache/solr/update/processor/DistributedUpdateProcessor.java index 622776597a1..9a4ca0ab29c 100644 --- a/solr/core/src/java/org/apache/solr/update/processor/DistributedUpdateProcessor.java +++ b/solr/core/src/java/org/apache/solr/update/processor/DistributedUpdateProcessor.java @@ -516,12 +516,41 @@ public class DistributedUpdateProcessor extends UpdateRequestProcessor { oldDoc.addField( sif.getName(), fieldVal, sif.getBoost()); } else if ("set".equals(key)) { oldDoc.setField(sif.getName(), fieldVal, sif.getBoost()); + } else if ("inc".equals(key)) { + SolrInputField numericField = oldDoc.get(sif.getName()); + if (numericField == null) { + oldDoc.setField(sif.getName(), fieldVal, sif.getBoost()); + } else { + // TODO: fieldtype needs externalToObject? + String oldValS = numericField.getFirstValue().toString(); + SchemaField sf = cmd.getReq().getSchema().getField(sif.getName()); + BytesRef term = new BytesRef(); + sf.getType().readableToIndexed(oldValS, term); + Object oldVal = sf.getType().toObject(sf, term); + + String fieldValS = fieldVal.toString(); + Number result; + if (oldVal instanceof Long) { + result = ((Long) oldVal).longValue() + Long.parseLong(fieldValS); + } else if (oldVal instanceof Float) { + result = ((Float) oldVal).floatValue() + Float.parseFloat(fieldValS); + } else if (oldVal instanceof Double) { + result = ((Double) oldVal).doubleValue() + Double.parseDouble(fieldValS); + } else { + // int, short, byte + result = ((Integer) oldVal).intValue() + Integer.parseInt(fieldValS); + } + + oldDoc.setField(sif.getName(), result, sif.getBoost()); + } + } } } else { // normal fields are treated as a "set" oldDoc.put(sif.getName(), sif); } + } cmd.solrDoc = oldDoc; diff --git a/solr/core/src/test/org/apache/solr/update/TestUpdate.java b/solr/core/src/test/org/apache/solr/update/TestUpdate.java index c63e2d876eb..4665b756807 100644 --- a/solr/core/src/test/org/apache/solr/update/TestUpdate.java +++ b/solr/core/src/test/org/apache/solr/update/TestUpdate.java @@ -159,6 +159,50 @@ public class TestUpdate extends SolrTestCaseJ4 { assertEquals(409, se.code()); } + version = addAndGetVersion(sdoc("id","1", "val_i",5), null); + afterUpdate.call(); + + version = addAndGetVersion(sdoc("id","1", + "val_is",map("inc",1), + "val2_i",map("inc","1"), + "val2_f",map("inc",1), + "val2_d",map("inc","1.0"), + "val2_l",map("inc",1) + ), + null); + afterUpdate.call(); + + assertJQ(req("qt","/get", "id","1", "fl","id,val*") + ,"=={'doc':{'id':'1', 'val_i':5, 'val_is':[1], 'val2_i':1, 'val2_f':1.0, 'val2_d':1.0, 'val2_l':1}}" + ); + + version = addAndGetVersion(sdoc("id","1", + "val_is",map("inc","-5"), + "val2_i",map("inc",-5), + "val2_f",map("inc","-5.0"), + "val2_d",map("inc",-5), + "val2_l",map("inc","-5") + ), + null); + afterUpdate.call(); + + assertJQ(req("qt","/get", "id","1", "fl","id,val*") + ,"=={'doc':{'id':'1', 'val_i':5, 'val_is':[-4], 'val2_i':-4, 'val2_f':-4.0, 'val2_d':-4.0, 'val2_l':-4}}" + ); + + version = addAndGetVersion(sdoc("id","1", + "val_is",map("inc","2000000000"), + "val2_i",map("inc",-2000000000), + "val2_f",map("inc","1e+20"), + "val2_d",map("inc",-1.2345678901e+100), + "val2_l",map("inc","5000000000") + ), + null); + afterUpdate.call(); + + assertJQ(req("qt","/get", "id","1", "fl","id,val*") + ,"=={'doc':{'id':'1', 'val_i':5, 'val_is':[1999999996], 'val2_i':-2000000004, 'val2_f':1.0E20, 'val2_d':-1.2345678901e+100, 'val2_l':4999999996}}" + ); }