Skip to main content
Viithiisys
Back to blog
Engineering1 min readMon, Jan 28, 2019

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.

Gaurav Saini

Founder, Viithiisys

Integrate Solr with Spring Boot

Apache Solr is a search engine and a distributed document database with SQL support. This guide integrates it with Spring Boot using spring-data-solr, covering the dependency to add, the host configuration, and how the repository connects.

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.1
Start Solr: ./bin/solr start
Stop Solr: ./bin/solr stop
Check Logs: tail -f server/logs/solr.log
Start 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 the spring-data-solr 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.

package com.anuragdhunna.solrIntegration.documents;

import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

/**
* @author anuragdhunna
*/

@SolrDocument(solrCoreName = "user_core")
public class UserDoc {

@Id
@Indexed
private String id;

@Indexed(name = "username", type = "string")
private String username;

@Indexed(name = "email", type = "string")
private String email;

@Indexed(name = "phone_number", type = "string")
private String phoneNumber;

// Getter Setters
}

Create a Solr Repository

package com.anuragdhunna.solrIntegration.solrRepositories;

import com.anuragdhunna.solrIntegration.documents.UserDoc;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* @author anuragdhunna
*/

@Repository
@Qualifier("userSolrRepo")
public interface UserSolrRepo extends SolrCrudRepository<UserDoc, String> {

    @Query(value = "*:*")
    List<UserDoc> getUsers();

}

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.

If you liked ❤ this article, please share.

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!