Spring Data Solr Configuration using Spring Boot

Spring Data Solr ConfigurationIn 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