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#a12608860
Sent from the AppFuse - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to