首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

Hibernate Search运用

2013-01-17 
Hibernate Search应用Hibernate Search基本配置和使用Hibernate Search运行的环境如下:1、JDK或JRE 5.0以上

Hibernate Search应用

Hibernate Search基本配置和使用

Hibernate Search运行的环境如下:

1、JDK或JRE 5.0以上

2、Hibernate-Search以及相应的依赖包

3、Hibernate Core 3.2.X

4、Hibernate Annotations 3.3.X

一、配置

使用过Lucene的人都知道,Lucene是使用Directory这个概念来存储索引文件的,所以在Hibernate Search中提供了一个初始化、配置化的工厂类DirectoryProvider来生成相应的Directory。而在这里,我使用了 FSDirectoryProvider这个工厂类,其中FS代表文件系统,意思是索引文件保存在文件系统中。因此,我们在hibernate.cfg.xml文件中加入了一下内容:

  1. <propertyname"hibernate.search.default.directory_provider"></property><propertyname"hibernate.search.default.indexBase">??????????E:/temp/index ??
  2. </property>
    1. @Indexed"text"publicclassimplements{ ??
    2. ????@DocumentId????private????private????private????@Field"content"@Analyzerclassprivate??
    3. ????...... ??
    4. }??

    其中@Indexed用于标示需要建立全文索引的实体类,它包含一个属性index用于标示这个全文索引的名字

    @DocumentId用于标示实体类中的唯一的属性保存在索引文件中,是当进行全文检索时可以这个唯一的属性来区分索引中其他实体对象,一般使用实体类中的主键属性

    @Field就是用来标示Lucene的Field字段,其中name属性用于标示Field的名称,store属性用于标示这个属性的内容是否需要保存在索引中,index属性标示该字段属性是否进行分词(Index.TOKENIZED
    1. factory?=?new
      1. //获取Session//封装Session为FullTextSession??
      2. //开始事务Transaction?tx?=?fullTextSession.beginTransaction(); ??
      3. ??
      4. ...... ??
      5. ??
      6. //提交事务//关闭会话
        1. Session?session?=?HibernateUtil.getSession(); ??
        2. FullTextSession?fullTextSession?=?Search.createFullTextSession(session); ??
        3. ??
        4. Transaction?tx?=?fullTextSession.beginTransaction(); ??
        5. ??
        6. QueryParser?parser?=?new"content"new??
        7. Query?query?=?fullTextSession.createFullTextQuery(parser.parse(word), ??
        8. ????????Text.classList?result?=?query.list(); ??
        9. forint0null{ ??
        10. ????Text?pojo?=?(Text)?result.get(i); ??
        11. ????System.out.println("文件名:""文件路径:"????System.out.println(); ??
        12. } ??
        13. ??
        14. tx.commit(); ??
        15. fullTextSession.close();??

        首先是建立相应的QueryParser由他来对输入的关键字进行切分后产生Lucene下的Query实例,最后通过 FullTextSession的createFullTextQuery方法生成hibernate下的Query实例,执行list方法即可获得查询 的实例结果集合。