面向对象的全文搜索引擎hibernate-search例子

 

面向对象的全文搜索引擎hibernate-search例子

分类: 随笔 427人阅读 评论(0) 收藏 举报

 

         “Full text search engines like Apache Lucene are very powerful technologies to add efficient free text search capabilities to applications. However, Lucene suffers several mismatches when dealing with object domain models.To achieve this Hibernate Search is combining the power of Hibernate and Apache Lucene.”   

         Apache Lucene是非常强大的全文搜索引擎,然而,在处理面向对象模型时会很多的不方便,为了解决这个问题,结合hibernate,Lucene两者的优点,hibernate组织开发了hibernate-search框架。

         以一个购物网站的商品表tb_goods作为例子,简单说一下如何建立索引,如何通过lucene索引搜索数据库数据。

         第一步:orm映射,并用注释告诉lucene如何索引。

                           在主键的get方法上注上@Id

                                       如:@Id
                                       private Integer goodId;

                           在要索引的字段上注上@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)

                                      如:

                                     @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
                              private String goodsName;

  1. /** 
  2.  * TbGoods entity. @author MyEclipse Persistence Tools 
  3.  */  
  4. @Entity  
  5. @Indexed  
  6. @Table(name = "tb_goods")  
  7. public class TbGoods implements java.io.Serializable {  
  8.   
  9.   
  10.     // Fields  
  11.   
  12.   
  13.     /** 
  14.      *  
  15.      */  
  16.     private static final long serialVersionUID = 1060427957232239211L;  
  1.         //在主键的get方法上注上@Id  
  2. @Id  
  3. @GeneratedValue  
  4. private Integer goodId;  
  1.          //要索引的字段,goodsName将会被索引  
  2.     @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)  
  3.     private String goodsName;  
  4.       
  5.     @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)  
  6.     private String introduce;  
  7.       
  8.     private Double importPrice;  
  9.     private Double nowPrice;  
  10.     private String picture;  
  11.     private Timestamp importDate;  
  12.     private Integer newGoods;  
  13.     private Integer number;  
  14.     private Integer hit;  
  15.     private String remarks;  
  16.   
  17.   
  18.     // Constructors  
  19.   
  20.   
  21.     /** default constructor */  
  22.     public TbGoods() {  
  23.     }  
  24.   
  25.   
  26.     /** minimal constructor */  
  27.     public TbGoods(String goodsName, Double nowPrice) {  
  28.         this.goodsName = goodsName;  
  29.         this.nowPrice = nowPrice;  
  30.     }  
  31.   
  32.   
  33.     /** full constructor */  
  34.     public TbGoods(String goodsName, String introduce,  
  35.             Double importPrice, Double nowPrice, String picture,  
  36.             Timestamp importDate, Integer newGoods, Integer number,  
  37.             Integer hit, String remarks) {  
  38.         this.goodsName = goodsName;  
  39.         this.introduce = introduce;  
  40.         this.importPrice = importPrice;  
  41.         this.nowPrice = nowPrice;  
  42.         this.picture = picture;  
  43.         this.importDate = importDate;  
  44.         this.newGoods = newGoods;  
  45.         this.number = number;  
  46.         this.hit = hit;  
  47.         this.remarks = remarks;  
  48.            
  49.     }  
  50.   
  51.   
  52.     // Property accessors  
  53.       
  54.     public Integer getGoodId() {  
  55.         return this.goodId;  
  56.     }  
  57.   
  58.   
  59.     public void setGoodId(Integer goodId) {  
  60.         this.goodId = goodId;  
  61.     }  
  62.     public String getGoodsName() {  
  63.         return this.goodsName;  
  64.     }  
  65.   
  66.   
  67.     public void setGoodsName(String goodsName) {  
  68.         this.goodsName = goodsName;  
  69.     }  
  70.       
  71.     public String getIntroduce() {  
  72.         return this.introduce;  
  73.     }  
  74.   
  75.   
  76.     public void setIntroduce(String introduce) {  
  77.         this.introduce = introduce;  
  78.     }  
  79.       
  80.     public Double getImportPrice() {  
  81.         return this.importPrice;  
  82.     }  
  83.   
  84.   
  85.     public void setImportPrice(Double importPrice) {  
  86.         this.importPrice = importPrice;  
  87.     }  
  88.     public Double getNowPrice() {  
  89.         return this.nowPrice;  
  90.     }  
  91.   
  92.   
  93.     public void setNowPrice(Double nowPrice) {  
  94.         this.nowPrice = nowPrice;  
  95.     }  
  96.   
  97.   
  98.     public String getPicture() {  
  99.         return this.picture;  
  100.     }  
  101.   
  102.   
  103.     public void setPicture(String picture) {  
  104.         this.picture = picture;  
  105.     }  
  106.   
  107.   
  108.     public Timestamp getImportDate() {  
  109.         return this.importDate;  
  110.     }  
  111.   
  112.   
  113.     public void setImportDate(Timestamp importDate) {  
  114.         this.importDate = importDate;  
  115.     }  
  116.   
  117.   
  118.     public Integer getNewGoods() {  
  119.         return this.newGoods;  
  120.     }  
  121.   
  122.   
  123.     public void setNewGoods(Integer newGoods) {  
  124.         this.newGoods = newGoods;  
  125.     }  
  126.   
  127.   
  128.     public Integer getNumber() {  
  129.         return this.number;  
  130.     }  
  131.   
  132.   
  133.     public void setNumber(Integer number) {  
  134.         this.number = number;  
  135.     }  
  136.   
  137.   
  138.     public Integer getHit() {  
  139.         return this.hit;  
  140.     }  
  141.   
  142.   
  143.     public void setHit(Integer hit) {  
  144.         this.hit = hit;  
  145.     }  
  146.   
  147.   
  148.     public String getRemarks() {  
  149.         return this.remarks;  
  150.     }  
  151.   
  152.   
  153.     public void setRemarks(String remarks) {  
  154.         this.remarks = remarks;  
  155.     }  
  156.     @Override  
  157.     public String toString(){  
  158.         StringBuilder stringBuilder = new StringBuilder("Id: ").append(this.getGoodId())  
  159.                 .append(" | goodName:").append(this.getGoodsName()).  
  160.                 append(" | Introduce:\n").append(this.getIntroduce())  
  161.                 .append(" | ImportPrice:").append(this.getImportPrice())  
  162.                 .append(" | nowPrice:").append(this.getNowPrice());  
  163.         return stringBuilder.toString();  
  164.     }  
  165.   
  166.   
  167. }  

    第二步:编写索引方法,和搜索方法:

 

  1. public class SearchEngine {  
  2.       
  3.     /* 建立lucene索引  
  4.      * 搜索之前必须有索引文件,不然会报错。  
  5.      * hibernate的配置文件中  
  6.      * hibernate.search.default.indexBase的值就是索引保存的路径,  
  7.      * 搜索前必须有内容,索引完后下次搜索可以不用索引了。  
  8.      */  
  9.     public void doIndex() throws InterruptedException {  
  10.         Session session = HibernateSessionFactory.getSession();  
  11.         FullTextSession fullTextSession = Search.getFullTextSession(session);  
  12.         fullTextSession.createIndexer().startAndWait();  
  13.         fullTextSession.close();  
  14.     }  
  15.       
  16.     /**  
  17.      * 搜索方法  
  18.      * @param queryString 要查询的关键字  
  19.      * @return 返回一个数据封装模型的数据库表对象的集合,这里是符合条件的TbGoods集合  
  20.      */  
  21.     public List<TbGoods> search(String queryString) {  
  22.         Session session = HibernateSessionFactory.getSession();  
  23.         FullTextSession fullTextSession = Search.getFullTextSession(session);  
  24.           
  25.         QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(TbGoods.class).get();  
  26.         //在这里修改要查询的字段,比如可以改成introduce  
  27.         org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("goodsName").matching(queryString).createQuery();  
  28.         org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, TbGoods.class);  
  29.           
  30.         List<TbGoods> tbgoodsList = fullTextQuery.list();  
  31.           
  32.         fullTextSession.close();  
  33.           
  34.         return tbgoodsList;  
  35.     }  
  36. }  

第三步:编写主方法测试:先索引,后搜索。

 

 

  1. public static void main(String[] args) throws InterruptedException{  
  2.     SearchEngine searchEngine = new SearchEngine();  
  3.     //搜索前必须有索引得到的文件,此句运行后可以删掉,下次查询只需用索引过的索引文件即可。  
  4.     searchEngine.doIndex();  
  5.     List<TbGoods> result = searchEngine.search("固态硬盘");   //在这里写要搜索的关键字         
  6.     System.out.println("\n\n>>>>>>找到与关键字有关的记录 ' 固态硬盘 '");  
  7.     for (TbGoods tbgoods : result) {  
  8.         System.out.println(tbgoods.toString());  
  9.     }     
  10.    }  

运行结果:

 

 

  1. >>>>>>找到与关键字有关的记录 ' 固态硬盘 '  
  2. Id: 1 | goodName:美光固态硬盘(Crucial)M4系列 128G 2.5英寸 | Introduce:  
  3. 美光(Crucial)M4系列 128G 2.5英寸 SATA-3固态硬盘(CT128M4SSD2)高速稳 | ImportPrice:500.0 | nowPrice:799.0  
  4. Id: 6 | goodName:美光M4系列 128G 2.5英寸固态硬盘 | Introduce:  
  5. 高速稳定 | ImportPrice:799.0 | nowPrice:799.0  
  6. Id: 8 | goodName:美光M4系列 64G 2.5英寸 SATA-3固态硬盘 | Introduce:  
  7.  | ImportPrice:500.0 | nowPrice:499.0  

例子结束,初学,多多指教。

 

Myeclipse工程文件已经上传到csdn:http://download.csdn.net/detail/hortond/4511699

包含jar包。只要有mysql和myeclipse就可直接运行。

posted @ 2013-04-23 22:12  眉间尺之魂  阅读(860)  评论(0编辑  收藏  举报