Solr Search & Querying using Java Spring Boot Application
The success of any e-commerce web application depends on a fast and reliable search solution. Solr search is the best search solution in the market.
Let us explore Solr and integration with Spring Boot application which enables you to design an effective search feature for your website.
What is Solr?
Solr is an open source search platform by Apache Lucene and it is used to power the search and filtering features of many of the largest internet sites along the globe namely Apple, Netflix, Cisco,Chegg, NASA, Goldmansachs. It’s popularity is mainly due to its highly reliable and fault tolerant features.
Solr provides high maintenance by providing distributed indexing, replication, automated failure & recovery and very specific filter results to the search queries.
Starting Solr and integrating with website
- Prerequisites before starting Solr: latest JRE on the machine.
- Solr can be downloaded and started by unpacking and archiving to the directory of your choice.
- Starting Solr: Navigate to the Solr home folder through the terminal
bin/solr start(Unix based machines)bin\solr.cmd start(windows machines)bin\solr start(windows machines)
Furthermore, you can start Solr in cloud mode to get a more flexible distributed search with ZooKeeper integration for cluster configuration and maintenance & high availability
bin/solr start -e cloud
The above command starts the Solr in cloud mode with Solr’s internal Zookeeper to maintain the cloud nodes
External ZooKeeper with Solr
You can start Solr with external zookeeper by defining the zookeeper host while starting the Solr in the terminal
bin/solr start -e cloud -z <zookeeperIP>:<Port>
In addition you can start Solr with multiple zookeepers for high maintenance and fault tolerance by comma separating zookeepers as an ensemble.
bin/solr start -e cloud -z <zookeeperIP1>:<Port>,<zookeeperIP2>:<Port>,<zookeeperIP3>:<Port>
Working with example configsets(sample examples to test Solr before indexing a website):
bin/solr -e techproducts
Here, techproducts in one of the example configuration sets given by Solr to understand querying in Solr
Integrating Solr with Spring Boot
- Create a spring started project using Spring tool suite with dependencies
- Getting the fields from Solr index using @Field annotation:
Here, features and cat are multi valued field from Solr, hence we have declared the type to be a List of string
- Controller to connect to Solr at backend using solrj
SolrClient client = new HttpSolrClient
.Builder(“http://localhost:8983/solr/techproducts").build();
The SolrClient is a class of solrJ and it lets the user connect to Solr at backend with the IP address and port.
For a better understanding on how the controller maps with Solr and retrieves the search queries please visit: