Skip to content

Apache Lucene open-source search software

SOLR-5380: Using cloudSolrServer.setDefaultCollection(collectionId) does not work as intended for an alias spanning more than 1 collection.

Mark Robert Miller9c47892d4ea8
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 8d7d42f700c..2140a9d1ad7 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -122,6 +122,10 @@ Bug Fixes
* SOLR-5363: Solr doesn't start up properly with Log4J2 (Petar Tahchiev via Alan
Woodward)
+* SOLR-5380: Using cloudSolrServer.setDefaultCollection(collectionId) does not
+ work as intended for an alias spanning more than 1 collection.
+ (Thomas Egense, Shawn Heisey, Mark Miller)
+
Optimizations
----------------------
diff --git a/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java b/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java
index 0815bf699c7..16d9b48467d 100644
--- a/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java
@@ -157,6 +157,12 @@ public class AliasIntegrationTest extends AbstractFullDistribZkTestBase {
query = new SolrQuery("*:*");
query.set("collection", "testalias");
res = cloudSolrServer.query(query);
+ assertEquals(5, res.getResults().getNumFound());
+
+ // Try with setDefaultCollection
+ query = new SolrQuery("*:*");
+ cloudSolrServer.setDefaultCollection("testalias");
+ res = cloudSolrServer.query(query);
cloudSolrServer.shutdown();
assertEquals(5, res.getResults().getNumFound());
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrServer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrServer.java
index b6537be818b..1130e15cd0b 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrServer.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrServer.java
@@ -546,17 +546,7 @@ public class CloudSolrServer extends SolrServer {
throw new SolrException(ErrorCode.BAD_REQUEST,
"Could not find collection: " + collection);
}
- collection = collectionsList.iterator().next();
-
- StringBuilder collectionString = new StringBuilder();
- Iterator<String> it = collectionsList.iterator();
- for (int i = 0; i < collectionsList.size(); i++) {
- String col = it.next();
- collectionString.append(col);
- if (i < collectionsList.size() - 1) {
- collectionString.append(",");
- }
- }
+
// TODO: not a big deal because of the caching, but we could avoid looking
// at every shard
// when getting leaders if we tweaked some things
@@ -592,10 +582,24 @@ public class CloudSolrServer extends SolrServer {
|| !coreNodeProps.getState().equals(ZkStateReader.ACTIVE)) continue;
if (nodes.put(node, nodeProps) == null) {
if (!sendToLeaders || (sendToLeaders && coreNodeProps.isLeader())) {
- String url = coreNodeProps.getCoreUrl();
+ String url;
+ if (reqParams.get("collection") == null) {
+ url = ZkCoreNodeProps.getCoreUrl(
+ nodeProps.getStr(ZkStateReader.BASE_URL_PROP),
+ defaultCollection);
+ } else {
+ url = coreNodeProps.getCoreUrl();
+ }
urlList2.add(url);
} else if (sendToLeaders) {
- String url = coreNodeProps.getCoreUrl();
+ String url;
+ if (reqParams.get("collection") == null) {
+ url = ZkCoreNodeProps.getCoreUrl(
+ nodeProps.getStr(ZkStateReader.BASE_URL_PROP),
+ defaultCollection);
+ } else {
+ url = coreNodeProps.getCoreUrl();
+ }
replicas.add(url);
}
}