Search code examples
javahibernatedropwizard

Dropwizard Hibernate Search


I am busy with a Dropwizard application and would like to search my database for certain entities. I've seen this can be done by means of creating NamedQuery statements at the top of Entity classes as in this tutorial. The queries are then executed from the DAO classes.

I since stumbled across Hibernate search and it seems to be a better solution when it comes to searching for entities in the database. Therefore my question is if it is supported by the Dropwizard framework? Furthermore, if it is supported how would one configure and use it? I failed to find any reference to it in Dropwizard's hibernate documentation.


Solution

  • I managed to figure out how to utilize hibernate search in my Dropwizard application by trial and error. Turns out it is not directly supported by Dropwizard but can be added with a small amount of effort. I did the following:

    1.Added the Hibernate Search dependency in my pom file:

        <!-- Search -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-search-orm</artifactId>
            <version>5.9.0.Final</version>
        </dependency>
    

    2.In my application's run-method I created the indexer:

               // search functionality
            try {
                EntityManager em = hibernate.getSessionFactory().createEntityManager();
                FullTextEntityManager  fullTextEntityManager = Search.getFullTextEntityManager(em);
    
                fullTextEntityManager.createIndexer().startAndWait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    

    3.Made my Entity classes searchable with the @Indexed and @Field annotations

    @Entity
    @Indexed
    @Table(name = "product")
    public class Product {
    
        @Id
        private int id;
    
        @Field(termVector = TermVector.YES)
        private String productName;
    
        @Field(termVector = TermVector.YES)
        private String description;
    
        @Field
        private int memory;
    
        // getters, setters, and constructors
    } 
    

    4.Then in my DAO classes I could search the entity as follows:

    public List<Product> search(String term) {
            EntityManager em = this.currentSession();
    
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
    
            // create native Lucene query unsing the query DSL
            // alternatively you can write the Lucene query using the Lucene query parser
            // or the Lucene programmatic API. The Hibernate Search DSL is recommended though
            QueryBuilder qb = fullTextEntityManager.getSearchFactory()
                    .buildQueryBuilder()
                    .forEntity(Product.class)
                    .get();
    
            org.apache.lucene.search.Query luceneQuery = qb
                    .keyword()
                    .onFields("description",  "productName")
                    .matching(term)
                    .createQuery();
    
            // wrap Lucene query in a javax.persistence.Query
            javax.persistence.Query jpaQuery =
                    fullTextEntityManager.createFullTextQuery(luceneQuery, Product.class);
    
            // execute search
            List<Product> result = jpaQuery.getResultList();
    
            return result;
        }
    

    I found this tutorial very helpful during my implementation.