Spring Data Solr

Spring Data Solr Configuration using Spring Boot

In this article, we will explore more about the Spring Data Solr Configuration using Spring Boot Application. We will see how to customize Spring Data Solr configuration using @EnableSolrRepositories annotation in the Spring Boot Application.

In the previous article of Spring Data Solr tutorial, we have seen how to install and configure Solr and how to use Solr. The Solr provides a REST-like HTTP API to execute queries to the indexed data and also to add Solr index.

Spring Data Solr Configuration in Spring Boot Example

Spring Boot application provides auto-configuration for the Spring Data Solr by adding following Maven dependency:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>

The above maven dependency of the Spring Data Solr starter with groupId org.springframework.boot and artifactId spring-boot-starter-data-solr includes the Spring Solr Data into your Spring Boot application.

Same dependency configuration we can use configure with the Gradle as following:

dependencies {
       ...
	compile('org.springframework.boot:spring-boot-starter-data-solr')
       ...
}

The above maven dependency provide auto-configure Solr and you have to configure a property either in application.properties or in application.yml file. Let’s see the following configuration file:

application.properties

spring.data.solr.host=http://localhost:8983/solr/

Same configuration in the YML format as look like following:

application.yml

spring:
	data:
		solr:
			host: http://localhost:8983/solr/

Spring Data Solr Java Configuration

Till now we have seen default auto-configuration of the Spring Data Solr provided by the Spring Boot application. If you want to customize this configuration, Spring Boot also allows you to customize it by creating a configuration class following:

package com.doj.app.config;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;

/**
* @author Dinesh.Rajput
*
*/
@Configuration
@EnableSolrRepositories(
basePackages = "com.doj.app.repository",
namedQueriesLocation = "classpath:solr-named-queries.properties")
@ComponentScan
public class SolrConfig {

@Value("spring.data.solr.host") String solrURL;

@Bean
public SolrClient solrClient() {
return new HttpSolrClient.Builder(solrURL).build();
}

@Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
return new SolrTemplate(client);
}
}

In the above configuration file, we have used @EnableSolrRepositories annotation to scan the packages for repositories. This annotation enables Spring Data Solr repositories and configuring the root package of our Solr repositories. And also this annotation uses namedQueriesLocation attribute to provide location of configuration file for named queries.

In the configuration class, we have created a method called solrClient() and annotate this method with the @Bean annotation. The implementation of this method creates a new HttpSolrClient using Builder and passed the solrURL of hosted Solr Server.

In the configuration class, we have created a method called solrTemplate() and annotate this method with the @Bean annotation. The implementation of this method creates a new SolrTemplate object and passed the created SolrClient implementation as a constructor argument.

Enabling/Disabling Solr Repository in Spring Boot Application

Spring Boot also allows you to disable and enable Solr repository by setting following property either in application.properties or in application.yml file.

In application.properties

spring.data.solr.repositories.enabled=false

In application.yml

spring:
	data:
		solr:
			repositories:
				enabled: false

By default, this property set to true.

We have seen in this tutorial about basic of the Spring Data Solr configuration in the Spring Boot application.

I hope, this article have given better understanding about the Spring Data Solr Configuration in your Spring Boot Application. We have now successfully obtained the required dependencies with Maven and configured Spring Data Solr for your.

In the next article, we will see how to create Spring Data Solr Repositories.

Thanks for learning with the Dineshonjava.

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago