集成Hibernate Search做全文检索

集成Hibernate Search做全文检索 原文来自 http://blog.csdn.net/zhengwei223/article/details/11952763

版本及依赖:

 

[html]  view plain copy
 
  1. <dependency>  
  2.             <groupId>org.hibernate</groupId>  
  3.             <artifactId>hibernate-search-orm</artifactId>  
  4.             <version>4.2.0.Final</version>  
  5.         </dependency>  
[html]  view plain copy
 
  1. <dependency>  
  2. <span style="white-space:pre">          </span><groupId>org.apache.lucene</groupId>  
  3. <span style="white-space:pre">          </span><artifactId>lucene-smartcn</artifactId>  
  4. <span style="white-space:pre">          </span><version>3.6.2</version>  
  5. <span style="white-space:pre">      </span></dependency>  



 

1、修改hibernate主配置文件,增加:

 

[java]  view plain copy
 
  1. <property name="hibernate.search.default.directory_provider">  
  2.             org.hibernate.search.store.impl.FSDirectoryProvider  
  3.         </property>  
  4.         <property name="hibernate.search.default.indexBase">  
  5.             e:\luceneLinde  
  6.         </property>  


一个是存储的实现,一个是存储的路径

 

 

2、给实体类上注解

 

[java]  view plain copy
 
  1. import javax.persistence.*;  
  2.   
  3. import org.hibernate.annotations.GenericGenerator;  
  4.   
  5. import org.hibernate.search.annotations.DocumentId;  
  6. import org.hibernate.search.annotations.Field;  
  7. import org.hibernate.search.annotations.Indexed;  
  8. import org.hibernate.search.annotations.IndexedEmbedded;  
  9. import org.hibernate.search.annotations.Store;  
  10.   
  11.   
  12. @Entity  
  13. @Table(name = "PAGEINFO")  
  14. @Indexed(index="PageInfo")/*标记该表可索引,参数index指定存放索引信息的文件名,路径在主配置文件中指定*/  
[java]  view plain copy
 
  1. @Analyzer(impl=SmartChineseAnalyzer.class)//分词器  
  2. public class Pageinfo implements java.io.Serializable {  
  3.     private static final long serialVersionUID = 5454155825314635342L;  
  4.   
  5.   
  6.           
  7.     // columns START  
  8. //省略1000字  
  9.     // columns END  
  10.   
  11.     @Id  
  12.     @GeneratedValue(generator = "custom-id")  
  13.     @GenericGenerator(name = "custom-id", strategy = "uuid")  
  14.     @Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true, length = 32)  
  15.     @DocumentId  /*以字段id作为文档id*/  
  16.     public java.lang.String getId() {  
  17.         return this.id;  
  18.     }  
  19.   
  20.     @Column(name = "TITLE", unique = false, nullable = true, insertable = true, updatable = true, length = 255)  
  21.     @Field(store=Store.NO)  /*可索引,但不存储*/  
  22.     public java.lang.String getTitle() {  
  23.         return this.title;  
  24.     }  
  25.   
  26.     @Column(name = "CONTENT", unique = false, nullable = true, insertable = true, updatable = true)  
  27.     @Field(store=Store.NO)  
  28.     public java.lang.String getContent() {  
  29.         return this.content;  
  30.     }  
  31.   
  32.     @Column(name = "SOURCE", unique = false, nullable = true, insertable = true, updatable = true)  
  33.     @Field(store=Store.NO)  
  34.     public java.lang.String getSource() {  
  35.         return this.source;  
  36.     }  
  37.   
  38.   
  39.     @Column(name = "SUMMARY", unique = false, nullable = true, insertable = true, updatable = true)  
  40.     @Field(store=Store.NO)  
  41.     public java.lang.String getSummary() {  
  42.         return this.summary;  
  43.     }  
  44.   
  45.   
  46.     @ManyToOne(cascade = {}, fetch = FetchType.LAZY)  
  47.     @JoinColumns({ @JoinColumn(name = "SITE_ID", nullable = false, insertable = false, updatable = false) })  
  48.     @IndexedEmbedded(prefix="site_",depth=1)  /*关联检索,如field为site_name实则是按关联表的那么属性检索*/  
  49.     public GrabageSiteconfig getGrabageSiteconfig() {  
  50.         return grabageSiteconfig;  
  51.     }  
  52.   
  53. }  


省略了大量东西,如域成员,set方法等,一般以id作为doc的id,其他的你想对哪些字段做全文检索,就使用@Field标记,至于其store属性和lucene中的含义一致,不赘述。

 

 

3、使用API

 

[java]  view plain copy
 
  1. package othertest;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5.   
  6. import javacommon.gather.bean.Pageinfo;  
  7.   
  8. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  9. import org.apache.lucene.queryParser.ParseException;  
  10. import org.apache.lucene.queryParser.QueryParser;  
  11. import org.apache.lucene.search.Query;  
  12. import org.apache.lucene.util.Version;  
  13. import org.hibernate.Session;  
  14. import org.hibernate.SessionFactory;  
  15. import org.hibernate.Transaction;  
  16. import org.hibernate.search.FullTextQuery;  
  17. import org.hibernate.search.FullTextSession;  
  18. import org.hibernate.search.Search;  
  19. import org.junit.Before;  
  20. import org.junit.BeforeClass;  
  21. import org.junit.Test;  
  22.   
  23. public class SearchTest {  
  24.     private static SessionFactory sf;  
  25.       
  26.     @BeforeClass  
  27.     public static void init() {  
  28.         sf = HibernateConfigTest.sf;//弄一个SessionFactory,不多说  
  29.           
  30.     }  
  31.     @Before  
  32.     //执行索引  
  33.     public void index(){  
  34.         Session session = sf.openSession();  
  35.         FullTextSession fullTextSession = Search.getFullTextSession(session);  
  36.         //查出结果  
  37.         List<Pageinfo> pageinfos = session.createCriteria(Pageinfo.class).list();  
  38.         session.beginTransaction();  
  39.         //依次建立索引  
  40.         for (Iterator iterator = pageinfos.iterator(); iterator.hasNext();) {  
  41.             Pageinfo pageinfo = (Pageinfo) iterator.next();  
  42.             fullTextSession.index(pageinfo);  
  43.         }  
  44.         session.getTransaction().commit();  
  45.         session.close();  
  46.         System.out.println("index over......");  
  47.     }  
  48.   
  49.     @Test  
  50.     public void searchTest() {  
  51.         Session session = sf.openSession();  
  52.         FullTextSession fullTextSession = Search.getFullTextSession(session);  
  53.         //在字段content中检索  
  54.         QueryParser queryParser = new QueryParser(Version.LUCENE_36, "content"new SmartChineseAnalyzer(Version.LUCENE_36));  
  55.         Query luceneqQuery=null;  
  56.         try {  
  57.             //检索含有“大风”的信息  
  58.             luceneqQuery = queryParser.parse("大风");  
  59.         } catch (ParseException e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.         //执行检索,得到结果集  
  63.         FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneqQuery, Pageinfo.class);  
  64.         List<Pageinfo> pageinfos = fullTextQuery.list();  
  65.         //查看结果做验证  
  66.         for (Iterator iterator = pageinfos.iterator(); iterator.hasNext();) {  
  67.             Pageinfo pageinfo = (Pageinfo) iterator.next();  
  68.             System.out.println(pageinfo.getContent());  
  69.         }  
  70.     }  
  71. }  

猜你喜欢

转载自cnmqw.iteye.com/blog/2059372