Quick revision
The short answer
Hibernate Search maps JPA entities to documents in a full-text index using annotations such as @Indexed, @FullTextField and @KeywordField. It listens to Hibernate's entity change events, collects the affected documents during the transaction, and updates the index after the transaction commits, in an embedded Lucene index or a remote Elasticsearch or OpenSearch cluster. We query it with a search DSL that returns managed entities, and the mass indexer rebuilds the index from the database for the initial load or after mapping changes.
What to remember
In this answer, we saw that Hibernate Search maps entities to index documents with @Indexed, @FullTextField, @KeywordField and related annotations, listens to Hibernate entity events, and writes the affected documents to the index after the transaction commits. The index can be embedded Lucene or a remote Elasticsearch or OpenSearch cluster, queries use a lambda-based search DSL that returns managed entities, and the mass indexer rebuilds indexes from the database for the initial load or after mapping changes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Hibernate Search maps JPA entities to documents in a full-text index using annotations such as @Indexed, @FullTextField and @KeywordField. It listens to Hibernate's entity change events, collects the affected documents during the transaction, and updates the index after the transaction commits, in an embedded Lucene index or a remote Elasticsearch or OpenSearch cluster. We query it with a search DSL that returns managed entities, and the mass indexer rebuilds the index from the database for the initial load or after mapping changes.
Why this question is asked
Sooner or later every application gets a search box, and SQL like '%java%' quickly shows its limits: it is slow on large tables, ignores word forms, and cannot rank results by relevance. Full-text engines solve this, but keeping a separate index in sync with the database is the hard part. Hibernate Search exists to do that synchronization for JPA applications. Interviewers ask about it to see whether we understand both how documents are mapped and when the index is updated, because the timing explains its limits. In this answer, we will recap full-text indexing, map an entity, follow how changes reach the index, look at the backends, write a query, and see what the mass indexer is for.
1A Quick Recap of Full-Text Indexing
A full-text index is built for searching words rather than matching exact values. When a document is indexed, its text goes through an analyzer, which splits it into tokens, lowercases them, and often reduces words to a stem, so that indexing and indexed both become index. The index then maps each token to the documents that contain it, an inverted index. A search for 'Indexing Java' is analyzed the same way and finds documents containing those tokens, ranked by how well they match.
Lucene is the Java library that implements this, and Elasticsearch and OpenSearch are servers built on Lucene that run it as a distributed cluster.
2Mapping an Entity to a Document
Hibernate Search reads annotations on our entities to decide what goes into the index. @Indexed marks an entity that gets its own index. @FullTextField marks text that should be analyzed for word search. @KeywordField indexes a value as a single exact token, good for filtering, sorting and facets. @GenericField indexes numbers and dates, and @IndexedEmbedded pulls fields of an associated entity into the same document.
Notice @IndexedEmbedded. A search for books by author name works without a join, because the author's name is copied into each book's document. The cost is that changing an author's name must update all of that author's books in the index, which Hibernate Search does automatically by tracking that dependency.
3How Changes Reach the Index
Hibernate Search registers listeners on Hibernate's entity events. When we persist, update or delete an indexed entity, or an entity embedded in one, the listener records which documents are affected in an indexing plan for the current transaction. At flush it builds the new documents from the entity state. After the transaction commits successfully, it sends the changes to the index. If the transaction rolls back, the plan is discarded and the index is untouched.
Waiting for commit is the point: the index never shows data the database did not keep. But notice the consequence. The index is updated just after the commit, as a separate step, so for a short moment the database and the index disagree, and anything that changes the database without going through Hibernate entity events is invisible to Hibernate Search.
6 messages in order. Play them, or step through them one at a time.
The index is written only after the database commit succeeds.
4Choosing a Backend
Hibernate Search supports two kinds of backend. With the Lucene backend, the index lives in files on the application's own disk, embedded in the process. It needs no extra infrastructure, which suits a single instance or small deployments, but every instance of the application would have its own index unless we coordinate them. With the Elasticsearch backend, which also works with OpenSearch, the index lives in a separate cluster that all application instances share, which suits clustered deployments and larger data, at the cost of running that cluster.
The mapping annotations and the search DSL are the same for both, so switching backends is mostly configuration, although some analysis and query features differ, so check your version's documentation.
5Querying With the Search DSL
Queries go through a SearchSession, obtained from the EntityManager. The DSL builds the query with lambdas, and the result is a list of managed entities loaded from the database, so we can use them like any entity we loaded with JPA.
Notice the split of work. The index finds and ranks the matching ids, and Hibernate then loads those entities from the database. So results always reflect current entity state, even if the index is a moment behind, though a hit can still be missing or stale for that moment.
6Rebuilding With the Mass Indexer
Listeners only see changes that happen while the application runs. When we first add Hibernate Search to existing data, change a mapping such as adding a new indexed field or a different analyzer, or suspect the index has drifted, we rebuild it from the database with the mass indexer. It reads entities in parallel batches and writes their documents to the index.
On large tables this can take minutes or hours, so it typically runs as an admin task or at deployment for a mapping change, not on every start.
7Follow-up questions
Does Hibernate Search see changes from JPQL bulk updates?
No. Bulk JPQL and native SQL statements do not raise entity events, so the index is not updated. After such statements we reindex the affected entities explicitly or run the mass indexer for that type. The same applies to changes made by other applications directly in the database.
What happens if the index is down when a transaction commits?
The database commit has already happened, so the data is safe, but the default listener-based indexing fails to update the index and the change can be lost from it. For reliable indexing, Hibernate Search offers an outbox-polling coordination strategy that stores indexing events in a database table in the same transaction; check your version for details.
Why use @KeywordField instead of @FullTextField for an ISBN?
Because an ISBN must match exactly and be sortable. A full-text analyzer would split and transform it, which is useful for prose and harmful for identifiers, codes and statuses. Keyword fields keep the value as one token.
8Conclusion
In this answer, we saw that Hibernate Search maps entities to index documents with @Indexed, @FullTextField, @KeywordField and related annotations, listens to Hibernate entity events, and writes the affected documents to the index after the transaction commits. The index can be embedded Lucene or a remote Elasticsearch or OpenSearch cluster, queries use a lambda-based search DSL that returns managed entities, and the mass indexer rebuilds indexes from the database for the initial load or after mapping changes.
Open with
Hibernate Search connects JPA entities to a full-text index. I annotate an entity with @Indexed, text fields with @FullTextField so they go through an analyzer for word search, exact values like codes or ISBNs with @KeywordField for filtering and sorting, and I can pull associated data in with @IndexedEmbedded.
Then, if they want more
For synchronization it registers listeners on Hibernate's entity events. When an indexed entity, or one embedded in it, is persisted, updated or deleted, it adds the document to an indexing plan for the transaction, builds the document at flush, and writes it to the index after the transaction commits, so a rollback never reaches the index.
The backend can be embedded Lucene, files local to the application, or a remote Elasticsearch or OpenSearch cluster shared by all instances. Queries use the search DSL: Search.session(entityManager).search(Book.class).where(f -> f.match().field("title").matching("java")).fetchHits(20), and the results are managed entities loaded from the database. For existing data or after a mapping change, the mass indexer rebuilds the index from the database.
The limit to remember is that bulk updates and changes by other applications bypass the listeners.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does Hibernate Search do, and how does it keep a Lucene or Elasticsearch index in...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/feb8964a78/