Apache Solr open-source search software
SOLR-4133: Cannot "set" field to null with partial updates when using the standard RequestWriter
Shalin Shekhar Mangar460dec2c561e
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex c9982b84828..f4b04da227d 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -378,6 +378,9 @@ Bug Fixes* SOLR-3972: Fix ShowFileRequestHandler to not log a warning in the(expected) situation of a file not found. (hossman)+* SOLR-4133: Cannot "set" field to null with partial updates when using the+ standard RequestWriter. (Will Butler, shalin)+Other Changes----------------------diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.java b/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.javaindex 15b9db2157e..82639671773 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.java@@ -147,8 +147,12 @@ public class ClientUtils} else {if( boost != 1.0f ) {XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost, "update", update);- } else if (v != null) {- XML.writeXML(writer, "field", v.toString(), "name", name, "update", update);+ } else {+ if (v == null) {+ XML.writeXML(writer, "field", null, "name", name, "update", update, "null", true);+ } else {+ XML.writeXML(writer, "field", v.toString(), "name", name, "update", update);+ }}}}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.javaindex 975e40fb84f..d453a986b7f 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java@@ -21,6 +21,7 @@ package org.apache.solr.client.solrj;import java.io.IOException;import java.lang.reflect.Field;import java.util.ArrayList;+import java.util.Arrays;import java.util.Collection;import java.util.HashMap;import java.util.List;@@ -29,6 +30,7 @@ import java.util.Random;import java.util.concurrent.atomic.AtomicInteger;import com.google.common.collect.Lists;+import com.google.common.collect.Maps;import junit.framework.Assert;import org.apache.lucene.util._TestUtil;@@ -1392,7 +1394,7 @@ abstract public class SolrExampleTests extends SolrJettyTestBaseassertEquals("Failed to add doc to cloud server", 1, response.getResults().getNumFound());Map<String, List<String>> operation = new HashMap<String, List<String>>();- operation.put("set", Lists.asList("first", "second", new String[]{"third"}));+ operation.put("set", Arrays.asList("first", "second", "third"));doc.addField("multi_ss", operation);solrServer.add(doc);solrServer.commit(true, true);@@ -1402,7 +1404,7 @@ abstract public class SolrExampleTests extends SolrJettyTestBaseassertEquals("Field values was not updated with all values via atomic update", 3, values.size());operation.clear();- operation.put("add", Lists.asList("fourth", new String[]{"fifth"}));+ operation.put("add", Arrays.asList("fourth", "fifth"));doc.removeField("multi_ss");doc.addField("multi_ss", operation);solrServer.add(doc);@@ -1411,6 +1413,27 @@ abstract public class SolrExampleTests extends SolrJettyTestBasevalues = (List<String>) response.getResults().get(0).get("multi_ss");assertEquals("Field values was not updated with all values via atomic update", 5, values.size());}++ @Test+ public void testSetNullUpdates() throws Exception {+ SolrServer solrServer = getSolrServer();+ SolrInputDocument doc = new SolrInputDocument();+ doc.addField("id", "testSetNullUpdates");+ doc.addField("single_s", "test-value");+ doc.addField("multi_ss", Arrays.asList("first", "second"));+ solrServer.add(doc);+ solrServer.commit(true, true);+ doc.removeField("single_s");+ doc.removeField("multi_ss");+ Map<String, Object> map = Maps.newHashMap();+ map.put("set", null);+ doc.addField("multi_ss", map);+ solrServer.add(doc);+ solrServer.commit(true, true);+ QueryResponse response = solrServer.query(new SolrQuery("id:testSetNullUpdates"));+ assertNotNull("Entire doc was replaced because null update was not written", response.getResults().get(0).getFieldValue("single_s"));+ assertNull("Null update failed. Value still exists in document", response.getResults().get(0).getFieldValue("multi_ss"));+ }@Testpublic void testQueryWithParams() throws SolrServerException {