Hibernate Search Example - Search with Hibernate Search
Now it is time to execute a first search. The general approach is to create a Lucene query (either via the Lucene API (Section 5.1.1, “Building a Lucene query using the Lucene API�) or via the Hibernate Search query DSL (Section 5.1.2, “Building a Lucene query with the Hibernate Search query DSL�)) and then wrap this query into a org.hibernate.Query in order to get all the functionality one is used to from the Hibernate API. The following code will prepare a query against the indexed fields, execute it and return a list of Books. Example 1.8. Using Hibernate Session to create and execute a search FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction tx = fullTextSession.beginTransaction(); // 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 = fullTextSession.getSearchFactory()     .buildQueryBuilder().forEntity( Book.class ).get(); org.apache.lucene.search.Query query = qb   .keyword()   .onFields("title", "subtitle", "authors.name", "publicationDate")   .matching("Java rocks!");   .createQuery(); // wrap Lucene query in a org.hibernate.Query org.hibernate.Query hibQuery =     fullTextSession.createFullTextQuery(query, Book.class); // execute search List result = hibQuery.list();   tx.commit(); session.close();