szhswl
宋针还的个人空间
        最近看研究了一下搜索引擎(COMPASS),学到的东西不多,但还是把所学的东西在这里记录一下,本人用的是COMPASS1.2
下载地址:https://compass.dev.java.net/files/documents/4669/71790/compass-1.2-with-dependencies.zip
        本人用的是里面带的一个例子(petclinic),主要的配置文件有:petclinic-servlet.xml,applicationContext-hibernate.xml,petclinic-cpm.xml,petclinic-cmd.xml,petclinic-hbm.xml,compass.cfg.xml(查找资料后,为了用paoding中文分词而加上)。
注:配置文件中的注释都是网上资料或本人对其的理解,如果有误还请指教,以免误导学习者。
petclinic-servlet.xml:
  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  3 <beans>
  4     <!-- ========================= 消息来源定义 ========================= -->
  5     <!-- 加载"/WEB-INF/classes"目录下messages*.properties文件 -->
  6     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  7         <property name="basename"><value>messages</value></property>
  8     </bean>
  9 
 10     <!-- ========================= 映射定义 ============================= -->
 11     <!-- 加载"/WEB-INF/classes"目录下views.properties文件 -->
 12     <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
 13         <property name="basename"><value>views</value></property>
 14     </bean>
 15     <!-- 定义异常处理VIEW -->
 16     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
 17         <property name="exceptionMappings">
 18             <props>
 19                 <prop key="org.compass.core.CompassException">dataAccessFailure</prop>
 20                 <prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
 21                 <prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop>
 22             </props>
 23         </property>
 24     </bean>
 25     <!-- 定义url映射 -->
 26     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 27         <property name="mappings">
 28             <props>
 29                 <prop key="/welcome.htm">clinicController</prop>
 30                 <prop key="/tutorial.htm">clinicController</prop>
 31                 <prop key="/spring-tutorial.htm">clinicController</prop>
 32                 <prop key="/vets.htm">clinicController</prop>
 33                 <prop key="/findOwners.htm">findOwnersForm</prop>
 34                 <prop key="/owner.htm">clinicController</prop>
 35                 <prop key="/addOwner.htm">addOwnerForm</prop>
 36                 <prop key="/editOwner.htm">editOwnerForm</prop>
 37                 <prop key="/addPet.htm">addPetForm</prop>
 38                 <prop key="/editPet.htm">editPetForm</prop>
 39                 <prop key="/addVisit.htm">addVisitForm</prop>
 40                 <prop key="/search.htm">searchController</prop>
 41                 <prop key="/index.htm">indexController</prop>
 42             </props>
 43         </property>
 44     </bean>
 45 
 46 
 47     <!-- ========================= 控制器定义 ========================= -->
 48     <bean id="compassSearchHelper" class="org.compass.sample.petclinic.CompassSearchHelper">
 49         <property name="highlightFields">
 50             <list>
 51                 <value>description</value>
 52             </list>
 53         </property>
 54         <property name="pageSize"><value>10</value></property>
 55         <constructor-arg ref="compass"/>
 56     </bean>
 57     <!-- 搜索控制器定义 -->
 58     <bean id="searchController" class="org.compass.spring.web.mvc.CompassSearchController">
 59         <property name="compass"><ref bean="compass"/></property>
 60         <property name="searchView"><value>searchView</value></property>
 61         <property name="searchResultsView"><value>searchResultsView</value></property>
 62         <property name="pageSize"><value>10</value></property>
 63         <property name="searchHelper"><ref local="advanceCompassSearchHelper"/></property>
 64     </bean>
 65     <!-- 索引控制器定义 -->
 66     <bean id="indexController" class="org.compass.spring.web.mvc.CompassIndexController">
 67         <property name="compassGps"><ref bean="compassGps"/></property>
 68         <property name="indexView"><value>indexView</value></property>
 69         <property name="indexResultsView"><value>indexResultsView</value></property>
 70     </bean>
 71 
 72     <!-- 定义clinic控制器 -->
 73     <bean id="clinicController" class="org.compass.sample.petclinic.web.ClinicController">
 74         <property name="methodNameResolver"><ref local="clinicControllerResolver"/></property>
 75         <property name="clinic"><ref bean="clinic"/></property>
 76     </bean>
 77 
 78     <!--  -->
 79     <bean id="clinicControllerResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
 80         <property name="mappings">
 81             <props>
 82                 <prop key="/welcome.htm">welcomeHandler</prop>
 83                 <prop key="/tutorial.htm">tutorialHandler</prop>
 84                 <prop key="/spring-tutorial.htm">springTutorialHandler</prop>
 85                 <prop key="/vets.htm">vetsHandler</prop>
 86                 <prop key="/owner.htm">ownerHandler</prop>
 87             </props>
 88         </property>
 89     </bean>
 90     <!-- 定义FORM -->
 91     <bean id="findOwnersForm" class="org.compass.sample.petclinic.web.FindOwnersForm">
 92         <property name="formView"><value>findOwnersForm</value></property>
 93         <property name="selectView"><value>selectOwnerView</value></property>
 94         <property name="successView"><value>ownerRedirect</value></property>
 95         <property name="clinic"><ref bean="clinic"/></property>
 96     </bean>
 97     <!-- 定义FORM -->
 98     <bean id="addOwnerForm" class="org.compass.sample.petclinic.web.AddOwnerForm">
 99         <property name="formView"><value>ownerForm</value></property>
