The English version of quarkus.io is the official project site. Translated sites are community supported on a best-effort basis.

New extension: Hibernate Search + Elasticsearch

In the 0.15.0 version of Quarkus, we introduced the Hibernate Search + Elasticsearch extension and, with 0.16.0, we added a guide and a quickstart explaining how to use it.

If you want to index your Hibernate ORM entities in Elasticsearch and don’t mind updating your code from time to time, you’re very welcome to become an early adopter and provide feedback.

This is an old time favorite of mine. Back in the days, when I was developing business applications for customers, discovering Hibernate Search really changed the way we implemented search engines and our customers were very happy with the search features we provided from that moment on. I soon started to contribute to it and…​ here I am.

With an additional dependency and some configuration, you end up being able to provide full text search features to your users, just by synchronizing your Hibernate ORM entities to a full text index.

And when I say full text search, I mean the real one, backed by Apache Lucene or Elasticsearch.

Why is it better with Quarkus?

The nice thing with using Hibernate Search on Quarkus is that, when compiling a native application with GraalVM, we build the Hibernate Search metamodel at compilation time.

Expect your native application to start faster than ever!

How do I use it?

It can’t be easier.

First, add the quarkus-hibernate-search-elasticsearch extension to your Quarkus application:

./mvnw quarkus:add-extension -Dextension=hibernate-search-elasticsearch

(or you can directly add the io.quarkus:quarkus-hibernate-search-elasticsearch dependency to your pom.xml)

Then, take one of your Hibernate ORM entities and add a few annotations:

package org.acme.hibernate.search.elasticsearch.model;

import javax.persistence.Entity;

import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;

import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
@Indexed (1)
public class Book extends PanacheEntity {

    @FullTextField(analyzer = "standard") (2)
    public String title;
}
1 Declare your entity as part of the full text index
2 Add a full text field using the standard analyzer provided by Elasticsearch

Then add some additional configuration to your src/main/resources/application.properties:

quarkus.hibernate-search.elasticsearch.version=7
quarkus.hibernate-search.elasticsearch.automatic-indexing.synchronization-strategy=searchable
quarkus.hibernate-search.elasticsearch.index-defaults.lifecycle.strategy=drop-and-create
quarkus.hibernate-search.elasticsearch.index-defaults.lifecycle.required-status=yellow

I won’t give all the details here. You should just know that this is a good configuration for testing, but definitely not a good one for production!

And that’s it.

From now on, if you start an Elasticsearch cluster locally with the default port, your Book entities will be indexed in this cluster.

And to search for them (isn’t it the whole point of it?), we provide an easy to use DSL:

return Search.getSearchSession(em)
        .search(Book.class)
        .predicate(f ->
            f.simpleQueryString().onFields("title").matching(pattern)
        )
        .fetchHits();

さらに詳しく

This is just a small taste of what you can do with our Hibernate Search + Elasticsearch extension.

For instance, understanding the concept of text analysis is especially important for full text search and properly setting up analyzers depending on your requirements necessitates a bit more work.

You will find a lot more information about how you can set everything up properly in our extensive guide.