Compass java
1.概述
網站: http://www.compass-project.org/web
Compass是一流的開放源碼JAVA搜索引擎框架,對於你的應用修飾,搜索引擎語義更具備能力。依靠頂級的Lucene搜索引擎,Compass 結合了,像 Hibernate和 Sprin的流行的框架,爲你的應用提供了從數據模型和數據源同步改變的搜索力.而且添加了2方面的特徵,事物管理和快速更新優化. Compass的目標是:把java應用簡單集成到搜索引擎中.編碼更少,查找數據更便捷 .spring
2.Object/Search Engine Mapping - 配置XML
OSEM: 對象搜索引擎影影射(Object Search Engine Mapping),經過xml配置文件,提供了POJO's (Plain Old Java Objects)到搜索引擎. session
2.1 XML配置
通常都是 *.cpm.xml 形式 . app
1) package指定了對應java類的包名,
2)class爲pojo類名.contract爲公共部分, 子類被能夠extends.Property 對 java類的屬性。
3)資源的Property引用的Search Engine meta-data.
注意: id爲類標識. 框架
<compass-core-mapping package="org.springside.bookstore.domain"> <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)dom
<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源碼. jsp
4.2 spring配置
hiberante中的sessionFactory,transactionManager相比你們也是輕車熟路了.這裏仍是代過(由於不牽扯稿費的問題嗎^_^ ).compass已經對對spring集成作了很好的封裝,讓咱們的使用更加簡單,咱們能夠不爲compass編寫一行代碼,就能夠作完搜索引擎的檢索.下面談一下compass在spring中的簡明配置. ide
<beans>
<bean id="compass" >
<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" >
<property name="name">
<value>hibernateDevice</value>
</property>
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <bean id="compassGps" 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" > <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" > <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" value="">
搜索結果:
<div ><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>