100         <property name="successView"><value>ownerRedirect</value></property>
101         <property name="validator"><ref local="ownerValidator"/></property>
102         <property name="clinic"><ref bean="clinic"/></property>
103     </bean>
104     <bean id="editOwnerForm" class="org.compass.sample.petclinic.web.EditOwnerForm">
105         <property name="formView"><value>ownerForm</value></property>
106         <property name="successView"><value>ownerRedirect</value></property>
107         <property name="validator"><ref local="ownerValidator"/></property>
108         <property name="clinic"><ref bean="clinic"/></property>
109     </bean>
110     <bean id="ownerValidator" class="org.compass.sample.petclinic.validation.OwnerValidator"/>
111     <bean id="addPetForm" class="org.compass.sample.petclinic.web.AddPetForm">
112         <property name="formView"><value>petForm</value></property>
113         <property name="successView"><value>ownerRedirect</value></property>
114         <property name="validator"><ref local="petValidator"/></property>
115         <property name="clinic"><ref bean="clinic"/></property>
116     </bean>
117     <bean id="editPetForm" class="org.compass.sample.petclinic.web.EditPetForm">
118         <property name="formView"><value>petForm</value></property>
119         <property name="successView"><value>ownerRedirect</value></property>
120         <property name="validator"><ref local="petValidator"/></property>
121         <property name="clinic"><ref bean="clinic"/></property>
122     </bean>
123     <bean id="petValidator" class="org.compass.sample.petclinic.validation.PetValidator"/>
124     <!-- 定义FORM,用了visitValidatro和clinic属性 -->
125     <bean id="addVisitForm" class="org.compass.sample.petclinic.web.AddVisitForm">
126         <property name="formView"><value>visitForm</value></property>
127         <property name="successView"><value>ownerRedirect</value></property>
128         <property name="validator"><ref local="visitValidator"/></property>
129         <property name="clinic"><ref bean="clinic"/></property>
130     </bean>
131     <!-- 定义bean属性 -->
132     <bean id="visitValidator" class="org.compass.sample.petclinic.validation.VisitValidator"/>
133 </beans>
applicationContext-hibernate.xml:
  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  3 <beans>
  4     <!-- ========================= JDBC资源文件定义 ========================= -->
  5     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  6         <property name="location"><value>/WEB-INF/jdbc.properties</value></property>
  7     </bean>
  8     <!-- 定义数据源 -->
  9     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 10         <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
 11         <property name="url"><value>${jdbc.url}</value></property>
 12         <property name="username"><value>${jdbc.username}</value></property>
 13         <property name="password"><value>${jdbc.password}</value></property>
 14     </bean>
 15     <!-- 定义Hibernate的SessionFactory -->
 16     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 17         <property name="dataSource"><ref local="dataSource"/></property>
 18         <property name="mappingResources">
 19             <value>petclinic.hbm.xml</value>
 20         </property>
 21         <property name="hibernateProperties">
 22             <props>
 23                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
 24                 <prop key="hibernate.show_sql">true</prop>
 25                 <prop key="hibernate.generate_statistics">true</prop>
 26             </props>
 27         </property>
 28         <property name="eventListeners">
 29             <map>
 30                 <entry key="merge">
 31                     <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
 32                 </entry>
 33             </map>
 34         </property>
 35     </bean>
 36      <!-- COMPASS相关配置 -->
 37     <bean id="compass" class="org.compass.spring.LocalCompassBean">
 38         <property name="resourceLocations">
 39             <list>
 40                 <!-- 这里是compass所需要的两个关于数据项的配置文件 -->
 41                 <value>classpath:org/compass/sample/petclinic/petclinic.cmd.xml</value>
 42                 <value>classpath:petclinic.cpm.xml</value>
 43             </list>
 44         </property>
 45         <property name="configLocation">
 46             <value>classpath:compass.cfg.xml</value>
 47         </property>
 48         <property name="compassSettings">
 49             <props>
 50                 <!-- 索引文件在服务器上的存储路径 -->
 51                 <!-- 加亮显示 -->
 52                 <prop key="compass.engine.highlighter.default.formatter.simple.pre">
 53                      <![CDATA[<font color="red"><b>]]>
 54                 </prop>
 55                 <prop key="compass.engine.highlighter.default.formatter.simple.post">
 56                      <![CDATA[</b></font>]]>
 57                 </prop>
 58             </props>
 59         </property>
 60         <property name="transactionManager">
 61             <ref local="transactionManager" />
 62         </property>
 63     </bean>
 64     
 65     <bean id="queryAnalyzer" class="net.paoding.analysis.analyzer.PaodingAnalyzer">
 66     </bean> 
 67     
 68     
 69     <!-- 与hibernate的绑定,用Hibernate3事件系统,支持Real Time Data Mirroring.经Hiberante的数据改变会自动被反射到索引里面 -->
 70     <!--<bean id="hibernateGpsDevice" class="org.compass.spring.device.hibernate.dep.SpringHibernate3GpsDevice">-->
 71     <bean id="hibernateGpsDevice" class="org.compass.gps.device.hibernate.HibernateGpsDevice">
 72       <property name="name"><value>hibernateDevice</value></property>
 73       <property name="sessionFactory"><ref local="sessionFactory" /></property>
 74       <property name="nativeExtractor"><bean class="org.compass.spring.device.hibernate.SpringNativeHibernateExtractor" /></property>
 75     </bean>
 76     <!-- Compass中建立索引与mirror database change的部件,定义了init-method 和destory-method,
 77         会自动随ApplicaitonContext的启动,开始监控Hibernate的变化,经Hiberante的数据改变会自动被反射到索引里面 -->
 78     <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop">
 79        <property name="compass"><ref bean="compass" /></property>
 80        <property name="gpsDevices">
 81          <list>
 82              <bean class="org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper">
 83                  <property name="gpsDevice" ref="hibernateGpsDevice" />
 84              </bean>
 85          </list>
 86        </property>
 87      </bean>
 88     <!-- 定义事务处理交于hibernate3 -->
 89     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 90         <property name="sessionFactory"><ref local="sessionFactory"/></property>
 91     </bean>
 92 
 93     <!-- ========================= 业务对象相关定义 ========================= -->
 94     <bean id="clinicTarget" class="org.compass.sample.petclinic.hibernate.HibernateClinic">
 95         <property name="sessionFactory"><ref local="sessionFactory"/></property>
 96     </bean>
 97     <!-- 定义拦截器 -->
 98     <bean id="clinic" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
 99         <property name="transactionManager"><ref local="transactionManager"/></property>
