SearchStax Site Search solution is engineered to give marketers the agility they need to optimize site search outcomes. Get full visibility into search analytics and make real-time changes with one click.
SearchStax Managed Search service automates, manages and scales hosted Solr infrastructure in public or private clouds. Free up developers for value-added tasks and reduce costs with fewer incidents.
August 17, 2022
Dipsy Kapoor
|
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 Managed Search. It is the same great product that lets developers set up and deploy Solr infrastructure in minutes.
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.
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.
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.
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 the SolrJ Http client HttpSolrClient client = new HttpSolrClient.Builder(url). withConnectionTimeout(10000). withSocketTimeout(60000). build();
//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 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 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); } }
The Stack is delivered bi-monthly with industry trends, insights, products and more
Copyrights © SearchStax Inc.2014-2024. All Rights Reserved.