Featured Image with Sidebar

Add full-text search to your application with Hibernate Search

By Thorben Janssen

Query

Full-text search has become a common requirement for modern enterprise applications, and there are several good implementations available, like Apache Lucene and Elasticsearch. They provide powerful indexing and search capabilities that allow you to easily add full-text search capabilities to your application.

But one important questions remains when you decide to add Apache Lucene or Elasticsearch to your application: How do you keep the indexes in sync with of your database?

You need to update the index every time you create, update or delete an indexed entity. Doing that programmatically is a tedious and error-prone task.

Hibernate Search provides an easier solution. It integrates with Hibernate ORM, updates the Lucene and Elasticsearch indexes transparently and provides a query DSL for full-text queries.

Let’s have a look at what you need to do to add Hibernate Search to your project and to perform your first full-text search. I will show you some of the more advanced features in future blog posts.

Project setup

Add Hibernate Search to your project

The first thing you need to do, if you want to add Hibernate Search to your project is to add the required library to your project. That is the hibernate-search-orm.jar.

I’m using Hibernate Search 5.6.0.Final for this example which requires Hibernate ORM 5.1.3.Final. If you want to use the latest Hibernate ORM release (5.2.7), you can do that with Hibernate Search 5.7.0.CR1.

Configuration

You don’t need to provide any configuration when you start to use Hibernate Search. The default values provide a good starting point for most standard applications.

I recommend using the filesystem DirectoryProvider in the beginning, which is also used by default. It stores the Lucene indexes in the file system which allows you to easily inspect them and get a better understanding of your system. When you’re familiar with Hibernate Search and Lucene, you should also have a look at the other supported DirectoryProviders.

You can configure the filesystem DirectoryProvider with 2 configuration parameter which you can provide in the persistence.xml file. You can set the default DirectoryProvider to filesystem with the configuration parameter hibernate.search.default.directory_provider and you can provide the base directory of the index with the hibernate.search.default.indexBase parameter.


Index entity attributes

Indexing one of your entities requires 2 things:

  1. You need to annotate the entitiy with @Indexed to tell Hibernate Search to index the entity.
  2. You need to annotate the fields you want to index with the @Field annotation. This annotation also allows you to define how the attributes will be indexed. I will get into more detail about that in one of the following blog posts.

Let’s start with a simple example. The following code snippet shows the simple Tweet entity. It persists the date, user, message and URL of a tweet and I want to be able to search for the userName and the message. I, therefore, annotate both attributes with Hibernate Search’s @Field annotation. That tells Hibernate Search to index the both attributes in Lucene and use the primary key attribute id as the identifier.

That’s all you need to do to add an Entity to the Lucene index. You can now use the userName and the message attribute in a full-text search query.

But before you can do that, you might need to create the initial index based on the data already stored in your database.

Create the initial index

Hibernate Search manages the Lucene index and keeps it in sync when you change indexed entities. That’s great when you start with an empty database. But most often, that’s not the case. If you’re working with an existing database, you need to add the existing records to your Lucene index.

You can do that with a few lines of code and Hibernate Search’s batch indexer.

Perform a simple full-text search

The entity attributes you annotated with @Field are now indexed, and you can use them in a full-text search. I created a small test database with 3 of my recent tweets. Each of them promotes a different blog posts.

tweets

Similar to a search on Google, you can now use Hibernate Search to do a full-text search on the messages of these tweets. The following code snippet shows a query that searches for the words “validate” and “Hibernate” in the messages of the tweets.

In the first step, you need to get a FullTextEntityManager. It extends the EntityManager interface with full-text search capabilities and allows you to create a QueryBuilder for the entity class you’re searching. In this example, I create a QueryBuilder for my Tweet entity. You then use the QueryBuilder to define your query. I want to do a keyword search on the message field. That searches the index with message attributes for one or more words. In this case, I’m searching for the words “validate” and “Hibernate”. Then I create a query and provide it to the createFullTextQuery method. This method returns a FullTextQuery interface which extends JPA’s Query interface. And then I call the getResultList method to execute the query and get a List of results.

This query returns the primary keys of 2 Tweets and Hibernate ORM uses them to select the Tweet entities from the database.

You might be surprised that the query returned 2 Tweets because one of them doesn’t contain the word “validate”. Similar to a Google search, Lucene also returns documents that contain only one of the search terms. But as you can see in the log output, the Tweet with the message “How to automatically validate entities with Hibernate Validator BeanValidation” received the better ranking because it contained both search terms.

This example showed just a very small part of the query capabilities of Lucene and Hibernate Search. I will dive deeper into this topic in a future blog post.

The last thing I want to talk about in this post is one of the big advantages of Hibernate Search: What do you need to do to keep the full-text search index in sync with the database.

Keep the index in sync

There is nothing you need to do to keep the Lucene index in sync with your database as long as you use Hibernate ORM to perform the create, update or remove operation. The following code snippet shows an example searching and updating an entity in 3 independent transactions. Hibernate Search updates the Lucene index when the EntityManager updates the Tweet entity in the 2nd transaction, and the query in the 3rd transaction finds the changed entity.

Summary

Hibernate Search integrates the full-text search capabilities of Lucene and Elasticsearch with your Hibernate ORM entities. It transparently updates the indexes everytime you create, update or delete an indexed entity and provides a powerful query DSL to define full-text queries.

You can do a lot more with Hibernate Search than I was able to show you in this blog post. I will show you how to perform more complex full-text queries in next weeks blog posts.

About the author

Thorben is an independent consultant, international speaker, and trainer specialized in solving Java persistence problems with JPA and Hibernate.
He is also the author of Amazon’s bestselling book Hibernate Tips - More than 70 solutions to common Hibernate problems.

Books and Courses

Coaching and Consulting

Leave a Repl​​​​​y

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}