hibernate search

 一。配置

 

Xml代码   收藏代码
  1. <dependency>  
  2.             <groupId>org.hibernate</groupId>  
  3.             <artifactId>hibernate-core</artifactId>  
  4.             <version>${hibernate.version}</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.hibernate</groupId>  
  8.             <artifactId>hibernate-entitymanager</artifactId>  
  9.             <version>${hibernate.version}</version>  
  10.         </dependency>  
  11.   
  12.         <dependency>  
  13.             <groupId>org.hibernate</groupId>  
  14.             <artifactId>hibernate-search</artifactId>  
  15.             <version>${hibernate.version}</version>  
  16.         </dependency>  
  17.         <dependency>  
  18.             <groupId>org.hibernate</groupId>  
  19.             <artifactId>hibernate-search-analyzers</artifactId>  
  20.             <version>${hibernate.version}</version>  
  21.         </dependency>  

 

  其中的hibernate.version为4.0.0.CR1。如果hibernate-core的版本过低会导致hibernate-search无法应用,所以在使用之前请谨慎考虑。

 

  在hibernate.cfg.xml中添加如下设置

 

 

Xml代码   收藏代码
  1. <property name="hibernate.search.default.directory_provider">filesystem</property>  
  2.   
  3.         <property name="hibernate.search.default.indexBase">/lucene/indexes</property>  

 

  第一个属性表示将使用文件系统作为为默认的目录提供者,第二个属性表示存储目录。

 

  如果想对某个实体进行索引,那么需要在该实体上加上@Indexed注释,对于该实体的标识符上加上@DocumentId注释,并且在你想要 进行索引的属性上加上@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)注释,其中注释中的设置根据具体需求而定,以后会讲解到这些属性的意义和作用。

 

  二。创建索引

 

  这里以Person为例,对其中的name属性创建索引,创建索引的代码如下:

 

 

Java代码   收藏代码
  1. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();  
  2.         Session session = sessionFactory.openSession();  
  3.         FullTextSession fullTextSession = Search.getFullTextSession(session);  
  4.         fullTextSession.createIndexer(Person.class).startAndWait();  

 

  三。搜索

 

 

Java代码   收藏代码
  1. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();  
  2.         Session session = sessionFactory.openSession();  
  3.         FullTextSession fullTextSession = Search.getFullTextSession(session);  
  4.         Transaction transaction = fullTextSession.beginTransaction();  
  5.         SearchFactory searchFactory = fullTextSession.getSearchFactory();  
  6.         QueryBuilder queryBuilder = searchFactory.buildQueryBuilder().forEntity(Person.class).get();  
  7.         Query query = queryBuilder.keyword().onField("name").matching("Zhong").createQuery();  
  8.         FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Person.class);  
  9.         List<Person> list = fullTextQuery.list();  
  10.         transaction.commit();  
  11.         session.close(); 

转载于:https://www.cnblogs.com/weipeng/archive/2012/03/22/2411786.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值