Chapter 6. Querying with Hibernate Search

 

This chapter covers

  • Creating and executing a Hibernate Search query
  • Using pagination
  • Using projection
  • Converting results in a different structure (ResultTransformer)
  • Defining a fetching strategy

Hibernate Search queries are the key component shielding your application from the mismatches between the index model and the domain model and helping you to migrate queries from SQL, HQL, or Criteria to full-text queries easily. Building a full-text query with Hibernate Search consists of two steps:

  1. Build a Lucene query to express the full-text search (either through the query parser or the programmatic API).
  2. Build a Hibernate Search query that wraps the Lucene query.

But first you need to understand why we need this wrapper around Lucene queries.

6.1. Understanding the query paradigm

You may wonder why we’d use Hibernate Search to execute our query rather than plain Lucene. After all, chapters 3, 4, and 5 showed how Hibernate Search helps us index data in a regular Lucene index. Let’s imagine that we use plain Lucene to find our data and explore the problems we’d face.

6.1.1. The burdens of using Lucene by hand

6.2. Building a Hibernate Search query

6.3. Executing the full-text query

6.4. Paginating through results and finding the total

6.5. Projection properties and metadata

6.6. Manipulating the result structure

6.7. Sorting results

6.8. Overriding fetching strategy

6.9. Understanding query results

6.10. Summary