
Solr 9 Support
SearchStax is excited to announce full Solr 9 support for Dedicated Deployments in all available Cloud regions
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.
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.
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); } }
SearchStax is excited to announce full Solr 9 support for Dedicated Deployments in all available Cloud regions
Integrating Solr with Drupal can significantly enhance the search capabilities of your website, but historically
SearchStax is happy to announce that we are continuing to see momentum in how users perceive value in our products
Copyrights © SearchStax Inc.2014-2023. All Rights Reserved.
close
close