From 9f73bcf11d89f6ee7b2de8bea25be9018f4554cb Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Wed, 26 Jul 2017 21:10:21 +0930 Subject: [PATCH] SOLR-11130: V2Request is SolrJ should return the correct collection name --- .../apache/solr/client/solrj/SolrRequest.java | 5 +++++ .../client/solrj/impl/CloudSolrClient.java | 8 ++++---- .../solr/client/solrj/request/V2Request.java | 18 ++++++++++++++++-- .../client/solrj/request/TestV2Request.java | 9 +++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java index fe5c4fc77c8..37bc5e3ee6b 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java @@ -16,6 +16,7 @@ */ package org.apache.solr.client.solrj; +import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.ContentStream; @@ -195,4 +196,8 @@ public abstract class SolrRequest implements Serializabl return process(client, null); } + public String getCollection() { + return getParams() == null ? null : getParams().get("collection"); + } + } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java index eeb96af84e5..7da9d6ed743 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java @@ -786,10 +786,10 @@ public class CloudSolrClient extends SolrClient { @Override public NamedList request(SolrRequest request, String collection) throws SolrServerException, IOException { - SolrParams reqParams = request.getParams(); - - if (collection == null) - collection = (reqParams != null) ? reqParams.get("collection", getDefaultCollection()) : getDefaultCollection(); + if (collection == null) { + collection = request.getCollection(); + if (collection == null) collection = defaultCollection; + } return requestWithRetryOnStaleState(request, 0, collection); } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java index 39220d1fa3d..66944878ff3 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java @@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; import java.util.concurrent.atomic.AtomicLong; +import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.solr.client.solrj.SolrClient; @@ -38,14 +39,22 @@ import org.apache.solr.common.util.Utils; public class V2Request extends SolrRequest { //only for debugging purposes public static final ThreadLocal v2Calls = new ThreadLocal<>(); - static final Pattern COLL_REQ_PATTERN = Pattern.compile("/(c|collections)/[^/]+/(?!shards)"); + static final Pattern COLL_REQ_PATTERN = Pattern.compile("/(c|collections)/([^/])+/(?!shards)"); private InputStream payload; private SolrParams solrParams; public final boolean useBinary; + private String collection; + private boolean isPerCollectionRequest = false; private V2Request(METHOD m, String resource, boolean useBinary) { super(m, resource); + Matcher matcher = COLL_REQ_PATTERN.matcher(getPath()); + if (matcher.find()) { + this.collection = matcher.group(2); + isPerCollectionRequest = true; + } this.useBinary = useBinary; + } @Override @@ -73,7 +82,12 @@ public class V2Request extends SolrRequest { } public boolean isPerCollectionRequest() { - return COLL_REQ_PATTERN.matcher(getPath()).find(); + return isPerCollectionRequest; + } + + @Override + public String getCollection() { + return collection; } @Override diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestV2Request.java b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestV2Request.java index 61f771e9e12..566405f500d 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestV2Request.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestV2Request.java @@ -49,6 +49,7 @@ public class TestV2Request extends SolrCloudTestCase { assertFalse(new V2Request.Builder("/collections/a/shards").build().isPerCollectionRequest()); assertFalse(new V2Request.Builder("/collections/a/shards/").build().isPerCollectionRequest()); assertTrue(new V2Request.Builder("/collections/a/update").build().isPerCollectionRequest()); + assertEquals("a", new V2Request.Builder("/collections/a/update").build().getCollection()); assertTrue(new V2Request.Builder("/c/a/update").build().isPerCollectionRequest()); assertTrue(new V2Request.Builder("/c/a/schema").build().isPerCollectionRequest()); assertFalse(new V2Request.Builder("/c/a").build().isPerCollectionRequest()); @@ -80,10 +81,18 @@ public class TestV2Request extends SolrCloudTestCase { "}").build()); assertSuccess(client, new V2Request.Builder("/c").build()); assertSuccess(client, new V2Request.Builder("/c/_introspect").build()); + + + assertSuccess(client, new V2Request.Builder("/c/test/config") + .withMethod(SolrRequest.METHOD.POST) + .withPayload("{'create-requesthandler' : { 'name' : '/x', 'class': 'org.apache.solr.handler.DumpRequestHandler' , 'startup' : 'lazy'}}") + .build()); + assertSuccess(client, new V2Request.Builder("/c/test").withMethod(SolrRequest.METHOD.DELETE).build()); NamedList res = client.request(new V2Request.Builder("/c").build()); List collections = (List) res.get("collections"); assertEquals(0, collections.size()); + } }