Skip to main content

Custom implementation for a Spring Data Solr Repository

In this article we are going to write the our own implementation for Solr repository and add some custom methods to it, However I am going to cover every little details but you should have little bit idea of Apache Solr, Spring Boot Repositories to understand this article.

Apache Solr is basically an open source web application built on top of Apache lucene search API but Solr provides lots of extra functionalities than lucene. You don't need to be a Java programmer to perform search on Solr it provides a cool user interface with help of its REST-like APIs, We can do approx everything on this UI without having any knowledge of programming with little bit configuration. Rather than this Solr also provides advanced and optimised full text search, integration with Apache UIMA and Apache Tika and lots more you can see them official website of solr.

With initiations convention over configuration Spring introduced a new framework named Spring Boot, Spring Boot has a lots of advance thing and one of them is the concept of repositories. Spring boot Repositories have some methods which are commonly used while writing any enterprise apps.

Here we are going to use Spring boot Solr repository and provide some custom implementation for that repository.

First of all we need to setup a rough maven project structure, we can generate and download it from Spring Initializer or we can create it.

Now let's move our demo application, here I am going to create complete project from scratch in below simple steps.

Step 1. Create A maven project with Java 8 archtype






Step 2. Download, install and configure Apache Solr Http server, and create a core. I have created a core by name basic_core by using basic_configs.

Step 3. Add spring-boot-starter and spring-data-solr dependencies to your pom.xml file.

Step 4. Create below configuration classes and add solr.server.url=http://localhost:8983/solr/basic_core to application.properties file under resources folder.

CustomSolrRepoApp.java
Marking our class with @SpringBootApplication is same as marking the class with @Documented, @Inherited, @Configuration, @EnableAutoConfiguration, @ComponentScan  annotations

It is not general practice to write any code in this class rather than 

SpringApplication.run(CustomSolrRepoApp.class, args); 

But in this class I am getting the repository bean from application context and then creating dummy data with help of that repository instance and then calling findall() method of SolrRepository class and also our custom getLastNames() method which will give us unique list of lastnames present in our dummy data.

SolrConfig.java
Here we are creating beans for SolrServer and SolrTemplate, Here we are using HttpSolrServer to communicate with Apache solr, however we can also use EmbeddedSolrServerFactoryBean for same purpose. EmbeddedSolrServerFactoryBean is only good for dev server not production server.


Step 5. Create Employee model class

Employee.java
Mark id field with @Id and @Field annotation and other fields with @Field annotation

Step 6. Create repositories, custom repository and repository implementations

CustomSolrRepository.java
Define all your custom methods in this interface

EmployeeRepository.java
This class extends CustomSolrRepository and SolrCrudRepository interface here, This interface will be treated as Solr repository reference

EmployeeRepositoryImpl.java
This should be named as EmployeeRepository + impl according spring boot repository name convention. This class should also marked as @Repository annotation and it should also provide implementations for CustomSolrRepository's methods

To see the output we need to run CustomSolrRepoApp as java application as we can see in below screenshot first we will have the complete list of employees and then we will have list of unique last names.


At the end I want to say thanks for reading, Please feel free to contact me if you found anything wrong or phase any other problem, or if you just want to talk.

Comments

Post a Comment

Popular posts from this blog

Java Cloning - Copy Constructor versus Cloning

In my previous article Java Cloning and Types of Cloning (Shallow and Deep) in Details with Example , I have discussed Java Cloning in details and answered questions about how we can use cloning to copy objects in Java, what are two different types of cloning (Shallow & Deep) and how we can implement both of them, if you haven’t read it please go ahead. In order to implement cloning, we need configure our classes to follow below steps Implement Cloneable interface in our class or its superclass or interface, Define clone() method which should handle CloneNotSupportedException (either throw or log), And in most cases from our clone() method we call the clone() method of the superclass. And super.clone() will call its super.clone() and chain will continue until call will reach to clone() method of the Object class which will create a field by field mem copy of our object and return it back. Like everything Cloning also comes with its advantages and disadvantages. However, Java c...

Creating objects through Reflection in Java with Example

In Java, we generally create objects using the new keyword or we use some DI framework e.g. Spring to create an object which internally use Java Reflection API to do so. In this Article, we are going to study the reflective ways to create objects. There are two methods present in Reflection API which we can use to create objects Class.newInstance() → Inside java.lang package Constructor.newInstance() → Inside java.lang.reflect package However there are total 5 ways create objects in Java, if you are not aware of them please go through this article 5 Different ways to create objects in Java with Example . Both Class.newInstance() and java.lang.reflect.Constructor.newInstance() are known as reflective methods because these two uses reflection API to create the object. Both are not static and we can call earlier one on a class level object while latter one needs constructor level object which we can get by using the class level object. Class.newInstance() The Class class is th...

Everything About Object Oriented JavaScript

Complete explanation of Object Oriented JavaScript 01:50  JavaScript Objects 02:36  Objects in Objects 04:12  Constructor Functions 05:58  instanceof 06:28  Passing Objects to Functions 08:09  Prototypes 09:34  Adding Properties to Objects 10:44  List Properties in Objects 11:38  hasOwnProperty 12:42  Add Properties to Built in Objects 14:31  Private Properties 18:01  Getters / Setters 21:20  defineGetter / defineSetter 24:38  defineProperty 27:07  Constructor Function Getters / Setters 29:40  Inheritance 37:13  Intermediate Function Inheritance 39:14  Call Parent Functions 41:51  ECMAScript 6 47:31  Singleton Pattern 49:32  Factory Pattern 52:53  Decorator Pattern 54:52  Observer Pattern