<p>In this tutorial, I am going explain an example where I have used <strong><a href="https://dineshonjava.com/spring-boot-tutorial/">Spring Boot</a></strong> 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.</p>
<p>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&#8217;s see the following image has all modules of the Spring Data project of the Spring Framework.</p>
<p><img class="aligncenter wp-image-4115 size-full" src="https://dineshonjava.com/wp-content/uploads/2018/02/spring-data-solr.png" alt="Spring Data Solr" width="1120" height="628" /></p>
<h2>Spring Boot and Spring Data Solr Configuration Example</h2>
<p>Spring Boot provides a stater for the Spring Data Solr. You can include this starter in your Maven or Gradle configuration as following.</p>
<h3>In Maven build specification</h3>
<p>Following entry you have add into your pom.xml file.</p>
<pre class="highlight"><;dependency>; 
 <;groupId>;org.springframework.boot<;/groupId>; 
 <;artifactId>;spring-boot-starter-data-solr<;/artifactId>; 
 <;version>;1.5.9.RELEASE<;/version>; 
<;/dependency>; 
</pre>
<h3>In Gradle build specification</h3>
<p>Following entry you have to add into build.gradle file.</p>
<pre class="highlight">dependencies { 
 compile 'org.springframework.boot:spring-boot-starter-data-solr:1.5.9.RELEASE' 
}repositories { 
 maven { 
 url 'https://repo.spring.io/libs-release' 
 } 
} 
</pre>
<p>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.</p>
<p>In <em>application.properties</em> file, it look like this:</p>
<pre class="highlight">spring.data.solr.repositories.enabled=true 
</pre>
<p>In <em>application.yml</em> file, it look like this:</p>
<pre class="highlight">spring: 
	data: 
		solr: 
			repositories: 
					enabled: true 
 
</pre>
<p>The above configuration will enable the Spring Data Solr repository for your Spring application, let&#8217;s see in the next section to set the property for Solr host in your application.</p>
<h3>Providing Solr host configuration</h3>
<p>In Spring Boot application, you can provide Solr host in the application property file as following:</p>
<pre class="highlight">spring.data.solr.host=http://192.168.100.01/solr/ 
</pre>
<p>Same above configuration in the <em>application.yml</em> file look like this:</p>
<pre class="highlight">spring: 
	data: 
		solr: 
			host: http://192.168.100.01/solr/ 
</pre>
<p>Spring Boot also allow to customize the Spring Data Solr configuration according to your choice. Let&#8217;s see in the next section.</p>
<h2>Customizing Spring Data Solr</h2>
<p>You can customize the default Spring Data configuration in your Spring Boot application. Let&#8217;s see the following customized configuration of the Spring Data Solr in your application.</p>
<pre class="highlight">@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); 
 } 
} 
</pre>
<p>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.</p>
<p>Spring Data Solr provide several feature, you can see as below:</p>
<h2>Spring Data Solr Features</h2>
<p>Let&#8217;s see the following features:</p>
<ul>
<li>High level repository abstractions with multicore support</li>
<li>Annotations for Boost-, Facet- and Highlighting</li>
<li>Customizable type mappings and type conversions</li>
<li>Solr template supporting fluent query api</li>
<li>Exception translation to Spring’s portable Data Access exception hierarchy</li>
</ul>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput"><br />
<img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://i0.wp.com/dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern.jpg?resize=243%2C300&;ssl=1" alt="Spring-5-Design-Pattern" width="243" height="300" /></a></div>
</div>
<h2>Creating a Solr Document class</h2>
<p>Let&#8217;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.</p>
<pre class="highlight">/** 
 * 
 */ 
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 + "]"; 
	} 
	 
} 
 
</pre>
<p>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&#8217;s see how to create a Spring Data Solr repository file for your application.</p>
<h2>Creating a Solr Data Repository</h2>
<p>Let&#8217;s create a repository file to access the solr core in your application.</p>
<pre class="highlight">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); 
} 
</pre>
<p>You can create a usage class to use this repository as following:</p>
<pre class="highlight">..... 
..... 
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 
 } 
 } 
} 
 
.... 
..... 
</pre>
<p>If we use this repository method to query for contents whose description contains the string &#8220;why spring is popular&#8221; a snipplet might look like this:</p>
<pre class="highlight"><;highlight>;why spring is popular<;/highlight>; and used in the software development. 
 
</pre>
<h2>Source Code for this Example</h2>
<p>You can find source code for this example from the Git.</p>
<h3><a href="https://github.com/dineshonjava/SpringBootSpringDataSolrExample">SpringBootSpringDataSolrExample Source Code</a></h3>
<h2>Summary</h2>
<p>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.</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-boot-interview-questions-and-answers/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/mastering-spring-boot-2-0-and-spring-cloud/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4114'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…