首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2+hibernate+spring+compass开展全文搜索

2012-08-31 
struts2+hibernate+spring+compass进行全文搜索今天完成了一个搜索功能,把compass整合到项目当中,做一下笔

struts2+hibernate+spring+compass进行全文搜索
今天完成了一个搜索功能,把compass整合到项目当中,做一下笔记,希望对大家有用。
1.搜索文本框

<s:form action="search.action" method="post"><input class=lst type=text name=queryString maxlength=2048 value="<s:property value='queryString'/>" title="搜索"><input type=submit name="btnG" class=lsb value="搜索图书"></s:form>

2.struts.xml配置
<action name="search" method="search">
<result name="search">searchResults.jsp</result>
</action>
3.Action层
public String search(){List results=productService.searchProducts(productVo.getQueryString());ServletActionContext.getRequest().setAttribute("searchresults", results);ServletActionContext.getRequest().setAttribute("queryString", productVo.getQueryString());return "search";}

4.service层
public List searchProducts(String queryString) {Compass compass = compassTemplate.getCompass();CompassSession session=compass.openSession();session.beginLocalTransaction();List list = new ArrayList();CompassHits hits= session.queryBuilder().queryString("name:"+queryString).toQuery().hits();for(int i=0;i<hits.length();i++){ProductInfo hit=(ProductInfo)hits.data(i);ProductInfo productInfo = this.find(hit.getId());String name_highlighter = hits.highlighter(i).fragment("name");   productInfo.setName(name_highlighter); list.add(productInfo);}session.close();return list;}

记得注入compassTemplate
5.compass配置文件
<?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"/><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>  <!--高亮显示配置start-->              <prop key="compass.engine.highlighter.default.formatter.simple.pre">               <![CDATA[<font color="red"><b>]]>              </prop>              <prop key="compass.engine.highlighter.default.formatter.simple.post">               <![CDATA[</b></font>]]>              </prop>    <!--高亮显示配置end-->    </props></property><property name="transactionManager" ref="transactionManager" /></bean><bean id="jpaGpsDevice"                       ref="entityManagerFactory" />           <property name="mirrorDataChanges">               <value>true</value>           </property>       </bean>   <!-- 同步更新索引 --><bean id="compassGps" destroy-method="stop"><property name="compass" ref="compass" /><property name="gpsDevices"><list><beanref="jpaGpsDevice" /></bean></list></property></bean><bean id="compassTemplate"ref="compass" /></bean><!-- 定时重建索引(利用quartz)或随Spring ApplicationContext启动而重建索引 --><bean id="compassIndexBuilder"ref="compassGps" /><property name="buildIndex" value="true" /><property name="lazyTime" value="10" /></bean></beans>

6.重建索引的Builder
/**
* 通过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;
    }
}

热点排行