`

集成Hibernate Search做全文检索

阅读更多

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

 

版本及依赖:

 

[html] view plaincopy
 
  1. <dependency>  
  2.             <groupId>org.hibernate</groupId>  
  3.             <artifactId>hibernate-search-orm</artifactId>  
  4.             <version>4.2.0.Final</version>  
  5.         </dependency>  
[html] view plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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. }  
分享到:
评论

相关推荐

    Hibernate搜索框架HibernateSearch.zip

    它是hibernate对著名的全文检索系统Lucene的一个集成方案,作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate search就可以对这些字段进行全文检索后获得相应的POJO,...

    Hibernate Search 3.0 GA

    它是hibernate对著名的全文检索系统Lucene的一个集成方案,作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate search就可以对这些字段进行全文检索后获得相应的POJO,...

    hibernate-search-5.3.0.Final.chm

    它是hibernate对著名的全文检索系统Lucene的一个集成方案,作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate search就可以对这些字段进行全文检索后获得相应的POJO,...

    harmonyos2-grails-hibernate-search-plugin:将HibernateSearch功能集成到Grails

    compile("org.grails.plugins:hibernate-search:2.3.0") compile("org.grails.plugins:hibernate5:6.1.8") compile("org.grails.plugins:cache") compile("org.hibernate:hibernate-core:5.2.10.Final") compile(...

    hibernate-search-infinispan-jms:JBoss EAP-带有Infinispan目录和JMS后端的Hibernate搜索

    Hibernate Search,JDG和JMS:演示JBoss EAP将Hibernate Search与Infinispan目录和JMS后端集成在一起作者:马特·罗布森(Matt Robson) 技术:JBoss EAP,JBoss数据网格,Hibernate Search,HornetQ 产品:JBoss ...

    jboss seam

    为轻量化的异步性集成了EJB Timer Service和Quartz,为工作流集成了jBPM,为业务规则集成了JBoss规则,为电子邮件集成了Meldware Mail,为完整的文本搜索集成了Hibernate Search和Lucene,为消息集成了JMS,以及为...

    LiveSearch:通过 db 中的链接使用 ajax 进行搜索 - spring mvc、hibernate 模型 + dao

    实时搜索通过 db 中的链接使用 ajax 进行搜索 - spring mvc、hibernate 模型 + dao 更新 - git bash update2 - git bash update3 - git eclipse 集成update4 - git bash update5 - Eclipse

    Java类库大全.docx

    Apache Commons:一个流行的Java类库,提供了许多实用的工具和组件,如Commons Lang(用于处理核心...它提供了许多用于处理数据库访问的工具和组件,如Hibernate Search(提供了搜索引擎功能)、Hibernate Validator

    Spring Boot带前后端 渐进式开发企业级博客系统

    本章节会将带领学员了解全文搜索的概念,并熟悉如何用ElasticSearch来实现全文搜索。 第8章 架构设计与分层 本章节讲解了系统的整体架构设计思路,包括如何来组织项目结构。让学员理解系统的数据流程。 第9章 ...

    SpringBoot企业级博客系统(未加密+源码)

    本章节会将带领学员了解全文搜索的概念,并熟悉如何用ElasticSearch来实现全文搜索。 第8章 架构设计与分层 本章节讲解了系统的整体架构设计思路,包括如何来组织项目结构。让学员理解系统的数据流程。 第9章 ...

    开源bbs源码java-BBS:Hiskey的论坛

    做数据检索,支持中文分词和结果关键字高亮 自己实现了一个Markdown编辑器, 附带菜单,书写方便, 还支持拖拽图片上传 待完成的功能 如果有你折腾过的,欢迎提pr,先谢过了:-) 权限修改后实现热更新,不用重新登录就...

    开源bbs源码java-NEW-BBS:新购物

    做数据检索,支持中文分词和结果关键字高亮 自己实现了一个Markdown编辑器, 附带菜单,书写方便, 还支持拖拽图片上传 待完成的功能 如果有你折腾过的,欢迎提pr,先谢过了:-) 权限修改后实现热更新,不用重新登录就...

    JBoss Seam 工作原理、seam和hibernate的范例、RESTFul的seam、seam-gen起步、seam组件、配置组件、jsf,jboss、标签、PDF、注解等等

    Seam - 语境相关的组件[满江红20071230]............................................................................................................................ 1 Java EE 框架...........................

    Lerx 网站内容管理系统 v5.5.zip

    支持信息分析、相似性检索技术,支持关键字检索、全文检索、组合检索等,并使关键字在搜索结果中进行高亮显示。文章发布后即生成相应的文件索件,避免过多查询数据库,引起服务器堵塞。 Lerx 网站内容管理系统截图 ...

    java开源包1

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包11

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包2

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包3

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包6

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

    java开源包5

    AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...

Global site tag (gtag.js) - Google Analytics