100         <property name="target"><ref local="clinicTarget"/></property>
101         <property name="transactionAttributes">
102             <props>
103                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
104                 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
105                 <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
106                 <prop key="store*">PROPAGATION_REQUIRED</prop>
107             </props>
108         </property>
109     </bean>
110 </beans>
compass.cfg.xml:
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <compass-core-config
 3  xmlns="http://www.opensymphony.com/compass/schema/core-config"
 4  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5  xsi:schemaLocation="http://www.opensymphony.com/compass/schema/core-config
 6            http://www.opensymphony.com/compass/schema/compass-core-config.xsd">
 7 
 8 <compass name="default"> 
 9     <connection>
10         <file path="/${user.home}/compass" /> <!-- 这里是索引文件的存放路径 -->
11     </connection>
12 
13     <searchEngine>
14     <!-- 因是使用自己的分词算法,所以这里的类型必须是CustomAnalyzer -->
15         <analyzer name="paodingAnalyer" type="CustomAnalyzer" analyzerClass="net.paoding.analysis.analyzer.PaodingAnalyzer">
16             <stopWords>
17                 <stopWord value="test" />
18             </stopWords>
19         </analyzer>
20     </searchEngine>
21 
22  </compass>
23 </compass-core-config>
petclinic-cpm.xml(列出部分):
 1 <?xml version="1.0"?>
 2 <!DOCTYPE compass-core-mapping PUBLIC
 3     "-//Compass/Compass Core Mapping DTD 1.0//EN"
 4     "http://www.opensymphony.com/compass/dtd/compass-core-mapping.dtd">
 5 
 6 <!-- 这里的包名必须和pojo的包名一致 -->
 7 <compass-core-mapping package="org.compass.sample.petclinic">
 8 
 9 <!-- 定义实体及其字段的对应关系 -->
