Hibernate Search Tutorial
Hibernate Search Getting started
Hibernate Search provides "full-text search" capabilities for your application. It depends on Apache Lucene and Hibernate ORM.Full-text search is a set of techniques for searching, in a corpus of text documents, the documents that best match a given query.
Hibernate Search relies on either Apache Lucene or Elasticsearch to implement full-text search. Since Elasticsearch uses Lucene internally, they share a lot of characteristics and their general approach to full-text search.
Steps to integrate Hibernate Search into your application:
Compatibility
Java Runtime : Java 8 or greater.Hibernate ORM (for the ORM mapper) :Hibernate ORM 5.4.12.Final.
JPA (for the ORM mapper) :JPA 2.2.
Dependencies
In order to use Hibernate Search, Below are the matching dependencies for a quick start.Configuration
Once you have added all required dependencies to your application you have to add a couple of properties to your Hibernate ORM configuration file.
- A hibernate.properties file in your classpath.
- The hibernate.cfg.xml file in your classpath, if using Hibernate ORM native bootstrapping.
- The persistence.xml file in your classpath, if using Hibernate ORM JPA bootstrapping.
Hibernate Search properties in persistence.xml for a "Hibernate ORM + Lucene" setup
<property name="hibernate.search.backends.myBackend.type"
value="lucene"/>
<!--
<property name="hibernate.search.backends.myBackend.directory.root"
value="some/filesystem/path"/>
-->
<property name="hibernate.search.default_backend"
value="myBackend"/>
Define a backend named "myBackend" relying on Lucene technology.
The backend will store indexes in the current working directory by default. If you want to store the indexes elsewhere, uncomment this line and set the value of the property.
Make sure to use the backend we just defined for all indexes.
Hibernate Search properties in persistence.xml for a "Hibernate ORM + Elasticsearch" setup
<property name="hibernate.search.backends.myBackend.type"
value="elasticsearch" />
<!--
<property name="hibernate.search.backends.myBackend.hosts"
value="elasticsearch.mycompany.com"/>
<property name="hibernate.search.backends.myBackend.protocol"
value="https"/>
<property name="hibernate.search.backends.myBackend.username"
value="ironman"/>
<property name="hibernate.search.backends.myBackend.password"
value="j@rV1s"/>
-->
<property name="hibernate.search.default_backend"
value="myBackend"/>
Define a backend named "myBackend" relying on Elasticsearch technology.
The backend will attempt to connect to http://localhost:9200 by default. If you want to connect to another URL, uncomment these lines and set the value for the "hosts" property, and optionally the username and password.
Make sure to use the backend we just defined for all indexes.
Configuring the mapping
Hibernate Search will automatically process mapping annotations for entity types, as well as nested types in those entity types, for instance embedded types. See Entity/index mapping and Mapping a property to an index field with @GenericField, @FullTextField, …to get started with annotation-based mapping.
If you want to ignore these annotations, set hibernate.search.mapping.process_annotations to false.
To configure the mapping manually, you can set a mapping configurer. By setting hibernate.search.mapping.configurer to a bean reference of type org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer, you can use a programmatic API to define the mapping.
Implementing a mapping configurer
public class MySearchMappingConfigurer implements HibernateOrmSearchMappingConfigurer {
@Override
public void configure(HibernateOrmMappingConfigurationContext context) {
ProgrammaticMappingConfigurationContext mapping = context.programmaticMapping();
mapping.type( Book.class )
.indexed()
.property( "title" ).fullTextField().analyzer( "english" );
}
}
Comments
Post a Comment