原文链接:http://www.cnblogs.com/MoShin/archive/2011/01/19/leon.html
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):
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
两个类没什么,就是一个简单的JavaBean,之间是一对多关系,配置好hibernate映射文件,基本就完成了映射。
然后解释一下注解:
下面就接着编写Compass的配置文件,这里写得是与Spring框架整合的配置方法。
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将来将索引库建立在哪里,也同样是搜索时从哪里搜索的位置,也就是索引库位置。
第30行和31行,配置具体需要索引的JavaBean。
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
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配置基本就完成了, 现在启动服务器,就会创建索引库了。
随后记录怎样进行搜索,包括多条件搜索,关联外键搜索等等。。。