10 
11 <!-- 注意实体及其字段的名称的大小写应当与pojo对象一致,而不是与数据库一致
12   关于pojo与数据库的对应表的一致性关系由hibernate的映谢文件定义,而不是这个文件
13   当前映射文件只定义compass与hibernate的关系 -->
14     <contract alias="entity">
15         <id name="id" />
16     </contract>
17 
18     <contract alias="named-entity" extends="entity">
19         <property name="name">
20             <meta-data>${petclinic.name}</meta-data>
21         </property>
22     </contract>
23 
24     <contract alias="person" extends="entity">
25         <property name="firstName">
26             <meta-data>${petclinic.firstName}</meta-data>
27         </property>
28 
29         <property name="lastName">
30             <meta-data>${petclinic.lastName}</meta-data>
31         </property>
32 
33         <property name="address">
34             <meta-data>${petclinic.address}</meta-data>
35         </property>
36 
37         <property name="city">
38             <meta-data>${petclinic.city}</meta-data>
39         </property>
40 
41         <property name="telephone">
42             <meta-data>${petclinic.telephone}</meta-data>
43         </property>
44     </contract>
45 
petclinic-cmd.xml(列出部分):
 1 <?xml version="1.0"?>
 2 <!DOCTYPE compass-core-meta-data PUBLIC 
 3     "-//Compass/Compass Core Meta Data DTD 1.0//EN"
 4     "http://www.opensymphony.com/compass/dtd/compass-core-meta-data.dtd">
 5 
 6 <compass-core-meta-data>
 7 
 8     <!-- 定义一个实体和字段组-->
 9     <meta-data-group id="petclinic" displayName="Petclinic Meta Data">
