Using SolrJ to Connect to SearchStax Solr Deployments

Using SolrJ to Connect to SearchStax

We are asked many times if customers can use SolrJ to connect to SearchStax Managed Solr deployments. The answer is yes, absolutely! 

This blog post provides a brief overview of SolrJ, explains why you need to use the HttpSolrClient client and shares some sample code for using SolrJ to connect to SearchStax.

Editor’s Note: Managed Solr is now SearchStax Cloud. It is the same great product that lets developers set up and deploy Solr infrastructure in minutes.

What is SolrJ?

Per the Apache Solr Reference Guide, “SolrJ is an API that makes it easy for applications written in Java or any language based on the JVM to talk to Solr.”

SolrJ hides a lot of the details for connecting to Solr and uses simple high-level methods to interact with Solr. The SolrJ API ships with Solr so you don’t need to download or install any other components. 

Connecting SolrJ to Managed Solr Using HttpSolrClient

The SolrJ client recommended to use with SearchStax Managed Solr is the HttpSolrClient – `org.apache.solr.client.solrj.impl.HttpSolrClient`. The HttpSolrClient lets a deployment connect directly to Solr via the Solr endpoint using HTTP.

SearchStax Solr deployments are configured in SolrCloud configuration. In addition, for security reasons, our Solr nodes register with the Zookeeper Ensemble using private IP addresses and are not accessible to the world by their Public IPs.

For this reason, SolrJ client classes that connect to Solr using Zookeeper do not work for SearchStax deployments.

Sample Code to Connect Using the SolrJ Client

Here is some sample code that lets you connect to Solr, add documents, and query using the SolrJ client. Refer to these notes before you use the sample code.

  • Basic Authentication – Note that the example code shows how to connect to Solr using Basic Authentication. If you do not have basic authentication, you can skip the `setBasicAuthCredentials` method.
  • IP Filtering – If your deployment has IP Filtering and you are not able to connect to Solr, make sure that the IP address of the instance from which you are running the code is whitelisted.
  • Private Deployments – For Private deployments, your code should be run from the instance that it is peered to the Solr deployment and has access to the Solr deployment.

Getting Started

import java.io.IOException;
import java.util.UUID;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;

String url = "https://xxxxxxxxx-central-1-aws.searchstax.com/solr";
String collectionName = "searchstax”
String config = "_default”
String username = "myusername”
String password = "mypassword”

Create SolrJ HTTP Client

//Create the SolrJ Http client
HttpSolrClient client = new HttpSolrClient.Builder(url).
 withConnectionTimeout(10000).
 withSocketTimeout(60000).
 build();

Create a Collection

//Create a collection
SolrRequest createRequest=CollectionAdminRequest.createCollection(collectionName, config, 1, 1);
request.setBasicAuthCredentials(username, password);
SolrResponse response = request.process(client, collectionName);

Add Documents to the Collection

//Add documents to the collection
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.setBasicAuthCredentials(username, password);
for(int i=0; i<5; i++) {
final SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", UUID.randomUUID().toString());
updateRequest.add(doc);
}
UpdateResponse response = request.process(client, collectionName);
updateRequest.commit(client, collectionName);

Query the Index and Get Results

//Query the index and get the results
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*:*");
solrQuery.set("fl","*");
solrQuery.setRows(10);

QueryRequest queryRequest = new QueryRequest(solrQuery);
queryRequest.setBasicAuthCredentials(username, password);
QueryResponse response =  queryRequest.process(client, collectionName);
System.out.println("Total Documents : " + response.getResults().getNumFound());
for(SolrDocument document : response.getResults()) {
       System.out.println("----------- DOCUMEMENT ---------");
       for(String name : document.getFieldNames()) {
           Object value = document.getFirstValue(name);
           System.out.println(name + ":" + value);
       }
}

 


If you have any further questions about connecting SolrJ to SearchStax, submit a support request from the application or contact your Customer Success team member to learn more.

LEARN MORE ABOUT SEARCHSTAX CLOUD