Add Full-Text To Your Application With Hibernate Search: Project Setup
Add Full-Text To Your Application With Hibernate Search: Project Setup
Project setup
Add Hibernate Search to your project
The first thing you need to do, if you want to add Hibernate Search to
your project is to add the required libraries to your project. These are
the hibernate-search-orm.jar and if you want to use it with JPA also
the hibernate-entitymanager.jar.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
www.thoughts-on-java.org
Add full-text to your application with Hibernate Search
Configuration
You don’t need to provide any configuration when you start to use
Hibernate Search. The default values provide a good starting point
for most standard applications.
But I recommend to use the filesystem DirectoryProvider in the
beginning. It stores the Lucene indexes in the file system which
allows you to easily inspect them and get a better understanding of
your system. When you’re familiar with Hibernate Search and
Lucene, you should also have a look at the other supported
DirectoryProviders.
<persistence>
<persistence-unit name="my-persistence-unit">
...
<properties>
...
<property name =
"hibernate.search.default.directory_provider"
value="filesystem"/>
<property name =
"hibernate.search.default.indexBase"
value="./lucene/indexes"/>
</properties>
</persistence-unit>
</persistence>
www.thoughts-on-java.org
Add full-text to your application with Hibernate Search
Index entity attributes
Indexing one of your entities requires 2 things:
1. You need to annotate the entitiy with @Indexed to tell
Hibernate Search to index the entity.
2. You need to annotate the fields you want to index with the
@Field annotation. This annotation also allows you to define
how the attributes will be indexed. I will get into more detail
about that in one of the following blog posts.
@Indexed
@Entity
public class Tweet {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column
@Field
private String userName;
@Column
@Field
private String message;
…
}
www.thoughts-on-java.org
Add full-text to your application with Hibernate Search
Perform a simple full-text search
Similar to a search on Google, you can now use Hibernate Search to
do a full-text search on the messages of these tweets. The following
code snippet shows a query that searches for the words “validate”
and “Hibernate” in the messages of the tweets.
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
www.thoughts-on-java.org
Add full-text to your application with Hibernate Search
www.thoughts-on-java.org