SOLR-10250: CloudSolrClient can now return versions for documents added or deleted when versions=true is passed

This commit is contained in:
Ishan Chattopadhyaya 2017-03-13 19:53:22 +05:30
parent a06c39f3e5
commit ceffbf9844
3 changed files with 68 additions and 1 deletions

View File

@ -169,6 +169,10 @@ New Features
* SOLR-10039: New LatLonPointSpatialField replacement for LatLonType (and some uses of RPT). Multi-value capable
indexed geo lat-lon points, query by rect or circle. Efficient distance sorting/boosting too. (David Smiley)
* SOLR-10250: CloudSolrClient can now return versions for documents added or deleted when "versions=true" is passed.
However, if there is a leader election while this request is in transit, the versions may not be returned from that
shard. (Boris Naguet, Ishan Chattopadhyaya)
Bug Fixes
----------------------

View File

@ -904,6 +904,9 @@ public class CloudSolrClient extends SolrClient {
List<SimpleOrderedMap<String>> toleratedErrors = null;
int maxToleratedErrors = Integer.MAX_VALUE;
// For "adds", "deletes", "deleteByQuery" etc.
Map<String, NamedList> versions = new HashMap<>();
for(int i=0; i<response.size(); i++) {
NamedList shardResponse = (NamedList)response.getVal(i);
NamedList header = (NamedList)shardResponse.get("responseHeader");
@ -937,6 +940,15 @@ public class CloudSolrClient extends SolrClient {
toleratedErrors.add(err);
}
}
for (String updateType: Arrays.asList("adds", "deletes", "deleteByQuery")) {
Object obj = shardResponse.get(updateType);
if (obj instanceof NamedList) {
NamedList versionsList = versions.containsKey(updateType) ?
versions.get(updateType): new NamedList();
versionsList.addAll((NamedList)obj);
versions.put(updateType, versionsList);
}
}
}
NamedList cheader = new NamedList();
@ -971,6 +983,9 @@ public class CloudSolrClient extends SolrClient {
throw toThrow;
}
}
for (String updateType: versions.keySet()) {
condensed.add(updateType, versions.get(updateType));
}
condensed.add("responseHeader", cheader);
return condensed;
}

View File

@ -49,6 +49,7 @@ import org.apache.solr.client.solrj.response.RequestStatusState;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.cloud.AbstractDistribZkTestBase;
import org.apache.solr.cloud.SolrCloudTestCase;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
@ -61,6 +62,7 @@ import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.params.UpdateParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.handler.admin.CollectionsHandler;
@ -633,6 +635,52 @@ public class CloudSolrClientTest extends SolrCloudTestCase {
}
}
@Test
public void testVersionsAreReturned() throws Exception {
// assert that "adds" are returned
UpdateRequest updateRequest = new UpdateRequest()
.add("id", "1", "a_t", "hello1")
.add("id", "2", "a_t", "hello2");
updateRequest.setParam(UpdateParams.VERSIONS, Boolean.TRUE.toString());
NamedList<Object> response = updateRequest.commit(cluster.getSolrClient(), COLLECTION).getResponse();
Object addsObject = response.get("adds");
assertNotNull("There must be a adds parameter", addsObject);
assertTrue(addsObject instanceof NamedList<?>);
NamedList<?> adds = (NamedList<?>) addsObject;
assertEquals("There must be 2 versions (one per doc)", 2, adds.size());
Map<String, Long> versions = new HashMap<>();
Object object = adds.get("1");
assertNotNull("There must be a version for id 1", object);
assertTrue("Version for id 1 must be a long", object instanceof Long);
versions.put("1", (Long) object);
object = adds.get("2");
assertNotNull("There must be a version for id 2", object);
assertTrue("Version for id 2 must be a long", object instanceof Long);
versions.put("2", (Long) object);
QueryResponse resp = cluster.getSolrClient().query(COLLECTION, new SolrQuery("*:*"));
assertEquals("There should be one document because overwrite=true", 2, resp.getResults().getNumFound());
for (SolrDocument doc : resp.getResults()) {
Long version = versions.get(doc.getFieldValue("id"));
assertEquals("Version on add must match _version_ field", version, doc.getFieldValue("_version_"));
}
// assert that "deletes" are returned
UpdateRequest deleteRequest = new UpdateRequest().deleteById("1");
deleteRequest.setParam(UpdateParams.VERSIONS, Boolean.TRUE.toString());
response = deleteRequest.commit(cluster.getSolrClient(), COLLECTION).getResponse();
Object deletesObject = response.get("deletes");
assertNotNull("There must be a deletes parameter", deletesObject);
NamedList deletes = (NamedList) deletesObject;
assertEquals("There must be 1 version", 1, deletes.size());
}
private static void checkSingleServer(NamedList<Object> response) {
final CloudSolrClient.RouteResponse rr = (CloudSolrClient.RouteResponse) response;
final Map<String,LBHttpSolrClient.Req> routes = rr.getRoutes();