Integrate Solr with Spring Boot
In this tutorial, I will show you how to improve your search by integrating Solr with Spring Boot. Spring-data-solr is the extension of spring data which used to integrate Solr with spring boot.
What is Solr:
Apache Solr is both a search engine and a distributed document database with SQL support. Solr is a search engine at heart, but it is much more than that. It is a NoSQL database with transactional support.
Steps:
- Configure Solr
- Create a Solr Document
- Create a Solr Repository
NOTE: Complete example for Solr REST API is provided in github project.
Configure Solr:
Download Solr:
You can download Solr here:
Basic Solr Commands:
Directory solr/solr-5.3.1Start Solr: ./bin/solr start
Stop Solr: ./bin/solr stop
Check Logs: tail -f server/logs/solr.logStart Solr on a different port: ./bin/solr start -p 2000
Run Solr:
cd solr/solr-5.3.1
./bin/solr start
Create Core:
./bin/solr create -c user_core
Integrate with Spring Boot Project
Add Dependency
Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
Gradle:
implementation('org.springframework.boot:spring-boot-starter-data-solr')
Create a Solr Document:
The Solr document is created by using @SolrDocument annotation and the core is defined in it.
@Indexed annotation is used for fields to make them searchable.
Create a Solr Repository:
Note: If Solr is running on a different Port(8983) add a property in the application.properties file:
spring.data.solr.host=http://127.0.0.1:3000/solr
For the complete API reference, you can check the project on Github.
If you have any questions or comments, feel free to comment below or message me on LinkedIn.
Before you go…
If you liked ❤ this article, please hit 👏 clap.
Also, to be notified about my new articles and stories, follow me on Medium and Twitter. You can find me on LinkedIn as well. Cheers!
Originally published at www.viithiisys.com on January 28, 2019.