Spring Data Solr

Spring Data Solr Configuration using Spring Boot

<p><img class&equals;"alignright wp-image-4171 size-medium" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Spring-Boot-Solr-Example-300x93&period;jpg" alt&equals;"Spring Data Solr Configuration" width&equals;"300" height&equals;"93" &sol;>In this article&comma; we will explore more about the Spring Data Solr Configuration using <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-boot-tutorial&sol;">Spring Boot<&sol;a> <&sol;strong>Application&period; We will see how to customize Spring Data Solr configuration using <em>&commat;EnableSolrRepositories<&sol;em> annotation in the Spring Boot Application&period;<&sol;p>&NewLine;<p>In the <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;installing-configuring-apache-solr&sol;">previous article<&sol;a><&sol;strong> of <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-solr-tutorial&sol;">Spring Data Solr tutorial<&sol;a><&sol;strong>&comma; we have seen how to install and configure Solr and how to use Solr&period; The Solr provides a REST-like HTTP API to execute queries to the indexed data and also to add Solr index&period;<&sol;p>&NewLine;<div align&equals;"center"><iframe style&equals;"width&colon; 120px&semi; height&colon; 240px&semi;" src&equals;"&sol;&sol;ws-in&period;amazon-adsystem&period;com&sol;widgets&sol;q&quest;ServiceVersion&equals;20070822&amp&semi;OneJS&equals;1&amp&semi;Operation&equals;GetAdHtml&amp&semi;MarketPlace&equals;IN&amp&semi;source&equals;ac&amp&semi;ref&equals;qf&lowbar;sp&lowbar;asin&lowbar;til&amp&semi;ad&lowbar;type&equals;product&lowbar;link&amp&semi;tracking&lowbar;id&equals;dineshonjav06-21&amp&semi;marketplace&equals;amazon&amp&semi;region&equals;IN&amp&semi;placement&equals;1788299450&amp&semi;asins&equals;1788299450&amp&semi;linkId&equals;05b0146b0a85f4472697901e353dc276&amp&semi;show&lowbar;border&equals;true&amp&semi;link&lowbar;opens&lowbar;in&lowbar;new&lowbar;window&equals;true&amp&semi;price&lowbar;color&equals;333333&amp&semi;title&lowbar;color&equals;0066c0&amp&semi;bg&lowbar;color&equals;ffffff" frameborder&equals;"0" marginwidth&equals;"0" marginheight&equals;"0" scrolling&equals;"no"><br &sol;>&NewLine;<&sol;iframe><&sol;div>&NewLine;<h2>Spring Data Solr Configuration in Spring Boot Example<&sol;h2>&NewLine;<p>Spring Boot application provides auto-configuration for the Spring Data Solr by adding following <strong>Maven<&sol;strong> dependency&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;dependency&gt&semi; &NewLine;&lt&semi;groupId&gt&semi;org&period;springframework&period;boot&lt&semi;&sol;groupId&gt&semi; &NewLine;&lt&semi;artifactId&gt&semi;spring-boot-starter-data-solr&lt&semi;&sol;artifactId&gt&semi; &NewLine;&lt&semi;&sol;dependency&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>The above maven dependency of the Spring Data Solr starter with groupId <em>org&period;springframework&period;boot<&sol;em> and artifactId <em>spring-boot-starter-data-solr<&sol;em> includes the Spring Solr Data into your Spring Boot application&period;<&sol;p>&NewLine;<p>Same dependency configuration we can use configure with the <strong>Gradle<&sol;strong> as following&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">dependencies &lbrace; &NewLine; &period;&period;&period; &NewLine;&Tab;compile&lpar;'org&period;springframework&period;boot&colon;spring-boot-starter-data-solr'&rpar; &NewLine; &period;&period;&period; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>The above maven dependency provide auto-configure Solr and you have to configure a property either in <em>application&period;properties<&sol;em> or in <em>application&period;yml<&sol;em> file&period; Let&&num;8217&semi;s see the following configuration file&colon;<&sol;p>&NewLine;<p><strong>application&period;properties<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">spring&period;data&period;solr&period;host&equals;http&colon;&sol;&sol;localhost&colon;8983&sol;solr&sol; &NewLine;<&sol;pre>&NewLine;<p>Same configuration in the YML format as look like following&colon;<&sol;p>&NewLine;<p><strong>application&period;yml<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">spring&colon; &NewLine;&Tab;data&colon; &NewLine;&Tab;&Tab;solr&colon; &NewLine;&Tab;&Tab;&Tab;host&colon; http&colon;&sol;&sol;localhost&colon;8983&sol;solr&sol; &NewLine;<&sol;pre>&NewLine;<h3>Spring Data Solr Java Configuration<&sol;h3>&NewLine;<p>Till now we have seen default auto-configuration of the Spring Data Solr provided by the Spring Boot application&period; If you want to customize this configuration&comma; Spring Boot also allows you to customize it by creating a configuration class following&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;app&period;config&semi; &NewLine; &NewLine;import org&period;apache&period;solr&period;client&period;solrj&period;SolrClient&semi; &NewLine;import org&period;apache&period;solr&period;client&period;solrj&period;impl&period;HttpSolrClient&semi; &NewLine;import org&period;springframework&period;beans&period;factory&period;annotation&period;Value&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;Bean&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;ComponentScan&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;Configuration&semi; &NewLine;import org&period;springframework&period;data&period;solr&period;core&period;SolrTemplate&semi; &NewLine;import org&period;springframework&period;data&period;solr&period;repository&period;config&period;EnableSolrRepositories&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine;&ast; &commat;author Dinesh&period;Rajput &NewLine;&ast; &NewLine;&ast;&sol; &NewLine;&commat;Configuration &NewLine;&commat;EnableSolrRepositories&lpar; &NewLine;basePackages &equals; "com&period;doj&period;app&period;repository"&comma; &NewLine;namedQueriesLocation &equals; "classpath&colon;solr-named-queries&period;properties"&rpar; &NewLine;&commat;ComponentScan &NewLine;public class SolrConfig &lbrace; &NewLine; &NewLine;&commat;Value&lpar;"spring&period;data&period;solr&period;host"&rpar; String solrURL&semi; &NewLine; &NewLine;&commat;Bean &NewLine;public SolrClient solrClient&lpar;&rpar; &lbrace; &NewLine;return new HttpSolrClient&period;Builder&lpar;solrURL&rpar;&period;build&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine; &NewLine;&commat;Bean &NewLine;public SolrTemplate solrTemplate&lpar;SolrClient client&rpar; throws Exception &lbrace; &NewLine;return new SolrTemplate&lpar;client&rpar;&semi; &NewLine;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>In the above configuration file&comma; we have used <em>&commat;EnableSolrRepositories<&sol;em> annotation to scan the packages for repositories&period; This annotation enables Spring Data Solr repositories and configuring the root package of our Solr repositories&period; And also this annotation uses <em>namedQueriesLocation<&sol;em> attribute to provide location of configuration file for named queries&period;<&sol;p>&NewLine;<p>In the configuration class&comma; we have created a method called <em>solrClient&lpar;&rpar;<&sol;em> and annotate this method with the <em>&commat;Bean<&sol;em> annotation&period; The implementation of this method creates a new <em>HttpSolrClient<&sol;em> using Builder and passed the <em>solrURL<&sol;em> of hosted Solr Server&period;<&sol;p>&NewLine;<p>In the configuration class&comma; we have created a method called <em>solrTemplate&lpar;&rpar;<&sol;em> and annotate this method with the <em>&commat;Bean<&sol;em> annotation&period; The implementation of this method creates a new <em>SolrTemplate<&sol;em> object and passed the created <em>SolrClient<&sol;em> implementation as a constructor argument&period;<&sol;p>&NewLine;<h3>Enabling&sol;Disabling Solr Repository in Spring Boot Application<&sol;h3>&NewLine;<p>Spring Boot also allows you to disable and enable Solr repository by setting following property either in application&period;properties or in application&period;yml file&period;<&sol;p>&NewLine;<p><strong>In application&period;properties<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">spring&period;data&period;solr&period;repositories&period;enabled&equals;false &NewLine;<&sol;pre>&NewLine;<p><strong>In application&period;yml<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">spring&colon; &NewLine;&Tab;data&colon; &NewLine;&Tab;&Tab;solr&colon; &NewLine;&Tab;&Tab;&Tab;repositories&colon; &NewLine;&Tab;&Tab;&Tab;&Tab;enabled&colon; false &NewLine;<&sol;pre>&NewLine;<p>By default&comma; this property set to <em>true<&sol;em>&period;<&sol;p>&NewLine;<p>We have seen in this tutorial about basic of the Spring Data Solr configuration in the Spring Boot application&period;<&sol;p>&NewLine;<div align&equals;"center"><iframe style&equals;"width&colon; 120px&semi; height&colon; 240px&semi;" src&equals;"&sol;&sol;ws-in&period;amazon-adsystem&period;com&sol;widgets&sol;q&quest;ServiceVersion&equals;20070822&amp&semi;OneJS&equals;1&amp&semi;Operation&equals;GetAdHtml&amp&semi;MarketPlace&equals;IN&amp&semi;source&equals;ac&amp&semi;ref&equals;tf&lowbar;til&amp&semi;ad&lowbar;type&equals;product&lowbar;link&amp&semi;tracking&lowbar;id&equals;dineshonjav06-21&amp&semi;marketplace&equals;amazon&amp&semi;region&equals;IN&amp&semi;placement&equals;1787127567&amp&semi;asins&equals;1787127567&amp&semi;linkId&equals;a1e64f621b5e6128d8cb10e876eb1c48&amp&semi;show&lowbar;border&equals;true&amp&semi;link&lowbar;opens&lowbar;in&lowbar;new&lowbar;window&equals;true&amp&semi;price&lowbar;color&equals;333333&amp&semi;title&lowbar;color&equals;0066c0&amp&semi;bg&lowbar;color&equals;ffffff" frameborder&equals;"0" marginwidth&equals;"0" marginheight&equals;"0" scrolling&equals;"no" align&equals;"center"><span data-mce-type&equals;"bookmark" style&equals;"display&colon; inline-block&semi; width&colon; 0px&semi; overflow&colon; hidden&semi; line-height&colon; 0&semi;" class&equals;"mce&lowbar;SELRES&lowbar;start"><&sol;span><br &sol;>&NewLine;<&sol;iframe><&sol;div>&NewLine;<p>I hope&comma; this article have given better understanding about the Spring Data Solr Configuration in your Spring Boot Application&period; We have now successfully obtained the required dependencies with Maven and configured Spring Data Solr for your&period;<&sol;p>&NewLine;<p>In the next article&comma; we will see how to create Spring Data Solr Repositories&period;<&sol;p>&NewLine;<p>Thanks for learning with the <em><strong>Dineshonjava<&sol;strong><&sol;em>&period;<&sol;p>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;installing-configuring-apache-solr&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creating-spring-data-solr-repositories&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '4170'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago