compass 搜索框架学习笔记(一)

Compass搜索框架学习笔记(一) 借助Compass建立索引库

 

Compass是基于Lucene的一个搜索框架,它可以创建索引,修改索引和查询,主要功能就这些

首先需要Jar包,在Comapss官方网站,down下来Compass开源框架,我down的是Comapss2.1.0 . 下载地址:http://www.compass-project.org/.

下载下来解压开,挑选自己需要的Jar包,我用了是 以下几个:

compass-2.1.0.jar | compass-index-patch.jar | lucene-core.jar | lucene-highlighter.jar 这四个,分词我自己下载了一个,用得庖丁分词:paoding-analysis.jar

一共5个jar包. 

5个Jar导入项目以后,开始选择需要建立索引的类,我用得是注解的方法进行索引标注:

两个类,一个用户类(User),一个部门类(Dept):

User类
Dept类

两个类没什么,就是一个简单的JavaBean,之间是一对多关系,配置好hibernate映射文件,基本就完成了映射。

然后解释一下注解:

 

@Searchable 这个注解标明这个类是需要创建索引的,必须有

 

@SearchableId 这个注解是标注索引这个类的ID索引时哪个,必须有

 

@SearchableProperty 这个注解是标注哪个字段需要建立索引

 

@SearchableComponent  这个注解是标注哪个属性字段是外键关联的,即便是个外键集合也同样适用,比如Dept类中的User集合也可以用这个标注。

 

 

 

 

下面就接着编写Compass的配置文件,这里写得是与Spring框架整合的配置方法。

 

复制代码
 1  <? xml version="1.0" encoding="GBK" ?>
 2  < beans  xmlns ="http://www.springframework.org/schema/beans"
 3      xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 4      xsi:schemaLocation =" http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
 5      default-lazy-init ="false" >
 6      
 7        <!--  Comapss ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -->
 8             
 9             < bean  id ="annotationConfiguration"
10              class ="org.compass.annotations.config.CompassAnnotationsConfiguration" >
11           </ bean >
12          
13          
14           < bean  id ="compass"  class ="org.compass.spring.LocalCompassBean" >
15            
16           < property  name ="resourceDirectoryLocations" >
17               < list >
18                   < value > classpath:com/xraining/vo </ value >
19               </ list >
20           </ property >
21          
22          
23           < property  name ="connection" >
24               < value > /lucene/indexes </ value >
25           </ property >
26 
27 
28           < property  name ="classMappings" >
29               < list >
30                   < value > com.xraining.vo.User </ value >
31                   < value > com.xraining.vo.Dept </ value >
32               </ list >
33           </ property >
34          
35          
36           < property  name ="compassConfiguration"  ref ="annotationConfiguration"   />
37 
38 
39           < property  name ="compassSettings" >
40               < props >
41                   < prop  key ="compass.transaction.factory" > org.compass.spring.transaction.SpringSyncTransactionFactory </ prop >
42                     < prop  key ="compass.engine.analyzer.MMAnalyzer.CustomAnalyzer" > net.paoding.analysis.analyzer.PaodingAnalyzer  </ prop >
43                    
44                     <!--  高亮关键字  -->   
45                   < prop  key ="compass.engine.highlighter.default.formatter.simple.pre" > <![CDATA[ <font color="red"><b> ]]> </ prop >   
46                   < prop  key ="compass.engine.highlighter.default.formatter.simple.post" > <![CDATA[ </b></font> ]]>   
47                   <!--  高亮关键字 END  -->
48                    
49                   </ prop >   
50               </ props >
51           </ property >
52 
53           < property  name ="transactionManager"  ref ="transactionManager"   />
54          
55       </ bean >
56      
57      
58       < bean  id ="hibernateGpsDevice"
59          class ="org.compass.gps.device.hibernate.HibernateGpsDevice" >
60           < property  name ="name" >
61               < value > hibernateDevice </ value >
62           </ property >
63           < property  name ="sessionFactory"  ref ="sessionFactory"   />
64           < property  name ="mirrorDataChanges" >
65               < value > true </ value >
66           </ property >
67       </ bean >
68       <!--  同步更新索引  -->
69       < bean  id ="compassGps"  class ="org.compass.gps.impl.SingleCompassGps"
70          init-method ="start"  destroy-method ="stop" >
71           < property  name ="compass"  ref ="compass"   />
72           < property  name ="gpsDevices" >
73               < list >
74                   < bean
75                       class ="org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper" >
76                       < property  name ="gpsDevice"  ref ="hibernateGpsDevice"   />
77                   </ bean >
78               </ list >
79           </ property >
80       </ bean >
81 
82 
83       < bean  id ="compassTemplate"
84          class ="org.compass.core.CompassTemplate" >
85           < property  name ="compass"  ref ="compass"   />
86       </ bean >
87 
88 
89       <!--  定时重建索引(利用quartz)或随Spring ApplicationContext启动而重建索引  -->
90       < bean  id ="compassIndexBuilder"  class ="com.common.biz.CompassIndexBuilder"  lazy-init ="false" >
91           < property  name ="compassGps"  ref ="compassGps"   />
92           < property  name ="buildIndex"  value ="true"   />
93           < property  name ="lazyTime"  value ="3"   />
94       </ bean >
95  <!--  Comapss  END |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||-  -->
96      
97       </ beans >
复制代码

这个配置文件中,大部分都是必须的配置,第18行,指定索引源位置,我指定的是我的实体类包。第24行的配置是告诉Compass将来将索引库建立在哪里,也同样是搜索时从哪里搜索的位置,也就是索引库位置。

< value > /lucene/indexes </ value > 这样写,索引库就会建立在服务器项目的根目录下的lucene文件夹下的indexs文件夹里,会自动创建这些文件夹,也可以不建立在服务器上,可以指定能硬盘的路径,比如:
<value>file://e:/myIndex</value> 这样就会建立在硬盘指定位置的文件夹里,文件夹会自动创建。

第30行和31行,配置具体需要索引的JavaBean。 

下面最后的一个Bean,这个Bean不是必须的,是一个帮助Bean,这个Bean是用来创建索引库的,利用了线程和InitializingBean接口,具体实现如下:
复制代码
 1  package  com.common.biz;
 2  import  org.compass.gps.CompassGps;
 3  import  org.springframework.beans.factory.InitializingBean;
 4 
 5 
 6  /**
 7   * 通过quartz定时调度定时重建索引或自动随Spring ApplicationContext启动而重建索引的Builder.
 8   * 会启动后延时数秒新开线程调用compassGps.index()函数.
 9   * 默认会在Web应用每次启动时重建索引,可以设置buildIndex属性为false来禁止此功能.
10   * 也可以不用本Builder, 编写手动调用compassGps.index()的代码.
11   *
12    */
13  public   class  CompassIndexBuilder  implements  InitializingBean {   
14       //  是否需要建立索引,可被设置为false使本Builder失效.
15       private   boolean  buildIndex  =   false ;
16 
17       //  索引操作线程延时启动的时间,单位为秒
18       private   int  lazyTime  =   10 ;
19 
20       //  Compass封装
21       private  CompassGps compassGps;
22 
23       //  索引线程
24       private  Thread indexThread  =   new  Thread() {
25 
26          @Override
27           public   void  run() {
28               try  {
29                  Thread.sleep(lazyTime  *   1000 );
30                  System.out.println( " begin compass index... " );
31                   long  beginTime  =  System.currentTimeMillis();
32                   //  重建索引.
33                   //  如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引,
34                   //  索引完成后再进行覆盖.
35                  compassGps.index();
36                   long  costTime  =  System.currentTimeMillis()  -  beginTime;
37                  System.out.println( " compss index finished. " );
38                  System.out.println( " costed  "   +  costTime  +   "  milliseconds " );
39              }  catch  (InterruptedException e) {
40                  e.printStackTrace();
41              }
42          }
43      };
44 
45       /**
46       * 实现<code>InitializingBean</code>接口,在完成注入后调用启动索引线程.
47       *
48       *  @see  org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
49        */
50       public   void  afterPropertiesSet()  throws  Exception {
51           if  (buildIndex) {
52              indexThread.setDaemon( true );
53              indexThread.setName( " Compass Indexer " );
54              indexThread.start();
55          }
56      }
57 
58       public   void  setBuildIndex( boolean  buildIndex) {
59           this .buildIndex  =  buildIndex;
60      }
61 
62       public   void  setLazyTime( int  lazyTime) {
63           this .lazyTime  =  lazyTime;
64      }
65 
66       public   void  setCompassGps(CompassGps compassGps) {
67           this .compassGps  =  compassGps;
68      }
69  }
70 
复制代码

 

 

 

 

 

 

下面是Spring的相关配置:
复制代码
 1  <? xml version="1.0" encoding="UTF-8" ?>
 2  < beans  xmlns ="http://www.springframework.org/schema/beans"
 3      xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 4      xmlns:context ="http://www.springframework.org/schema/context"
 5      xmlns:aop ="http://www.springframework.org/schema/aop"
 6      xmlns:tx ="http://www.springframework.org/schema/tx"
 7      xsi:schemaLocation ="http://www.springframework.org/schema/beans 
 8             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 9             http://www.springframework.org/schema/context
10             http://www.springframework.org/schema/context/spring-context-2.5.xsd
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
12             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
13             
14              <!--  开启自动扫描   -->
15              < context:component-scan  base-package ="com.xraining" ></ context:component-scan >
16              < context:component-scan  base-package ="com.common" ></ context:component-scan >
17            
18             
19              <!--  配置数据源  -->
20       < bean  id ="dataSource"
21          class ="com.mchange.v2.c3p0.ComboPooledDataSource" >
22           < property  name ="driverClass"  value ="com.mysql.jdbc.Driver"   />
23           < property  name ="jdbcUrl"  value ="jdbc:mysql:///mytest"   />
24           < property  name ="maxIdleTime"  value ="25000"   />
25           < property  name ="properties" >
26               < props >
27                   < prop  key ="user" > root </ prop >
28                   < prop  key ="password" > java </ prop >
29                   < prop  key ="c3p0.acquire_increment" > 2 </ prop >
30                   < prop  key ="c3p0.max_size" > 20 </ prop >
31                   < prop  key ="c3p0.min_size" > 1 </ prop >
32               </ props >
33           </ property >
34       </ bean >
35 
36       <!--  配置Hibernate:SessionFactory  -->
37       < bean  id ="sessionFactory"
38          class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
39           <!--  指定数据源  -->
40           < property  name ="dataSource"  ref ="dataSource" ></ property >
41           <!--  配置对象实体映射文件  -->
42           < property  name ="mappingResources" >
43               < value >
44                   <!--  实体关系映射文件  -->
45                      com/xraining/vo/User.hbm.xml,
46                      com/xraining/vo/Dept.hbm.xml
47                   <!--  ==================  -->
48               </ value >
49           </ property >
50           <!--  其他Hibernate常用属性  -->
51           < property  name ="hibernateProperties" >
52               < props >
53                   < prop  key ="hibernate.dialect" >
54                      org.hibernate.dialect.MySQL5Dialect
55                   </ prop >
56                   < prop  key ="hibernate.show_sql" > true </ prop >
57               </ props >
58           </ property >
59       </ bean >
60             
61     
62                <!--  配置处理事务的bean  -->
63              < bean  id ="transactionManager"  class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
64                 < property  name ="sessionFactory"  ref ="sessionFactory" ></ property >
65              </ bean >
66             
67              <!--  开启事物的annotation支持  -->
68              < tx:annotation-driven  transaction-manager ="transactionManager"   />
69             
70             
71            
72            
73               </ beans >
复制代码

 

 

 

在这里,我将Hibernate配置文件没有写,而是将Hibernate的相关配置写在了Spring文件里,开始我是单独写了一个Hibernate配置文件,可是不知道什么原因,Compass配置文件读不到SessionFactory,最后写在Spring里就OK了。

Compass配置文件里的内容也可以写在Spring配置里,为了清晰,我分开了。 

 

写到这里,Compass配置基本就完成了, 现在启动服务器,就会创建索引库了。

随后记录怎样进行搜索,包括多条件搜索,关联外键搜索等等。。。

 

 

 

 

 

转载于:https://www.cnblogs.com/faylz/archive/2013/04/23/3038509.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值