zoukankan      html  css  js  c++  java
  • 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(); 
  • 相关阅读:
    NSLocalizedString用法
    4-27学习心得
    手势学习
    plist处理
    数据存储
    initWithFrame方法
    控制器跳转小常识
    UIGestureRecognizer学习笔记
    博客资源
    检测手机wifi有没有打开
  • 原文地址:https://www.cnblogs.com/weipeng/p/2411786.html
Copyright © 2011-2022 走看看