原文鏈接: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):
兩個類沒什麼,就是一個簡單的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配置基本就完成了, 現在啓動服務器,就會創建索引庫了。
隨後記錄怎樣進行搜索,包括多條件搜索,關聯外鍵搜索等等。。。