1.概述
网站: http://www.compassframework.org/
Compass是一流的开放源码JAVA搜索引擎框架,对于你的应用修饰,搜索引擎语义更具有能力。
依靠顶级的Lucene搜索引擎,Compass 结合了,像 Hibernate和 Sprin的流行的框架,为你的应用提供了从数据模型和数据源同步改变的搜索力.并且添加了2方面的特征,事物管理和快速更新优化.
Compass的目标是:把java应用简单集成到搜索引擎中.编码更少,查找数据更便捷 .
2.Object/Search Engine Mapping - 配置XML
OSEM: 对象搜索引擎影射(Object Search Engine Mapping),通过xml配置文件,提供了POJO's (Plain Old Java Objects)到搜索引擎.
2.1 XML配置
一般都是 *.cpm.xml 形式 .
1) package指定了对应java类的包名,
2)class为pojo类名.contract为公共部分, 子类被可以extends.Property 对 java类的属性。
3)资源的Property引用的Search Engine meta-data.
注意: id为类标识.
< contract alias = " product " >
< id name = " id " />
< property name = " name " >
< meta - data > $ {springside.name} </ meta - data >
</ property >
< property name = " descn " >
< meta - data > $ {springside.descn} </ meta - data >
</ property >
</ contract >
< class name = " Book " alias = " ${springside.book} " extends = " product " >
< property name = " author " >
< meta - data > $ {springside.author} </ meta - data >
</ property >
< property name = " publisher " >
< meta - data > $ {springside.publisher} </ meta - data >
</ property >
</ class >
</ compass - core - mapping >
3.Common Meta Data
定义Compass配置文件(*.cfg.xml). Compass 自动替换OSEM对应的原数据(Common meta-data)标签的值. ${...} 很像ant.
简单的可以理解为alias对应为表,meta-data为column.具体阐述,请见compass reference(Chapter 3. Search Engine)
<compass-core-meta-data>
<meta-data-group id="springside" displayName="SpringSide Book Meta Data">
<description>SpringSide Meta Data</description>
<uri>http://compass/springside</uri>
<alias id="book" displayName="Book">
<description>Book alias</description>
<uri>http://compass/springside/alias/book</uri>
<name>book</name>
</alias>
<meta-data id="name" displayName="Name">
<uri>http://compass/springside/name</uri>
<name>descn</name> </meta-data>
<meta-data id="descn" displayName="Description">
<uri>http://compass/springside/descn</uri>
<name>descn</name>
</meta-data>
<meta-data id="author" displayName="Author">
<description>The author of a book</description>
<uri>http://compass/springside/author</uri>
<name>author</name>
</meta-data>
<meta-data id="publisher" displayName="Publisher">
<description>The publisher of a book</description>
<uri>http://compass/springside/publisher</uri>
<name>publisher</name>
</meta-data>
</meta-data-group>
</compass-core-meta-data>
4. 与spring,hibernate集成的实现
4.1 hibernate 配置
配置通透的pojo,class所应对的hibernate配置hbm.xml,这里就不在熬述.具体可见springside源码.
4.2 spring配置
hiberante中的sessionFactory,transactionManager相比大家也是轻车熟路了.这里还是代过(因为不牵扯稿费的问题吗^_^ ).compass已经对对spring集成做了很好的封装,让我们的使用更加简单,我们可以不为compass编写一行代码,就可以做完搜索引擎的检索.下面谈一下compass在spring中的简明配置.
<beans>
<bean id="compass" class="org.compass.spring.LocalCompassBean">
<property name="resourceLocations">
<list>
<value>classpath:compass-springside.cmd.xml</value>
<value>classpath:compass-springside.cpm.xml</value>
</list>
</property>
<property name="compassSettings">
<props>
<prop key="compass.engine.connection">file://${user.home}/springside/compass</prop>
<prop key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop>
</props>
</property>
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean id="hibernateGpsDevice" class="org.compass.spring.device.hibernate.SpringHibernate3GpsDevice">
<property name="name">
<value>hibernateDevice</value>
</property>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop">
<property name="compass" ref="compass"/>
<property name="gpsDevices">
<list>
<ref local="hibernateGpsDevice"/>
</list>
</property>
</bean>
</beans>
resourceLocations: 装入compass的配置文件 *.cmd.xml,*.cpm.xml
compass.engine.connection: 就是索引做存放的本机文件的路径.搜索引擎也是根据本机文件索引,进行查询.
hibernateGpsDevice: 注入sessionFactory.
The Hibernate Gps Device:用Hibernate 3 事件系统,提高支持Real Time Data Mirroring.经Hiberante的数据改变自动被反射到索引里面.
4.3 web中的配置
<bean id="indexBookController" class="org.compass.spring.web.mvc.CompassIndexController">
<property name="compassGps">
<ref bean="compassGps"/>
</property>
<property name="indexView">
<value>/admin/indexBook.jsp</value>
</property>
<property name="indexResultsView">
<value>/admin/indexBook.jsp</value>
</property>
</bean>
<bean id="searchBookController" class="org.compass.spring.web.mvc.CompassSearchController">
<property name="compass">
<ref bean="compass"/>
</property>
<property name="searchView">
<value>/home/top.jsp</value>
</property>
<property name="searchResultsView">
<value>/home/searchBook.jsp</value>
</property>
<property name="pageSize">
<value>3</value>
</property>
</bean>
调用到indexBookController,完成索引重建,调用到searchBookController,完成搜索.
搜索页面 :
<INPUT type="text" size="20" name="query" class="search" value="">
搜索结果:
<div class="left">
<c:if test="${! empty searchResults}">
耗时: <c:out value="${searchResults.searchTime}" />ms
<c:if test="${empty searchResults.hits}">
,没有找到符合条件的图书,请确保已在管理后台建立索引。
</c:if>
<c:forEach var="hit" items="${searchResults.hits}">
<c:choose>
<c:when test="${hit.alias == 'book'}">
<p><a href="<c:url value="/bookstore.do?action=bookDetail&id=${hit.data.id}"/>">
《<c:out value="${hit.data.name}" />》 </a> <br />
作者:<c:out value="${hit.data.author}" />
出版社:<c:out value="${hit.data.publisher}" /></p>
</c:when>
</c:choose>
</c:forEach>
...