不久前Hibernate推出了Hibernate Search 3.0 
GA,由它的名字大家也可以大概猜到它的作用是对数据库中的数据进行检索的。它是hibernate对著名的全文检索系统Lucene的一个集成方案,作
用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate 
search就可以对这些字段进行全文检索后获得相应的POJO,从而加快了对内容庞大字段进行模糊搜索的速度(sql语句中like匹配)。
		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文件中加入了一下内容:
		xml 代码
		
				
						- 
								
										<
										property
										 
										name
										=
										"hibernate.search.default.directory_provider"
										>
										  
								
						
 
						- 
								          org.hibernate.search.store.FSDirectoryProvider   
						
 
						- 
								
										</
										property
										>
										  
								
						
 
						- 
								
										<
										property
										 
										name
										=
										"hibernate.search.default.indexBase"
										>
										  
								
						
 
						- 
								          E:/temp/index   
						
 
						- 
								
										</
										property
										>
										  
								
						
 
				
		 
		其中属性hibernate.search.default.indexBase代表索引文件默认的保存位置。
		这些属性设置完成后,接下来就是使用Annotation对指定POJO的指定属性进行配置了。如下:
		java 代码
		
				
						
								- 
										
												@Indexed
												(index = 
												"text"
												)   
										
								
 
								- 
										
												public
												 
												class
												 Text 
												implements
												 java.io.Serializable   
										
								
 
								- 
										{   
								
 
								- 
										    
										@DocumentId
										  
								
 
								- 
										    
										private
										 Integer id;   
								
 
								- 
										  
								
 
								- 
										    
										private
										 String fileName;   
								
 
								- 
										  
								
 
								- 
										    
										private
										 String filePath;   
								
 
								- 
										  
								
 
								- 
										    
										@Field
										(name = 
										"content"
										, store = Store.NO, index = Index.TOKENIZED, analyzer = 
										@Analyzer
										(impl = ChineseAnalyzer.
										class
										))   
								
 
								- 
										    
										private
										 String content;   
								
 
								- 
										  
								
 
								- 
										    ......   
								
 
								- 
										}  
								
 
						
				 
				其中@Indexed用于标示需要建立全文索引的实体类,它包含一个属性index用于标示这个全文索引的名字
				@DocumentId用于标示实体类中的唯一的属性保存在索引文件中,是当进行全文检索时可以这个唯一的属性来区分索引中其他实体对象,一般使用实体类中的主键属性
				@Field就是用来标示Lucene的Field字段,其中name属性用于标示Field的名称,store属性用于标示这个属性的内容是否需要保存在索引中,index属性标示该字段属性是否进行分词(Index.TOKENIZED),analyzer用于标示建立索引时所使用的分析器是什么类,这里使用Lucene自带的ChineseAnalyzer
				
						
				
				
						二、建立索引
						配置完成以上设置之后,Hibernate Search的配置工作算是大功告成了,剩下的就是如何在编码时使用到Hibernate 
Search。其实Hibernate Search的使用与我们平时Hibernate的使用基本一致,索引的建立工作是可以由Hibernate 
Search后台自动处理的,无需手工操作,其中的主要差别有
						1、Configuration
						由于本文中Hibernate Search配置是由Annotation来完成的,所以我们在初始化Configuration、SessionFactory、Session时应该这样写:
						java 代码
						
								
										
												- 
														
																factory = 
																new
																 AnnotationConfiguration().configure(file).buildSessionFactory();  
														
												
 
										
								 
								使用AnnotationConfiguaration来代理平常使用的Configuration
								2、Session
								要使用Hibernate Search的功能就不能单纯使用平常的Session来开始事务,进行数据库操作,而是应该改用FullTextSession
								java 代码
								
										
												
														- 
																
																		
																		  
																
														
 
														- 
																Session session = HibernateUtil.getSession();   
														
 
														- 
																
																		
																		  
																
														
 
														- 
																FullTextSession fullTextSession = Search.createFullTextSession(session);   
														
 
														- 
																  
														
 
														- 
																
																		
																		  
																
														
 
														- 
																Transaction tx = fullTextSession.beginTransaction();   
														
 
														- 
																  
														
 
														- 
																......   
														
 
														- 
																  
														
 
														- 
																
																		
																		  
																
														
 
														- 
																tx.commit();   
														
 
														- 
																
																		
																		  
																
														
 
														- 
																fullTextSession.close();  
														
 
												
										 
										这样,我们使用FullTextSession进行save,update,delete操作hibernate search将会自动根据配置在后台对相应的域建立全文索引了
										
												
										
										三、检索
										接下来就是说一下如何使用全文检索功能来检索实体对象了。
										java 代码
										
												
														- 
																
																		Session session = HibernateUtil.getSession();   
																
														
 
														- 
																FullTextSession fullTextSession = Search.createFullTextSession(session);   
														
 
														- 
																  
														
 
														- 
																Transaction tx = fullTextSession.beginTransaction();   
														
 
														- 
																  
														
 
														- 
																QueryParser parser = 
																new
																 QueryParser(
																"content"
																, 
																new
																 ChineseAnalyzer());   
														
 
														- 
																  
														
 
														- 
																Query query = fullTextSession.createFullTextQuery(parser.parse(word),   
														
 
														- 
																        Text.
																class
																);   
														
 
														- 
																  
														
 
														- 
																List result = query.list();   
														
 
														- 
																
																		for
																		 (
																		int
																		 i = 
																		0
																		; result != 
																		null
																		 && i < result.size(); i++)   
																
														
 
														- 
																{   
														
 
														- 
																    Text pojo = (Text) result.get(i);   
														
 
														- 
																    System.out.println(
																"文件名:"
																 + pojo.getFileName());   
														
 
														- 
																    System.out.println(
																"文件路径:"
																 + pojo.getFilePath());   
														
 
														- 
																    System.out.println();   
														
 
														- 
																}   
														
 
														- 
																  
														
 
														- 
																tx.commit();   
														
 
														- 
																fullTextSession.close();  
														
 
												
										 
										首先是建立相应的QueryParser由他来对输入的关键字进行切分后产生Lucene下的Query实例,最后通过
FullTextSession的createFullTextQuery方法生成hibernate下的Query实例,执行list方法即可获得查询
的实例结果集合。
										四、完