Basic configuration and use of hibernate search

Source: Internet
Author: User

{
Newhighlight (Event)
} ">

The runtime environment of hibernate search is as follows:

1. JDK or JRE 5.0 or above

2. hibernate-search and corresponding dependent packages

3. hibernate core 3.2.x

4. hibernate annotations 3.3.x

I. Configuration

Anyone who has used Lucene knows that Lucene uses directory to store index files, therefore, a initialized and configured factory class directoryprovider is provided in hibernate search to generate the corresponding directory. Here, I use the fsdirectoryprovider factory class, in which FS represents the file system, meaning that the index file is saved in the file system. Therefore, we added the following content to the hibernate. cfg. xml file:

XML Code
  1. <Property name = "hibernate. Search. Default. directory_provider">
  2. Org. hibernate. Search. Store. fsdirectoryprovider
  3. </Property>
  4. <Property name = "hibernate. Search. Default. indexbase">
  5. E:/temp/Index
  6. </Property>

The property hibernate. Search. Default. indexbase indicates the default storage location of the index file.

After setting these attributes, you can use annotation to configure the specified attributes of the specified pojo. As follows:

Java code
  1. @ Indexed (Index = "text ")
  2. PublicClassTextImplementsJava. Io. serializable
  3. {
  4. @ Entid
  5. PrivateInteger ID;
  6. PrivateString filename;
  7. PrivateString filepath;
  8. @ Field (name = "content", store = store. No, Index = index. tokenized, analyzer = @ analyzer (impl = chineseanalyzer.Class))
  9. PrivateString content;
  10. ......
  11. }

Here, @ indexed is used to identify the entity class for which full-text index is to be created. It contains an Attribute Index to indicate the name of the full-text index.

@ Entid indicates that the unique attribute in the object class is stored in the index file. This unique attribute can be used to distinguish other object objects in the index during full-text search, generally, the primary key attribute in the object class is used.

@ Field indicates the field of Lucene. The name attribute indicates the field name, and the store attribute indicates whether the content of this attribute needs to be stored in the index, the index Property indicates whether the field property is segmented (index. tokenized), analyzer is used to identify the analyzer class used for index creation. Here we use the built-in chineseanalyzer

2. Create an index

After completing the preceding settings, the configuration of hibernate search is complete. The rest is how to use hibernate search for encoding. In fact, the use of hibernate search is basically the same as that of hibernate. The index creation work can be automatically processed by hibernate search in the background without manual operations. The main differences are as follows:

1. Configuration

In this article, the hibernate search configuration is completed by annotation, so we should write this when initializing configuration, sessionfactory, and Session:

Java code
  1. Factory =NewAnnotationconfiguration (). Configure (file). buildsessionfactory ();

Use annotationconfiguaration to proxy commonly used Configuration

2. Session

To use the hibernate search function, you cannot simply use a normal session to start transactions and perform database operations. Instead, you should use fulltextsession instead.

Java code
  1. // Obtain the session
  2. Session session = hibernateutil. getsession ();
  3. // Encapsulate the session as fulltextsession
  4. Fulltextsession = search. createfulltextsession (session );
  5. // Start the transaction
  6. Transaction Tx = fulltextsession. begintransaction ();
  7. ......
  8. // Submit the transaction
  9. TX. Commit ();
  10. // Close the session
  11. Fulltextsession. Close ();
In this way, we use fulltextsession for save, update, and delete operations. hibernate search will automatically create a full-text index for the corresponding domain in the background based on the configuration.

Iii. Search

Next we will explain how to use the full-text search function to retrieve object objects.

Java code
  1. Session session = hibernateutil. getsession ();
  2. Fulltextsession = search. createfulltextsession (session );
  3. Transaction Tx = fulltextsession. begintransaction ();
  4. Queryparser parser =NewQueryparser ("content ",NewChineseanalyzer ());
  5. Query query = fulltextsession. createfulltextquery (parser. parse (Word ),
  6. Text.Class);
  7. List result = query. List ();
  8. For(IntI = 0; Result! =Null& I <result. Size (); I ++)
  9. {
  10. Text pojo = (text) result. Get (I );
  11. System. Out. println ("file name:" + pojo. getfilename ());
  12. System. Out. println ("file path:" + pojo. getfilepath ());
  13. System. Out. println ();
  14. }
  15. TX. Commit ();
  16. Fulltextsession. Close ();

The first step is to create a corresponding queryparser to split the input keywords and generate a query instance under Lucene. Finally, the query instance under Hibernate is generated through the createfulltextquery method of fulltextsession, execute the list method to obtain the queried instance result set.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.