Spring Boot + Spring Data Solr Configuration Example

In this tutorial, I am going explain an example where I have used Spring Boot and Spring Data Solr. This example of the Spring Boot will use Spring Data Solr to access the Solr document hosted at the Solr Cluster.

As we know that Spring Data is an umbrella project of the Spring Community, they provide several implementations of the Spring Data such Spring Data JPA, Spring Data MongoDB, Spring Data Cassandra, Spring Data Solr etc. Let’s see the following image has all modules of the Spring Data project of the Spring Framework.

Spring Data Solr

Spring Boot and Spring Data Solr Configuration Example

Spring Boot provides a stater for the Spring Data Solr. You can include this starter in your Maven or Gradle configuration as following.

In Maven build specification

Following entry you have add into your pom.xml file.

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

In Gradle build specification

Following entry you have to add into build.gradle file.

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-data-solr:1.5.9.RELEASE'
}repositories {
    maven {
        url 'https://repo.spring.io/libs-release'
    }
}

This configuration will include Spring Data Solr in your application. By default, Spring Data Solr is enabled, so no need to enable it. But you could use following Spring boot property to enable or disable Spring Data solr in your application.

In application.properties file, it look like this:

spring.data.solr.repositories.enabled=true

In application.yml file, it look like this:

spring:
	data:
		solr:
			repositories:
					enabled: true

The above configuration will enable the Spring Data Solr repository for your Spring application, let’s see in the next section to set the property for Solr host in your application.

Providing Solr host configuration

In Spring Boot application, you can provide Solr host in the application property file as following:

spring.data.solr.host=http://192.168.100.01/solr/

Same above configuration in the application.yml file look like this:

spring:
	data:
		solr:
			host: http://192.168.100.01/solr/

Spring Boot also allow to customize the Spring Data Solr configuration according to your choice. Let’s see in the next section.

Customizing Spring Data Solr

You can customize the default Spring Data configuration in your Spring Boot application. Let’s see the following customized configuration of the Spring Data Solr in your application.

@Configuration
@EnableSolrRepositories(basePackages={"com.doj.app.solr"}, multicoreSupport=true)
public class SolrConfig {

  static final String SOLR_HOST = "solr.host";

  @Resource private Environment environment;

  @Bean
  public SolrServer solrServer() {
    String solrHost = environment.getRequiredProperty(SOLR_HOST);
    return new HttpSolrServer(solrHost);
  }
}

In the above configuration code, you can see that we have used @EnableSolrRepositories annotation to enable the Solr based repository of the Spring Data in the package com.doj.app.solr of your application. This configuration has one bean method solrServer(), that is responsible for creating SolrServer bean using given host address.

Spring Data Solr provide several feature, you can see as below:

Spring Data Solr Features

Let’s see the following features:

  • High level repository abstractions with multicore support
  • Annotations for Boost-, Facet- and Highlighting
  • Customizable type mappings and type conversions
  • Solr template supporting fluent query api
  • Exception translation to Spring’s portable Data Access exception hierarchy

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.

Creating a Solr Document class

Let’s create a Solr document class for your application. We can use this document for solr indexing. Here I am creating simple POJO class and adding Solrj annotations to it.

/**
 * 
 */
package com.doj.app.pojo;

import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;

/**
 * @author Dinesh.Rajput
 *
 */
@SolrDocument(solrCoreName="content")
public class Content {
	
	@Field
	Long id;
	@Field
	String heading;
	@Field
	String description;
	@Field
	String priority;
	@Field
	String city;
	@Field
	String locality;
	@Field
	String contenturl;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getHeading() {
		return heading;
	}
	public void setHeading(String heading) {
		this.heading = heading;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getPriority() {
		return priority;
	}
	public void setPriority(String priority) {
		this.priority = priority;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getLocality() {
		return locality;
	}
	public void setLocality(String locality) {
		this.locality = locality;
	}
	public String getContenturl() {
		return contenturl;
	}
	public void setContenturl(String contenturl) {
		this.contenturl = contenturl;
	}
	@Override
	public String toString() {
		return "Content [id=" + id + ", heading=" + heading + ", description=" + description + ", priority=" + priority
				+ ", city=" + city + ", locality=" + locality + ", contenturl=" + contenturl + "]";
	}
	
}

You can see in the above code, id is an unique document identifier and also each field of POJO is annotated with @Field annotation. By default Solrj tries to map document field names to Solr fields of the same name. Let’s see how to create a Spring Data Solr repository file for your application.

Creating a Solr Data Repository

Let’s create a repository file to access the solr core in your application.

package com.doj.app.repository;

import org.springframework.data.solr.repository.SolrCrudRepository;

import com.com.app.pojo.Content;

/**
* @author Dinesh.Rajput
*
*/
public interface SolrContentRepository extends SolrCrudRepository<Content, String>{

Page<Content> findByPriority(Integer priority, Pageable page);

Page<Content> findByHeadingOrDescription(@Boost(2) String heading, String description, Pageable page);

@Highlight(prefix = "<highlight>", postfix = "</highlight>")
HighlightPage<Content> findByCityIn(Collection<String> city, Page page);

@Query(value = "name:?0")
@Facet(fields = { "cat" }, limit=20)
FacetPage<Content> findByLocalityAndFacetOnCity(String locality, Pageable page);
}

You can create a usage class to use this repository as following:

.....
.....
HighlightPage highlightedContents = solrContentRepository.findByCityIn(city, new PageRequest(0, 10));
highlightedContents.getContent();
for (HighlightEntry content : highlightedContents.getHighlighted()) {
    for (Highlight highlight : content.getHighlights()) {
   
		for (String snipplet : highlight.getSnipplets()) {
		// snipplet contains the highlighted text
    }
  }
}

....
.....

If we use this repository method to query for contents whose description contains the string “why spring is popular” a snipplet might look like this:

<highlight>why spring is popular</highlight> and used in the software development.

Source Code for this Example

You can find source code for this example from the Git.

SpringBootSpringDataSolrExample Source Code

Summary

We have seen in this article, Spring Data Solr provides a very simple way to integrate Solr into Spring applications. Spring Boot provides you auto configuration for the Spring Data Solr by adding starter spring-boot-starter-data-solr. And also we have seen that how to customize the auto configuration the Spring Boot application. Also added the required dependencies with Maven and Gradle specification.

Previous
Next