Hibernate

Architecture of Hibernate Search

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p><b>Hibernate Search<&sol;b> brings the power of full text search engines to the persistence domain model by combining <b>Hibernate Core <&sol;b>with the capabilities of the<b> Apache Lucene <&sol;b>search engine&period;<&sol;p>&NewLine;<p><b>          Hibernate Search &equals; Hibernate Core &plus; Apache Lucene search engine <&sol;b><&sol;p>&NewLine;<p>Hibernate Search consists of an indexing component as well as an index search component&period; Both are backed by Apache Lucene&period;<&sol;p>&NewLine;<p><b>Hibernate Search i&period;e&period; Hibernate <&sol;b><b>indexing component &plus; <&sol;b><b>index search component <&sol;b><&sol;p>&NewLine;<p>Each time an entity is inserted&comma; updated or removed in&sol;from the database&comma; Hibernate Search keeps track of this event &lpar;through the Hibernate event system&rpar; and schedules an index update&period; All these updates are handled without you having to interact with the Apache Lucene APIs directly Instead&comma; the interaction with the underlying <b>Lucene <&sol;b>indexes is handled via so called <i><b> IndexManagers<&sol;b><&sol;i>&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p>Each Lucene index is managed by one index manager which is uniquely identified by name&period; In most cases there is also a one to one relationship between an indexed entity and a single <i><b> IndexManager<&sol;b><&sol;i>&period; The exceptions are the use cases of index sharding and index sharing&period; The former can be applied when the index for a single entity becomes too big and indexing operations are slowing down the application&period; In this case a single entity is indexed into multiple indexes each with its own index manager&period;<&sol;p>&NewLine;<p>Once the index is created&comma; you can search for entities and return lists of managed entities saving you the tedious object to Lucene Document mapping&period; The same persistence context is shared between Hibernate and Hibernate Search&period; As a matter of fact&comma; the <i><b> FullTextSession <&sol;b><&sol;i>is built on top of the Hibernate Session so that the application code can use the unified <i><b>org&period;hibernate&period;Query<&sol;b><&sol;i> or <i><b>javax&period;persistence&period;Query<&sol;b><&sol;i> APIs exactly the same way a HQL&comma; JPA-QL or native query would do&period;<&sol;p>&NewLine;<p><b>Back end Process-<&sol;b><br &sol;>&NewLine;Hibernate Search offers the ability to let the batched work being processed by different back ends&period; Several back ends are provided out of the box and you have the option to plugin your own&period; It is important to understand that in this context back end encompasses more than just the configuration option <i><b>hibernate&period;search&period;default&period;worker&period;backend<&sol;b><&sol;i>&period; This property just specifies a implementation of the <i><b>BackendQueueProcessor <&sol;b><&sol;i>interface which is a part of a back end configuration&period; In most cases&comma; however&comma; additional configuration settings are needed to successfully configure a specific backend setup&comma; like for example the JMS back end&period;<&sol;p>&NewLine;<p><b>Lucene-<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;lucene-backend&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>In the above mode&comma; all index update operations applied on a given node &lpar;JVM&rpar; will be executed to the Lucene directories &lpar;through the directory providers&rpar; by the same node&period; This mode is typically used in non clustered environment or in clustered environments where the directory store is shared&period;<&sol;p>&NewLine;<p>This mode targets non clustered applications&comma; or clustered applications where the Directory is taking care of the locking strategy&period;<&sol;p>&NewLine;<p><b>The main advantage is simplicity and immediate visibility of the changes in Lucene queries &lpar;a requirement in some applications&rpar;&period;<&sol;b><&sol;p>&NewLine;<p>An alternative back end viable for non-clustered and non-shared index configurations is the near-real-time backend&period;<&sol;p>&NewLine;<p><b>JMS-<&sol;b><br &sol;>&NewLine;All index update operations applied on a given node are sent to a JMS queue&period; A unique reader will then process the queue and update the master index&period; The master index is then replicated on a regular basis to the slave copies&period; This is known as the master&sol;slaves pattern&period; The master is the sole responsible for updating the Lucene index&period; The slaves can accept read as well as write operations&period; However&comma; they only process the read operation on their local index copy and delegate the update operations to the master&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;jms-backend&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>This mode targets clustered environments where throughput is critical&comma; and index update delays are affordable&period; Reliability is ensured by the JMS provider and by having the slaves working on a local copy of the index&period;<&sol;p>&NewLine;<p><b>Reader strategy-<&sol;b><br &sol;>&NewLine;When executing a query&comma; Hibernate Search interacts with the Apache Lucene indexes through a reader strategy&period; Choosing a reader strategy will depend on the profile of the application &lpar;frequent updates&comma; read mostly&comma; asynchronous index update etc&rpar;&period;<&sol;p>&NewLine;<p><b>shared-<&sol;b><&sol;p>&NewLine;<p>With this strategy&comma; Hibernate Search will share the same <i><b>IndexReader<&sol;b><&sol;i>&comma; for a given Lucene index&comma; across multiple queries and threads provided that the <i><b>IndexReader <&sol;b><&sol;i>is still up-to-date&period; If the IndexReader is not up-to-date&comma; a new one is opened and provided&period; Each <i><b>IndexReader <&sol;b><&sol;i>is made of several <i><b>SegmentReaders<&sol;b><&sol;i>&period; This strategy only reopens segments that have been modified or created after last opening and shares the already loaded segments from the previous instance&period; This strategy is the default&period;<&sol;p>&NewLine;<p>The name of this strategy is shared&period;<&sol;p>&NewLine;<p><b>not-shared-<&sol;b><&sol;p>&NewLine;<p>Every time a query is executed&comma; a Lucene <i><b>IndexReader <&sol;b><&sol;i>is opened&period; This strategy is not the most efficient since opening and warming up an <i><b>IndexReader <&sol;b><&sol;i>can be a relatively expensive operation&period;<&sol;p>&NewLine;<p>The name of this strategy is not-shared&period;<&sol;p>&NewLine;<p><b>Custom-<&sol;b><&sol;p>&NewLine;<p>You can write your own reader strategy that suits your application needs by implementing <i><b>org&period;hibernate&period;search&period;reader&period;ReaderProvider<&sol;b><&sol;i>&period; The implementation must be thread safe&period;<&sol;p>&NewLine;<p>Full text search engines like Apache Lucene are very powerful technologies to add efficient free text search capabilities to applications&period; However&comma; Lucene suffers several mismatches when dealing with object domain models&period; Amongst other things indexes have to be kept up to date and mismatches between index structure and domain model as well as query mismatches have to be avoided&period;<&sol;p>&NewLine;<p>Hibernate Search addresses these shortcomings&period; It indexes your domain model with the help of a few annotations&comma; takes care of database&sol;index synchronization and brings back regular managed objects from free text queries&period; Hence&comma; it solves&colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>The structural mismatch&colon;<&sol;b> Hibernate Search takes care of the object&sol;index translation<&sol;li>&NewLine;<li><b>The duplication mismatch&colon; <&sol;b>Hibernate Search manages the index&comma; keeps changes synchronized with your database&comma; and optimizes the index access transparently<&sol;li>&NewLine;<li><b>The API mismatch&colon;<&sol;b> Hibernate Search lets you query the index and retrieve managed objects as any regular Hibernate query would do&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Even though Hibernate Search is using Apache Lucene™ under the hood you can always fallback to the native Lucene APIs if the need arises&period;<&sol;p>&NewLine;<p>Depending on application needs&comma; Hibernate Search works well in non-clustered and clustered mode&comma; provides synchronous and asynchronous index updates&comma; allowing you to make an active choice between response&comma; throughput and index update time&period;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-search-api-hibernate-4-on&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-search-api-hibernate-4-on&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;configuration-for-hibernate-search&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-search-api-hibernate-4-on&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;configuration-for-hibernate-search&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '397'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago