1. Use of org.compass.spring.device.hibernate.dep.SpringHibernate3GpsDevice
is now deprecated in favor of
org.compass.gps.device.hibernate.HibernateGpsDevice. This requires a
org.compass.spring.device.hibernate.SpringNativeHibernateExtractor to be
injected (see applicationContext-service.xml, below.)
2. Most users add search terms to a search in order to narrow the search.
But, by default, Compass widens the search by ORing the search terms
together. To make searches more googlesque, I have created a
CompassSearchHelper subclass (AndDefaultCompassSearchHelper) that causes
search terms to be ANDed.
First, the new AndDefaultCompassSearchHelper:
package com.firstworldtoys.util;
import org.compass.core.Compass;
import org.compass.core.CompassQuery;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassSession;
import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchHelper;
public class AndDefaultCompassSearchHelper extends CompassSearchHelper
{
public AndDefaultCompassSearchHelper(Compass compass)
{
super(compass);
}
public AndDefaultCompassSearchHelper(Compass compass, Integer pageSize)
{
super(compass, pageSize);
}
protected CompassQuery buildQuery(CompassSearchCommand searchCommand,
CompassSession session)
{
CompassQueryBuilder queryBuilder = session.queryBuilder();
return
queryBuilder.queryString(searchCommand.getQuery()).useAndDefaultOperator().toQuery();
}
}
Second, updated XML for your dispatcher-servlet.xml file:
<!-- ==== COMPASS SEARCH CONTROLLER ==== -->
<bean id="andDefaultCompassSearchHelper"
class="com.firstworldtoys.util.AndDefaultCompassSearchHelper">
<constructor-arg index="0">
<ref bean="compass"/>
</constructor-arg>
<constructor-arg index="1">
<value>10</value>
</constructor-arg>
</bean>
<bean id="searchController"
class="org.compass.spring.web.mvc.CompassSearchController">
<property name="searchHelper" ref="andDefaultCompassSearchHelper"/>
<property name="compass">
<ref bean="compass" />
</property>
<property name="searchView">
<value>searchView</value>
</property>
<property name="searchResultsView">
<value>searchResultsView</value>
</property>
<property name="pageSize">
<value>10</value>
</property>
</bean>
<!-- ==== COMPASS INDEX CONTROLLER ==== -->
<bean id="indexController"
class="org.compass.spring.web.mvc.CompassIndexController">
<property name="compassGps">
<ref bean="compassGps" />
</property>
<property name="indexView">
<value>reindexView</value>
</property>
<property name="indexResultsView">
<value>reindexResultsView</value>
</property>
</bean>
Third, updated XML for your applicationContext-service.xml file:
<!-- COMPASS START -->
<bean id="compass" class="org.compass.spring.LocalCompassBean">
<property name="classMappings">
<list>
<value>com.firstworldtoys.model.Toy</value>
</list>
</property>
<property name="compassSettings">
<props>
<prop
key="compass.engine.connection">file:///Users/gregederer/dev/fwt/compass</prop>
<prop
key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop>
<!-- snowball analyzer provides stemming (so 'monkey' gets monkeys,
too) -->
<prop key="compass.engine.analyzer.default.type">snowball</prop>
<prop key="compass.engine.analyzer.default.name">English</prop>
</props>
</property>
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="hibernateGpsDevice"
class="org.compass.gps.device.hibernate.HibernateGpsDevice">
<property name="name" value="hibernateDevice" />
<property name="sessionFactory" ref="sessionFactory" />
<property name="nativeExtractor">
<bean
class="org.compass.spring.device.hibernate.SpringNativeHibernateExtractor"
/>
</property>
</bean>
<bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps"
init-method="start" destroy-method="stop">
<property name="compass">
<ref bean="compass" />
</property>
<property name="gpsDevices">
<list>
<bean
class="org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper">
<property name="gpsDevice" ref="hibernateGpsDevice" />
</bean>
</list>
</property>
</bean>
<!-- COMPASS END -->
Finally, here's the search JSP:
<%@ include file="/common/taglibs.jsp"%>
<html>
<head>
<title>Search</title>
</head>
<body>
Search:
<form method="GET">
<spring:bind path="command.query">
<input id="query" type="text" name="query" value="<c:out
value="${status.value}"/>" />
</spring:bind>
<p>
<input type="submit" value="Search" />
</p>
</form>
<c:if test="${! empty searchResults}">
<p>Search took <c:out value="${searchResults.searchTime}" /> ms</p>
<table border="2">
<tr>
<th>SCORE</th>
<th>TYPE</th>
<th>ID</th>
</tr>
<c:forEach var="hit" items="${searchResults.hits}">
<tr>
<td><fmt:formatNumber type="percent" value="${hit.score}"
/></td>
<td><c:out value="${hit.alias}" /></td>
<td><c:out value="${hit.data.id}" /></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
Cheers,
Greg
gederer wrote:
>
> Hi,
>
> I've been messing about with Compass and AppFuse 2.0. The below borrows
> heavily from Chris Barham's previous post on the subject. But, it uses the
> most recent version of Compass, Compass annotations, and, of course,
> AppFuse 2.
>
> Comments welcome!
>
> Cheers,
>
> Greg
>
>
> Setting Up Compass with AppFuse 2
> ==================
>
> 1. Download Compass with dependencies:
>
> http://www.opensymphony.com/compass/download.action
>
> 2. Add Compass jars to local m2 repo
>
> Compass does not currently have a Maven2 repository (that I could
> find). So,
> you'll need to install compass in your local repository by hand. Use
> the
> following command to install the compass jars locally:
>
> mvn install:install-file
> -Dfile=/tmp/compass-1.2GA-SNAPSHOT/dist/compass.jar -DgroupId=opensymphony
> -DartifactId=compass -Dversion=1.2GA-SNAPSHOT -Dpackaging=jar
>
> 3. Add Compass dependencies to pom.xml
>
> Under <dependencies>
>
> <dependency>
> <groupId>opensymphony</groupId>
> <artifactId>compass</artifactId>
> <version>${compass.version}</version>
> </dependency>
> <dependency>
> <groupId>org.apache.lucene</groupId>
> <artifactId>lucene-core</artifactId>
> <version>${lucene.version}</version>
> </dependency>
> <dependency>
> <groupId>org.apache.lucene</groupId>
> <artifactId>lucene-analyzers</artifactId>
> <version>${lucene.version}</version>
> </dependency>
> <dependency>
> <groupId>org.apache.lucene</groupId>
> <artifactId>lucene-highlighter</artifactId>
> <version>${lucene.version}</version>
> </dependency>
> <dependency>
> <groupId>org.apache.lucene</groupId>
> <artifactId>lucene-queries</artifactId>
> <version>${lucene.version}</version>
> </dependency>
> <dependency>
> <groupId>org.apache.lucene</groupId>
> <artifactId>lucene-snowball</artifactId>
> <version>${lucene.version}</version>
> </dependency>
>
> Under <properties>
>
> <compass.version>1.2GA-SNAPSHOT</compass.version>
> <lucene.version>2.2.0</lucene.version>
>
> 4. Annotate your POJO
>
> @Entity
> @Table(name="toy")
> @Searchable(alias="toy")
> public class Toy extends BaseObject
> {
> private static final long serialVersionUID =
> -2935222440301604395L;
>
> @Id
> @GeneratedValue(strategy = GenerationType.AUTO)
> @SearchableId
> private Long id;
>
> @SearchableProperty(name = "name")
> private String name;
>
> @Column(columnDefinition="text")
> @SearchableProperty(name="description")
> private String description;
>
> ...
> }
>
> See http://docs.codehaus.org/display/TRAILS/Using+search for more
> details on Compass annotations.
>
> 5. Add Compass Spring beans to applicationContext-service.xml:
>
> <!-- COMPASS START -->
> <bean id="compass" class="org.compass.spring.LocalCompassBean">
> <property name="classMappings">
> <list>
> <!-- Add POJOS here -->
> <value>com.firstworldtoys.model.Toy</value>
> </list>
> </property>
> <property name="compassSettings">
> <props>
> <prop
> key="compass.engine.connection">file:///Users/gregederer/dev/fwt/compass</prop>
> <prop
> key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop>
> <!-- snowball analyzer provides stemming (so 'monkey' gets
> 'monkeys', too) -->
> <prop
> key="compass.engine.analyzer.default.type">snowball</prop>
> <prop
> key="compass.engine.analyzer.default.name">English</prop>
> </props>
> </property>
> <property name="transactionManager" ref="transactionManager" />
> </bean>
>
> <bean id="hibernateGpsDevice"
> class="org.compass.spring.device.hibernate.dep.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 bean="compass" />
> </property>
> <property name="gpsDevices">
> <list>
> <bean
> class="org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper">
> <property name="gpsDevice" ref="hibernateGpsDevice" />
> </bean>
> </list>
> </property>
> </bean>
> <!-- COMPASS END -->
>
> 6. Add JSP for Compass rebuild index
>
> <%@ include file="/common/taglibs.jsp"%>
> <P>
> <H2>Compass Index</H2>
> <P>Use the Index button to index the database using Compass::Gps.
> The operation will delete the current index and reindex the database
> based on the mappings and devices defined in the Compass::Gps
> configuration context.
> <FORM method="POST" action="<c:url
> value="/reindex.html"/>"><spring:bind
> path="command.doIndex">
> <INPUT type="hidden" name="doIndex" value="true" />
> </spring:bind> <INPUT type="submit" value="Index" /></FORM>
> <c:if test="${! empty indexResults}">
> <P>Indexing took: <c:out value="${indexResults.indexTime}" />ms.
>
> </c:if>
> <P>
>
> 7. Add JSP for Compass search
>
> <%@ include file="/common/taglibs.jsp"%>
> <title>Search</title>
> Search:
> <form method="GET">
> <p>
> <spring:bind path="command.query">
> <INPUT type="text" size="20" name="query" value="<c:out
> value="${status.value}"/>" />
> </spring:bind>
> </p>
> <p>
> <input type="submit" value="Search" />
> </p>
> </form>
> <c:if test="${! empty searchResults}">
> <p>Search took <c:out value="${searchResults.searchTime}" />
> ms</p>
> <table border="2">
> <tr>
> <th>SCORE</th>
> <th>TYPE</th>
> <th>NAME</th>
> <th>ID</th>
> <th>DESCRIPTION</th>
> </tr>
> <c:forEach var="hit" items="${searchResults.hits}">
> <tr>
> <td><fmt:formatNumber type="percent" value="${hit.score}"
> /></td>
> <td><c:out value="${hit.alias}" /></td>
> <td><c:out value="${hit.data.name}" /></td>
> <td><c:out value="${hit.data.id}" /></td>
> <td><c:out value="${hit.data.description}" /></td>
> </tr>
> </c:forEach>
> </table>
> </c:if>
>
> 8. Add Compass controllers to dispatcher-servlet.xml
>
> <!-- ==== COMPASS SEARCH CONTROLLER ==== -->
> <bean id="searchController"
> class="org.compass.spring.web.mvc.CompassSearchController">
> <property name="compass">
> <ref bean="compass" />
> </property>
> <property name="searchView">
> <value>searchView</value>
> </property>
> <property name="searchResultsView">
> <value>searchResultsView</value>
> </property>
> <property name="pageSize">
> <value>10</value>
> </property>
> </bean>
> <!-- ==== COMPASS INDEX CONTROLLER ==== -->
> <bean id="indexController"
> class="org.compass.spring.web.mvc.CompassIndexController">
> <property name="compassGps">
> <ref bean="compassGps" />
> </property>
> <property name="indexView">
> <value>reindexView</value>
> </property>
> <property name="indexResultsView">
> <value>reindexResultsView</value>
> </property>
> </bean>
>
> 9. Add URL mappings to dispatcher-servlet.xml
>
> <bean id="urlMapping"
> class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
> <property name="mappings">
> <value>
> ...
> /search.html=searchController
> /reindex.html=indexController
> </value>
> </property>
> <property name="order" value="0" />
> </bean>
>
> 10. Add ResourceBundleViewResolver to dispatcher-servlet.xml
>
> <bean id="rbViewResolver"
>
> class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
> <property name="basename"><value>views</value></property>
> <property name="order"><value>0</value></property>
> </bean>
>
> 11. Create a views.properties file in src/main/resources with the
> following contents
>
> searchView.class=org.springframework.web.servlet.view.JstlView
> searchView.url=/WEB-INF/pages/search.jsp
>
>
> searchResultsView.class=org.springframework.web.servlet.view.JstlView
> searchResultsView.url=/WEB-INF/pages/search.jsp
>
> reindexView.class=org.springframework.web.servlet.view.JstlView
> reindexView.url=/WEB-INF/pages/reindex.jsp
>
>
> reindexResultsView.class=org.springframework.web.servlet.view.JstlView
> reindexResultsView.url=/WEB-INF/pages/reindex.jsp
>
--
View this message in context:
http://www.nabble.com/Mini-Tutorial%3A-Compass-and-AppFuse-2.0-tf4420567s2369.html#a12640692
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]