struts2+spring+hibernate+compass 实现全文检索

Compass是一流的开放源码JAVA搜索引擎框架,对于你的应用修饰,搜索引擎语义更具有能力。依靠顶级的Lucene搜索引擎,Compass 结合了,像 Hibernate和 Sprin的流行的框架,为你的应用提供了从数据模型和数据源同步改变的搜索力.并且添加了2方面的特征,事物管理和快速更新优化. Compass的目标是:把java应用简单集成到搜索引擎中.编码更少,查找数据更便捷。 

    这里struts2整合spring、hibernate就不说了。贴出compass+spring+hibernate整合的关键代码。 

     1、对要检索的实体进行可搜索注解: 
Java代码 
@Searchable  
public  class Product  implements java.io.Serializable {   
  
     @SearchableId  
     private Integer id;   
     @SearchableProperty(name= "name")   
     private String name;   
     @SearchableProperty(name= "price")   
     private Float price;   
     @SearchableProperty(name= "brand")   
     private String brand;   
     @SearchableProperty(name= "description")   
     private String description;   
  
         //getter setter   
}  
java代码 
package cn.changtusoft.s2sh_compass.service.impl;   
import org.compass.gps.CompassGps;   
import org.springframework.beans.factory.InitializingBean;   
  
  
/**  
 * 通过quartz定时调度定时重建索引或自动随Spring ApplicationContext启动而重建索引的Builder.  
 * 会启动后延时数秒新开线程调用compassGps.index()函数.  
 * 默认会在Web应用每次启动时重建索引,可以设置buildIndex属性为false来禁止此功能.  
 * 也可以不用本Builder, 编写手动调用compassGps.index()的代码.  
 *  
 */  
public  class CompassIndexBuilder  implements InitializingBean {      
     // 是否需要建立索引,可被设置为false使本Builder失效.   
     private  boolean buildIndex =  false;   
  
     // 索引操作线程延时启动的时间,单位为秒   
     private  int lazyTime =  10;   
  
     // Compass封装   
     private CompassGps compassGps;   
  
     // 索引线程   
     private Thread indexThread =  new Thread() {   
  
         @Override  
         public  void run() {   
             try {   
                Thread.sleep(lazyTime *  1000);   
                System.out.println( "begin compass index...");   
                 long beginTime = System.currentTimeMillis();   
                 // 重建索引.   
                 // 如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引,   
                 // 索引完成后再进行覆盖.   
                compassGps.index();   
                 long costTime = System.currentTimeMillis() - beginTime;   
                System.out.println( "compss index finished.");   
                System.out.println( "costed " + costTime +  " milliseconds");   
            }  catch (InterruptedException e) {   
                e.printStackTrace();   
            }   
        }   
    };   
  
     /**  
     * 实现<code>InitializingBean</code>接口,在完成注入后调用启动索引线程.  
     *  
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()  
     */  
     public  void afterPropertiesSet()  throws Exception {   
         if (buildIndex) {   
            indexThread.setDaemon( true);   
            indexThread.setName( "Compass Indexer");   
            indexThread.start();   
        }   
    }   
  
     public  void setBuildIndex( boolean buildIndex) {   
         this.buildIndex = buildIndex;   
    }   
  
     public  void setLazyTime( int lazyTime) {   
         this.lazyTime = lazyTime;   
    }   
  
     public  void setCompassGps(CompassGps compassGps) {   
         this.compassGps = compassGps;   
    }   
}  
      3、Manager 
Java代码 
package cn.changtusoft.s2sh_compass.service.impl;   
  
import java.util.ArrayList;   
import java.util.List;   
  
import org.compass.core.Compass;   
import org.compass.core.CompassHits;   
import org.compass.core.CompassSession;   
import org.compass.core.CompassTemplate;   
import org.compass.core.CompassTransaction;   
  
import cn.changtusoft.s2sh_compass.dao.ProductDao;   
import cn.changtusoft.s2sh_compass.model.Product;   
import cn.changtusoft.s2sh_compass.service.ProductManager;   
  
