Beginner Hibernate Search
To make your app capable of full-text search with Hibernate search, here are three things to do:
- Add necessary dependencies and configuration information to the project
- Add the necessary information to your entity classes so that Lucene knows how to index them (indexing)
- Use the Hibernate search specification query to complete the business logic where needed
The dependency information that needs to be added is described later. Let's start by looking at how the code should be written. We'll use a web app like the classic Java Pet Store to show how Hibernate search is used, and the main function of this app is to showcase a directory of software applications.
Create an entity class
Since the goal is to show the software application, the first entity class is the app class, and now we define it as follows, with 4 fields:
- ID: Primary Key information
- Name:app's name.
- Introduction of Description:app
- Links to Image:app pictures
@Entity Public class App{@Id @GeneratedValue Private LongId@Column Private StringName@Column(length= +)Private StringDescription@Column Private StringImage Public App() {} Public App(String name,String Image,String Description) { This.Name=Name This.Image=Image This.Description=Description }//Omit a multitude of getter and setter}
To modify an entity class to use Hibernate search
With the entity type, we need to tell hibernate search how to manage the entity with Lucene.
In the most basic scenario, we only need to add two annotations to the entity type:
The first is to add @indexed annotations:
import org.hibernate.search.annotations.indexed ; @entity @Indexed public class app implements serializable //...
This annotation tells Lucene to create an index for the app entity type. Note that this annotation is not required for each entity type, only that the entity class that will be the target of the search needs to be used.
Second, you need to add @field annotations to the specific fields:
Import Org.hibernate.search.annotations.Field;// ...@Id@GeneratedvaluePrivate Longid;@Column@FieldPrivate StringName; @Column (length= +)@FieldPrivate Stringdescription;@ColumnPrivate StringImage// ...
Here we add @field annotations to the name and description fields, indicating that both fields will be the target fields for the search. Also note that the image field is not @field labeled because we do not need to use the name of the image as a searchable field.
Create a query
After adding the necessary annotations to the entity classes, we can set up queries on them. The fulltextsession and QueryBuilder types are used primarily:
Import org.hibernate.Session;Import org.hibernate.search.FullTextSession;Import Org.hibernate.search.Search;// ...SessionSession= Startupdataloader.Opensession ();fulltextsessionFulltextsession= Search.Getfulltextsession (session); fulltextsession.BeginTransaction ();// ...
First set up a session and start a transaction. Next, you need to set up the query by passing in the keyword (Keyword):
Import Org.hibernate.search.query.dsl.QueryBuilder;// ...StringSearchString=Request.GetParameter ("SearchString");QueryBuilderQueryBuilder=Fulltextsession.Getsearchfactory (). Buildquerybuilder ().Forentity (App.Class).Get ();org.apache.lucene.search. QueryLucenequery=QueryBuilder. Keyword (). Onfields ("Name","description"). Matching (searchstring). CreateQuery ();
Very intuitive code, forentity is used to specify which entity to query, and Onfields to specify which fields to query. Translating the above code into a language that is easier to understand is this:
Create a keyword-based query that matches the searchstring parameter for the name and description fields of the app entity.
This is because the API is designed to be smooth, and it is also known as the Hibernate Search DSL (domain-specific Language). Also, notice that the query type created by the QueryBuilder object above is org.apache.lucene.search.Query. This is a way for hibernate search to connect with Lucene. After Lucene obtains the search results, the result is similarly converted to a Org.hibernate.Query object:
org.hibernate. QueryHibernatequery=Fulltextsession.Createfulltextquery (Lucenequery,App.Class);list<App>Apps=Hibernatequery.List (); request.SetAttribute ("Apps", apps);
As a result, hibernate search encapsulates a large amount of lucene usage details so that only developers who understand hibernate can easily add full-text search functionality to the app.
Create a project and import hibernate Search
Here we consider the dependencies that need to be added when using MAVEN, and the key is:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-search</artifactId> <version>4.2.0.Final</version></dependency>
for test environments, you can often also take advantage of in-memory database H2:
<dependency> <groupId>com.h2database</groupId> <artifactid>h2</ Artifactid> <version>1.3.168</version></dependency><dependency> <groupId> Commons-dbcp</groupid> <artifactId>commons-dbcp</artifactId> <version>1.4</version ></dependency>
Of course, the specific version number will vary depending on your needs.
[Hibernate search] initial hibernate search