From 68d180346ab14124c4286829c8ddeaac453e40d2 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Mon, 9 Mar 2015 11:54:45 +0000 Subject: [PATCH] SOLR-7201: HttpSolrClient can handle collection parameters git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1665199 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 4 +- .../client/solrj/impl/HttpSolrClient.java | 43 ++++++++++++++++--- .../solrj/impl/BasicHttpSolrClientTest.java | 18 ++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 8facb678630..a0d4841096c 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -136,8 +136,8 @@ New Features * SOLR-7164: BBoxField defaults sub fields to not-stored (ryan) -* SOLR-7155: All SolrClient methods now take an optional 'collection' argument - (Alan Woodward) +* SOLR-7155,SOLR-7201: All SolrClient methods now take an optional 'collection' argument + (Alan Woodward, Shawn Heisey) * SOLR-7073: Support adding a jar to a collections classpath (Noble Paul) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java index 9b6873ec804..80008c8fdae 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java @@ -75,6 +75,27 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +/** + * A SolrClient implementation that talks directly to a Solr server via HTTP + * + * There are two ways to use an HttpSolrClient: + * + * 1) Pass a URL to the constructor that points directly at a particular core + *
+ *   SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr/core1");
+ *   QueryResponse resp = client.query(new SolrQuery("*:*"));
+ * 
+ * In this case, you can query the given core directly, but you cannot query any other + * cores or issue CoreAdmin requests with this client. + * + * 2) Pass the base URL of the node to the constructor + *
+ *   SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr");
+ *   QueryResponse resp = client.query("core1", new SolrQuery("*:*"));
+ * 
+ * In this case, you must pass the name of the required core for all queries and updates, + * but you may use the same client for all cores, and for CoreAdmin requests. + */ public class HttpSolrClient extends SolrClient { private static final String UTF_8 = StandardCharsets.UTF_8.name(); @@ -204,11 +225,15 @@ public class HttpSolrClient extends SolrClient { if (responseParser == null) { responseParser = parser; } - return request(request, responseParser); + return request(request, responseParser, collection); + } + + public NamedList request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { + return request(request, processor, null); } - public NamedList request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { - return executeMethod(createMethod(request),processor); + public NamedList request(final SolrRequest request, final ResponseParser processor, String collection) throws SolrServerException, IOException { + return executeMethod(createMethod(request, collection),processor); } /** @@ -236,7 +261,7 @@ public class HttpSolrClient extends SolrClient { */ public HttpUriRequestResponse httpUriRequest(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { HttpUriRequestResponse mrr = new HttpUriRequestResponse(); - final HttpRequestBase method = createMethod(request); + final HttpRequestBase method = createMethod(request, null); ExecutorService pool = Executors.newFixedThreadPool(1, new SolrjNamedThreadFactory("httpUriRequest")); try { mrr.future = pool.submit(new Callable>(){ @@ -271,7 +296,7 @@ public class HttpSolrClient extends SolrClient { return queryModParams; } - protected HttpRequestBase createMethod(final SolrRequest request) throws IOException, SolrServerException { + protected HttpRequestBase createMethod(final SolrRequest request, String collection) throws IOException, SolrServerException { HttpRequestBase method = null; InputStream is = null; SolrParams params = request.getParams(); @@ -296,6 +321,10 @@ public class HttpSolrClient extends SolrClient { if (invariantParams != null) { wparams.add(invariantParams); } + + String basePath = baseUrl; + if (collection != null) + basePath += "/" + collection; int tries = maxRetries + 1; try { @@ -309,11 +338,11 @@ public class HttpSolrClient extends SolrClient { if( streams != null ) { throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!" ); } - method = new HttpGet( baseUrl + path + ClientUtils.toQueryString( wparams, false ) ); + method = new HttpGet(basePath + path + ClientUtils.toQueryString(wparams, false)); } else if( SolrRequest.METHOD.POST == request.getMethod() || SolrRequest.METHOD.PUT == request.getMethod() ) { - String url = baseUrl + path; + String url = basePath + path; boolean hasNullStreamName = false; if (streams != null) { for (ContentStream cs : streams) { diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java index 6b44912d704..86775b4571e 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java @@ -509,6 +509,24 @@ public class BasicHttpSolrClientTest extends SolrJettyTestBase { assertEquals(0, response.getStatus()); } } + + @Test + public void testCollectionParameters() throws IOException, SolrServerException { + + try (HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString())) { + SolrInputDocument doc = new SolrInputDocument(); + doc.addField("id", "collection"); + client.add("collection1", doc); + client.commit("collection1"); + + assertEquals(1, client.query("collection1", new SolrQuery("id:collection")).getResults().getNumFound()); + } + + try (HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString() + "/collection1")) { + assertEquals(1, client.query(new SolrQuery("id:collection")).getResults().getNumFound()); + } + + } @Test public void testSetParametersExternalClient() throws IOException{