Text Search With Hibernate Search And Spring Boot

Hibernate Search is a quick and easy way to add full text search to a java application that uses Spring. Spring Boot can be used very simply to demonstrate how quick it can be to get a text search up and running in a java application.

Lets create a web application running in Spring Boot to demonstrate this. Of course the text search part of this can be split out if required, this is here to make it easy to visualise.

Technologies

Heres what we are going to use

Project

Heres our basic setup for our eclipse project

Maven Pom

As usual I run the maven build in eclipse to grab all the relevant dependencies and setup the project using the goal

eclipse:clean eclipse:eclipse -DdownloadSources -DdownloadJavadocs

Model

Lets define our domain model.

By using Lombok we literally have to just mark this with @Data, Lombok will then automatically add the required getters and setters, toString() method, hashCode(), equals(), and the default no argument constructor… nice!

Also for Hibernate Search we need to tell it to index data in the class by marking it as @Indexed, and tell it which fields to index by marking them as @Field. There are additional options available within Hibernate Search for more control of its config for indexing the data, but we are using the defaults here for now.

Repository

By extending the Spring Data interfaces we get access to data access methods without writing our own implementation. By specifying interfaces in here we are able to use Spring Datas implementations automatically.

If we want to add additional methods for anything beyond those provided by Spring Data, we can create those in another class and extend that

So here we are using Spring Datas save implementation, but we want an additional method that we will specify in EmployeeRepositoryExt,

and we implement that in EmployeeRepositoryImpl.

Implementing The Hibernate Search

So here we are going to search on the keywords in the firstName and lastName fields for an exact match in either field

Initialising the Hibernate Search

We are also using a Spring Application Listener so that we can run the Hibernate Search indexing process once the application has started.

The Hibernate Search indexer will index the fields that we annotated in the Employee domain model class.

Spring MVC Controller

Lets create a controller to process the view form, trigger the search and return the reponse back to the form.

Thymeleaf UI

We will use a Thymeleaf template to implement the UI, to allow the search using a form and display the results.

and we will add some css to style that a little

CommandLineRunner

Lets implement a Spring CommandLineRunner to ensure we can populate some data in the embedded HSQL database (Spring Boot will have automatically configured the embedded database and started it, as it found the drivers on the classpath based on our POM.

SpringApplication

Lets implement a SpringApplication to startup our app.

We specify additional package locations for Spring, as we arent using the default options of having all our code in the same package as the SpringApplication.

If we run our SearchApplication now, it will start Spring Boot and then we should be able to go to http://localhost:8080/ and see our application.

if we enter some some search terms and search we should get results if they match. Search for Joe

We get a result as it matched. If we now search for Jo

We get nothing as it doesnt match.

Conclusion

Hope this helped to show you how easy it is to get started with Spring Boot and Hibernate Search. Of course there is a lot more to both, but its nice to get something up and running that you can then tweak, play with and improve on.

Leave a Comment

Your email address will not be published. Required fields are marked *