Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-13719: introducing SolrClient.ping(collection)

Mikhail Khludnev1cf7368ed8f5
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 5f4fcf2a519..33df7f1da16 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -187,6 +187,8 @@ Improvements
* SOLR-13795: Managed schema operations should do a core reload in Solr standalone mode.
(Thomas Wöckinger via David Smiley)
+* SOLR-13719: Introducing SolrClient.ping(collection) in SolrJ (Geza Nagy via Mikhail Khludnev)
+
Bug Fixes
----------------------
diff --git a/solr/solr-ref-guide/src/ping.adoc b/solr/solr-ref-guide/src/ping.adoc
index c1de95c837b..ed4e7ce481b 100644
--- a/solr/solr-ref-guide/src/ping.adoc
+++ b/solr/solr-ref-guide/src/ping.adoc
@@ -69,7 +69,7 @@ This command will ping all replicas of the given collection name for a response:
Both API calls have the same output. A status=OK indicates that the nodes are responding.
-*SolrJ Example*
+*SolrJ Example with SolrPing*
[source,java]
----
@@ -78,3 +78,12 @@ ping.getParams().add("distrib", "true"); //To make it a distributed request agai
rsp = ping.process(solrClient, collectionName);
int status = rsp.getStatus();
----
+
+*SolrJ Example with SolrClient*
+
+[source,java]
+----
+SolrClient client = new HttpSolrClient.Builder(solrUrl).build();
+SolrPingResponse pingResponse = client.ping(collectionName);
+int status = pingResponse.getStatus();
+----
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java
index 885edc9d95c..0bbdc1a2ad3 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java
@@ -958,6 +958,21 @@ public abstract class SolrClient implements Serializable, Closeable {
return deleteByQuery(null, query, commitWithinMs);
}
+ /**
+ * Issues a ping request to check if the collection's replicas are alive
+ *
+ * @param collection collection to ping
+ *
+ * @return a {@link org.apache.solr.client.solrj.response.SolrPingResponse} containing the response
+ * from the server
+ *
+ * @throws IOException If there is a low-level I/O error.
+ * @throws SolrServerException if there is an error on the server
+ */
+ public SolrPingResponse ping(String collection) throws SolrServerException, IOException {
+ return new SolrPing().process(this, collection);
+ }
+
/**
* Issues a ping request to check if the server is alive
*
@@ -971,6 +986,7 @@ public abstract class SolrClient implements Serializable, Closeable {
return new SolrPing().process(this, null);
}
+
/**
* Performs a query to the Solr server
*
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java
index 1c9ba04a5d1..a505799c6e3 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientTest.java
@@ -49,6 +49,7 @@ import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.RequestStatusState;
import org.apache.solr.client.solrj.response.UpdateResponse;
+import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.cloud.AbstractDistribZkTestBase;
import org.apache.solr.cloud.SolrCloudTestCase;
import org.apache.solr.common.SolrDocument;
@@ -951,4 +952,16 @@ public class CloudSolrClientTest extends SolrCloudTestCase {
log.info("Shards giving the response: " + Arrays.toString(shardAddresses.toArray()));
}
+ @Test
+ public void testPing() throws Exception {
+ final String testCollection = "ping_test";
+ CollectionAdminRequest.createCollection(testCollection, "conf", 2, 1).process(cluster.getSolrClient());
+ cluster.waitForActiveCollection(testCollection, 2, 2);
+ final SolrClient clientUnderTest = getRandomClient();
+
+ final SolrPingResponse response = clientUnderTest.ping(testCollection);
+
+ assertEquals("This should be OK", 0, response.getStatus());
+ }
+
}