You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2015/03/09 13:13:25 UTC

svn commit: r1665203 - in /lucene/dev/branches/branch_5x: ./ solr/ solr/solrj/ solr/solrj/src/java/org/apache/solr/client/solrj/impl/ solr/solrj/src/test/org/apache/solr/client/solrj/impl/

Author: romseygeek
Date: Mon Mar  9 12:13:24 2015
New Revision: 1665203

URL: http://svn.apache.org/r1665203
Log:
SOLR-7201: HttpSolrClient can handle collection parameters

Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/solr/   (props changed)
    lucene/dev/branches/branch_5x/solr/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_5x/solr/solrj/   (props changed)
    lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
    lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java
    lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java

Modified: lucene/dev/branches/branch_5x/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/CHANGES.txt?rev=1665203&r1=1665202&r2=1665203&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/solr/CHANGES.txt Mon Mar  9 12:13:24 2015
@@ -84,8 +84,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-6359: Allow number of logs and records kept by UpdateLog to be configured
   (Ramkumar Aiyengar)

Modified: lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java?rev=1665203&r1=1665202&r2=1665203&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java (original)
+++ lucene/dev/branches/branch_5x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java Mon Mar  9 12:13:24 2015
@@ -75,6 +75,27 @@ import java.util.concurrent.ExecutorServ
 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
+ * <pre>
+ *   SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr/core1");
+ *   QueryResponse resp = client.query(new SolrQuery("*:*"));
+ * </pre>
+ * 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
+ * <pre>
+ *   SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr");
+ *   QueryResponse resp = client.query("core1", new SolrQuery("*:*"));
+ * </pre>
+ * 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 Solr
     if (responseParser == null) {
       responseParser = parser;
     }
-    return request(request, responseParser);
+    return request(request, responseParser, collection);
   }
-  
+
   public NamedList<Object> request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException {
-    return executeMethod(createMethod(request),processor);
+    return request(request, processor, null);
+  }
+  
+  public NamedList<Object> 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 Solr
    */
   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<NamedList<Object>>(){
@@ -271,7 +296,7 @@ public class HttpSolrClient extends Solr
     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 Solr
     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 Solr
             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) {

Modified: lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java?rev=1665203&r1=1665202&r2=1665203&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java (original)
+++ lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java Mon Mar  9 12:13:24 2015
@@ -509,6 +509,24 @@ public class BasicHttpSolrClientTest ext
       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{

Modified: lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java?rev=1665203&r1=1665202&r2=1665203&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java (original)
+++ lucene/dev/branches/branch_5x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java Mon Mar  9 12:13:24 2015
@@ -17,7 +17,6 @@ package org.apache.solr.client.solrj.imp
  * limitations under the License.
  */
 
-import com.carrotsearch.randomizedtesting.annotations.Seed;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
@@ -78,7 +77,6 @@ import static org.apache.solr.common.clo
  * This test would be faster if we simulated the zk state instead.
  */
 @Slow
-@Seed("1CB6EE5E0046C651:94E2D184AEBAABA9")
 public class CloudSolrClientTest extends AbstractFullDistribZkTestBase {
   static Logger log = LoggerFactory.getLogger(CloudSolrClientTest.class);