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(); 
  • 相关阅读:
    sitecore系统教程之体验编辑器
    Sitecore安装(手动方式)
    Sitecore详细安装(包含sitecore安装过程截图)
    logstash快速入门实战指南-Logstash简介
    Elasticsearch从入门到精通之Elasticsearch基本概念
    arcgis api 3.x for js 解决 textSymbol 文本换行显示(附源码下载)
    openlayers4 入门开发系列之前端动态渲染克里金插值 kriging 篇(附源码下载)
    arcgis api 4.x for js 结合 react 入门开发系列react全家桶实现加载天地图(附源码下载)
    arcgis api 4.x for js 结合 react 入门开发系列"esri-loader"篇(附源码下载)
    arcgis api 4.x for js 结合 react 入门开发系列初探篇(附源码下载)
  • 原文地址:https://www.cnblogs.com/weipeng/p/2411786.html
Copyright © 2011-2022 走看看