public  class ProductManagerImpl  implements ProductManager {   
  
     private ProductDao productDao;   
       
     private CompassTemplate compassTemplate;   
       
     @Override  
     public List searchProducts(String description) {   
        List productList =  new ArrayList();   
           
        Compass compass = compassTemplate.getCompass();   
        CompassSession session = compass.openSession();   
           
        CompassTransaction tx =  null;   
           
        tx = session.beginTransaction();   
        CompassHits hits = session.queryBuilder().queryString( "brand:"+description).toQuery().hits();   
         for ( int i =  0; i < hits.length(); i++) {   
            Product p = (Product)hits.data(i);   
            productList.add(p);   
        }   
           
        tx.commit();   
           
         return productList;   
    }   
  
     /* setter */  
     public  void setProductDao(ProductDao productDao) {   
         this.productDao = productDao;   
    }   
     public  void setCompassTemplate(CompassTemplate compassTemplate) {   
         this.compassTemplate = compassTemplate;   
    }   
  
}  
        4、compass配置applicationContext-compass.xml 
Java代码 
<?xml version= "1.0" encoding= "UTF-8"?>   
  
<beans xmlns= "http://www.springframework.org/schema/beans"  
    xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation= " http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"  
     default-lazy-init= "true">   
  
  
    <bean id= "annotationConfiguration"  
         class= "org.compass.annotations.config.CompassAnnotationsConfiguration">   
    </bean>   
  
  
    <bean id= "compass"  class= "org.compass.spring.LocalCompassBean">   
        <property name= "resourceDirectoryLocations">   
            <list>   
                <value>classpath:cn/changtusoft</value>   
            </list>   
        </property>   
        <property name= "connection">   
            <value>/lucene/indexes</value>   
        </property>   
  
  
        <property name= "classMappings">   
            <list>   
                <value>cn.changtusoft.s2sh_compass.model.Product</value>   
            </list>   
        </property>   
        <property name= "compassConfiguration"  
            ref= "annotationConfiguration" />   
  
        <property name= "compassSettings">   
            <props>   
                <prop key= "compass.transaction.factory">   
                    org.compass.spring.transaction.SpringSyncTransactionFactory   
                </prop>   
                  <prop key= "compass.engine.analyzer.MMAnalyzer.CustomAnalyzer">net.paoding.analysis.analyzer.PaodingAnalyzer </prop>   
            </props>   
        </property>   
  
        <property name= "transactionManager" ref= "transactionManager" />   
    </bean>   
  
  
    <bean id= "hibernateGpsDevice"  
         class= "org.compass.gps.device.hibernate.HibernateGpsDevice">   
        <property name= "name">   
            <value>hibernateDevice</value>   
        </property>   
        <property name= "sessionFactory" ref= "sessionFactory" />   
        <property name= "mirrorDataChanges">   
            <value> true</value>   
        </property>   
    </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>   
                <bean   
                     class= "org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper">   
                    <property name= "gpsDevice" ref= "hibernateGpsDevice" />   
                </bean>   
            </list>   
        </property>   
    </bean>   
  
  
    <bean id= "compassTemplate"  
         class= "org.compass.core.CompassTemplate">   
        <property name= "compass" ref= "compass" />   
    </bean>   
  
    <!-- 定时重建索引(利用quartz)或随Spring ApplicationContext启动而重建索引 -->   
    <bean id= "compassIndexBuilder"  
         class= "cn.changtusoft.s2sh_compass.service.impl.CompassIndexBuilder"  
        lazy-init= "false">   
        <property name= "compassGps" ref= "compassGps" />   
        <property name= "buildIndex" value= "true" />   
        <property name= "lazyTime" value= "10" />   
    </bean>   
        
</beans>  
       5、因为本实例使用的是庖丁分词,所以添加它的配置文件到src下(paoding-dic-home.properties) 
Java代码 
paoding.dic.home=c:/paoding/dic   
paoding.dic.detector.interval= 60  
paoding.dic.home=c:/paoding/dicpaoding.dic.detector.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值