10     
11         <description>Petclinic Meta Data</description>       
12         <uri>http://compass/sample/petclinic</uri>
13         
14         <!-- 申明所有需要检索的实体--> 
15         <alias id="vet" displayName="Vet">
16             <description>Vet alias</description>
17             <uri>http://compass/sample/petclinic/alias/vet</uri>
18             <name>vet</name>
19         </alias>
20 
21         <alias id="pet" displayName="Pet">
22             <description>Pet alias</description>
23             <uri>http://compass/sample/petclinic/alias/pet</uri>
24             <name>pet</name>
25         </alias>
26 
27         <alias id="owner" displayName="Owner">
28             <description>Owner alias</description>
29             <uri>http://compass/sample/petclinic/alias/owner</uri>
30             <name>owner</name>
31         </alias>
32 
33         <alias id="visit" displayName="Visit">
34             <description>Visit alias</description>
35             <uri>http://compass/sample/petclinic/alias/visit</uri>
36             <name>visit</name>
37         </alias>
38 
39         <alias id="specialty" displayName="Specialty">
40             <description>Specialty alias</description>
41             <uri>http://compass/sample/petclinic/alias/specialty</uri>
42             <name>specialty</name>
43         </alias>
44 
45         <alias id="petType" displayName="Pet Type">
46             <description>Pet Type alias</description>
47             <uri>http://compass/sample/petclinic/alias/petType</uri>
48             <name>petType</name>
49         </alias>
50 
51         <!-- 申明所有需要检索的属性或者字段,而不管这些属性或者字段是哪个实体的 -->
52         <meta-data id="type" displayName="Type">
53             <description>Type of an entity in the system</description>
54             <uri>http://compass/sample/petclinic/type</uri>
55             <name>type</name>
56             <value id="mdPerson">person</value>
57         </meta-data>
58 
59         <meta-data id="name" displayName="Name">
60             <description>The name of an entity</description>
61             <uri>http://compass/sample/petclinic/name</uri>
62             <name>name</name>
63         </meta-data>
petclinic-hbm.xml就不做介绍,普通的hibernate配置文件。
        接下来是一个问题也是本人笔记的原因,
        在本人上一篇随笔(http://www.blogjava.net/szhswl/archive/2007/12/18/168437.html)写到查询时的注意问题,用"tes*"做为查询条件时是前缀查询,lucene里QueryParser类实例了PrefixQuery类,在compass1.2源代码里org.compass.core.lucene.engine.queryparser.CompassQueryParser类继承了并且重写了QueryParser类里的getPrefixQuery方法,方法源代码如下:
1     protected Query getPrefixQuery(String field, String termStr) throws ParseException {
2         if (getLowercaseExpandedTerms()) {
3             termStr = termStr.toLowerCase();
4         }
5 
6         Term t = new Term(field, termStr);
7         return new ConstantScorePrefixQuery(t);\
8     }
也就是说用"tes*"查询时在compass1.2里实例了ConstantScorePrefixQuery,compass1.2用的是lucene2.2,本人查看了lucene2.2里源代码,ConstantScorePrefixQuery类里实现高亮时用到的一个重要方法源代码如下:
1   public void extractTerms(Set terms) {
2     // OK to not add any terms when used for MultiSearcher,
3     // but may not be OK for highlighting
4   }
方法里说得很清楚,根本就没有实现,所以用compass1.2的petclinic例子时,用"tes*"查询时,查询结果没错,但页面${hit.highlightedText['highlightfield']}
高亮显示出现问题,然后本人把org.compass.core.lucene.engine.queryparser.CompassQueryParser类里getPrefixQuery方法注释,高亮显示居然正常了,但这还是修改了compass1.2里的源代码,应该不是最佳解决方案,如果有达人知道还请指教。

转载请注明出处。

---------------------------------------------------------------------------------------------------------------------------------
说人之短,乃护己之短。夸己之长,乃忌人之长。皆由存心不厚,识量太狭耳。能去此弊,可以进德,可以远怨。
http://www.blogjava.net/szhswl
------------------------------------------------------------------------------------------------------ ----------------- ---------
posted on 2007-12-23 13:25 宋针还 阅读(2934) 评论(4)  编辑  收藏

FeedBack:
# re: 搜索引擎(compass)学习笔记
2007-12-23 13:41 | 祎恬凡
交换一个链接,怎么样?  回复  更多评论
  
# re: 搜索引擎(compass)学习笔记
2007-12-23 14:10 | 宋针还
@祎恬凡
OK  回复  更多评论
  
# re: 搜索引擎(compass)学习笔记[未登录]
2009-06-22 14:31 | jane
CompassQueryParser和CompassMultiFieldQueryParser这两个类如何使用,楼主有没有比较好的例子?正在研究compass,在这个地方遇到了困难,还请楼主不吝赐教。  回复  更多评论
  
# re: 搜索引擎(compass)学习笔记[未登录]
2009-08-31 12:57 | 宋针还
@jane
很久没整compass了,当时是根据领导需求,研究下。不好意思。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: