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 12:54:46 UTC
svn commit: r1665199 - in /lucene/dev/trunk/solr: CHANGES.txt
solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java
Author: romseygeek
Date: Mon Mar 9 11:54:45 2015
New Revision: 1665199
URL: http://svn.apache.org/r1665199
Log:
SOLR-7201: HttpSolrClient can handle collection parameters
Modified:
lucene/dev/trunk/solr/CHANGES.txt
lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java
Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1665199&r1=1665198&r2=1665199&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Mon Mar 9 11:54:45 2015
@@ -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)
Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java?rev=1665199&r1=1665198&r2=1665199&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java Mon Mar 9 11:54:45 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/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java?rev=1665199&r1=1665198&r2=1665199&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java (original)
+++ lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrClientTest.java Mon Mar 9 11:54:45 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{