compass和spring 集成實現實時搜索

1.增加compass2.20的jar包 
2.用annotation在pojo類裏面增加實現搜索的功能 
Java代碼  收藏代碼
  1. Person類      
  2. import org.compass.annotations.Index;      
  3. import org.compass.annotations.Searchable;      
  4. import org.compass.annotations.SearchableId;      
  5. import org.compass.annotations.SearchableProperty;      
  6. import org.compass.annotations.Store;      
  7.      
  8.      
  9. @Searchable     
  10. public class Person {      
  11.     @SearchableId     
  12.     private int id;      
  13.     //@SearchableProperty(name="name") --- 這個默認的是分詞,存儲      
  14.     @SearchableProperty(name="name")      
  15.     private String name;      
  16.     //不分詞,用下面的annotation定義      
  17.     @SearchableProperty(index = Index.UN_TOKENIZED, store = Store.YES)      
  18.     private String sex;      
  19.     @SearchableProperty(name="adress")      
  20.     private String address;      
  21.     @SearchableProperty(index = Index.UN_TOKENIZED, store = Store.YES)      
  22.     private String duty;      
  23.     @SearchableProperty(index = Index.UN_TOKENIZED, store = Store.YES)      
  24.     private String phone;      
  25.     @SearchableProperty(name="description")      
  26.     private String description;      
  27.      
  28.     public int getId() {      
  29.         return id;      
  30.      }      
  31.      
  32.     public void setId(int id) {      
  33.         this.id = id;      
  34.      }      
  35.      
  36.     public String getName() {      
  37.         return name;      
  38.      }      
  39.      
  40.     public void setName(String name) {      
  41.         this.name = name;      
  42.      }      
  43.      
  44.     public String getSex() {      
  45.         return sex;      
  46.      }      
  47.      
  48.     public void setSex(String sex) {      
  49.         this.sex = sex;      
  50.      }      
  51.      
  52.     public String getAddress() {      
  53.         return address;      
  54.      }      
  55.      
  56.     public void setAddress(String address) {      
  57.         this.address = address;      
  58.      }      
  59.      
  60.     public String getDuty() {      
  61.         return duty;      
  62.      }      
  63.      
  64.     public void setDuty(String duty) {      
  65.         this.duty = duty;      
  66.      }      
  67.      
  68.     public String getPhone() {      
  69.         return phone;      
  70.      }      
  71.      
  72.     public void setPhone(String phone) {      
  73.         this.phone = phone;      
  74.      }      
  75.      
  76.     public String getDescription() {      
  77.         return description;      
  78.      }      
  79.      
  80.     public void setDescription(String description) {      
  81.         this.description = description;      
  82.      }      
  83.      
  84. }     


3.增加一個applicationContext-compass.xml文件,配置compass 

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>      
  2.      
  3. <beans xmlns="http://www.springframework.org/schema/beans"     
  4.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  5.      xsi:schemaLocation=" http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"     
  6.     default-lazy-init="true">      
  7.      
  8.      
  9.      <bean id="annotationConfiguration"     
  10.         class="org.compass.annotations.config.CompassAnnotationsConfiguration">      
  11.      </bean>      
  12.      
  13.      
  14.      <bean id="compass" class="org.compass.spring.LocalCompassBean">      
  15.          <!--      
  16.          <property name="resourceDirectoryLocations">      
  17.              <list>      
  18.                  <value>classpath:com/oa</value>      
  19.              </list>      
  20.          </property>      
  21.               
  22.      
  23.          -->      
  24.          <property name="connection">      
  25.              <value>/lucene/indexes</value>      
  26.          </property>      
  27.               
  28.          <property name="classMappings">      
  29.              <list>      
  30.                  <value>com.oa.model.Person</value>      
  31.              </list>      
  32.          </property>      
  33.               
  34.          <property name="compassConfiguration" ref="annotationConfiguration" />      
  35.               
  36.          <property name="transactionManager" ref="transactionManager" />      
  37.               
  38.          <property name="compassSettings">      
  39.              <props>      
  40.                  <!-- 指定索引的路徑 -->      
  41.                  <prop key="compass.engine.connection">e:/compass</prop>      
  42.                  <!-- 指定分詞器 -->      
  43.                  <prop key="compass.engine.analyzer.default.type">net.paoding.analysis.analyzer.PaodingAnalyzer</prop>      
  44.                  <prop key="compass.transaction.factory">      
  45.                      org.compass.spring.transaction.SpringSyncTransactionFactory      
  46.                  </prop>      
  47.                         
  48.              </props>      
  49.          </property>      
  50.               
  51.      </bean>      
  52.      
  53.      
  54.      <bean id="hibernateGpsDevice"     
  55.         class="org.compass.gps.device.hibernate.HibernateGpsDevice">      
  56.          <property name="name">      
  57.              <value>hibernateDevice</value>      
  58.          </property>      
  59.          <property name="sessionFactory" ref="sessionFactory" />      
  60.          <property name="mirrorDataChanges">      
  61.              <value>true</value>      
  62.          </property>      
  63.      </bean>      
  64.      <!-- 同步更新索引 通過compassGps-->      
  65.      <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps"     
  66.          init-method="start" destroy-method="stop">      
  67.          <property name="compass" ref="compass" />      
  68.          <property name="gpsDevices">      
  69.              <list>      
  70.                  <bean      
  71.                     class="org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper">      
  72.                      <property name="gpsDevice" ref="hibernateGpsDevice" />      
  73.                  </bean>      
  74.              </list>      
  75.          </property>      
  76.      </bean>      
  77.      
  78.      
  79.      <bean id="compassTemplate"     
  80.         class="org.compass.core.CompassTemplate">      
  81.          <property name="compass" ref="compass" />      
  82.      </bean>      
  83.      
  84.      <!-- 定時重建索引(利用quartz)或隨Spring ApplicationContext啓動而重建索引 -->      
  85.      <bean id="compassIndexBuilder"     
  86.         class="com.struts2.service.impl.CompassIndexBuilder"     
  87.          lazy-init="false">      
  88.          <property name="compassGps" ref="compassGps" />      
  89.          <property name="buildIndex" value="true" />      
  90.          <property name="lazyTime" value="10" />      
  91.      </bean>      
  92.           
  93.      
  94. </beans>     
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章