Hibernate Search运用
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文件中加入了一下内容:
- <propertyname"hibernate.search.default.directory_provider"></property><propertyname"hibernate.search.default.indexBase">??????????E:/temp/index ??
- </property>
- @Indexed"text"publicclassimplements{ ??
- ????@DocumentId????private????private????private????@Field"content"@Analyzerclassprivate??
- ????...... ??
- }??
其中@Indexed用于标示需要建立全文索引的实体类,它包含一个属性index用于标示这个全文索引的名字
@DocumentId用于标示实体类中的唯一的属性保存在索引文件中,是当进行全文检索时可以这个唯一的属性来区分索引中其他实体对象,一般使用实体类中的主键属性
@Field就是用来标示Lucene的Field字段,其中name属性用于标示Field的名称,store属性用于标示这个属性的内容是否需要保存在索引中,index属性标示该字段属性是否进行分词(Index.TOKENIZED- factory?=?new
- //获取Session//封装Session为FullTextSession??
- //开始事务Transaction?tx?=?fullTextSession.beginTransaction(); ??
- ??
- ...... ??
- ??
- //提交事务//关闭会话
- Session?session?=?HibernateUtil.getSession(); ??
- FullTextSession?fullTextSession?=?Search.createFullTextSession(session); ??
- ??
- Transaction?tx?=?fullTextSession.beginTransaction(); ??
- ??
- QueryParser?parser?=?new"content"new??
- Query?query?=?fullTextSession.createFullTextQuery(parser.parse(word), ??
- ????????Text.classList?result?=?query.list(); ??
- forint0null{ ??
- ????Text?pojo?=?(Text)?result.get(i); ??
- ????System.out.println("文件名:""文件路径:"????System.out.println(); ??
- } ??
- ??
- tx.commit(); ??
- fullTextSession.close();??
首先是建立相应的QueryParser由他来对输入的关键字进行切分后产生Lucene下的Query实例,最后通过 FullTextSession的createFullTextQuery方法生成hibernate下的Query实例,执行list方法即可获得查询 的实例结果集合。