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
This commit is contained in:
Alan Woodward 2015-03-09 11:54:45 +00:00
parent a8fd826f4a
commit 68d180346a
3 changed files with 56 additions and 9 deletions

View File

@ -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)

View File

@ -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
* <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 SolrClient {
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 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<NamedList<Object>>(){
@ -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();
@ -297,6 +322,10 @@ public class HttpSolrClient extends SolrClient {
wparams.add(invariantParams);
}
String basePath = baseUrl;
if (collection != null)
basePath += "/" + collection;
int tries = maxRetries + 1;
try {
while( tries-- > 0 ) {
@ -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) {

View File

@ -510,6 +510,24 @@ public class BasicHttpSolrClientTest extends SolrJettyTestBase {
}
}
@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{