spring + hibernate + struts2 + compass2.1

1导入相关包:

2.web-info中加入applicationContext-compass.xml

3.spring配置文件中加入compass支持类的引用

<bean id="productManager" class="com.v512.example.service.impl.ProductManagerImpl">

    <property name="compassTemplate" ref="compassTemplate"></property>

</bean>

4.src下加入对分词器padding文件paoding-dic-home.properties

5.在对应的pojo中加入要索引的注释

 

@Searchable

public class Test implements java.io.Serializable {

@SearchableId

    private Integer id;

@SearchableProperty(name="name")

    private String name;

@SearchableProperty(name="password")

    private String password;

@SearchableProperty(name="message")

    private String message;

6.Service中加入查询的方法,的自动生动索引的类

创建索引类

CompassIndexBuilder.java

查找的方法:

private CompassTemplate compassTemplate;

public void setCompassTemplate(CompassTemplate compassTemplate){

       this.compassTemplate=compassTemplate;

}

public List searchProducts(String queryString) {

       Compass compass = compassTemplate.getCompass();

       CompassSession session=compass.openSession();

       List list = new ArrayList();

      

       CompassHits hits= session.queryBuilder().queryString("name:"+queryString).toQuery().hits();

       System.out.println("queryString:"+queryString);

       System.out.println("hits:"+hits.getLength());

       for(int i=0;i<hits.length();i++){

           Product hit=(Product)hits.data(i);

           list.add(hit);

       }

      

       return list;

    }

 

创建索引类

CompassIndexBuilder.java

 

package com.v512.example.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;

    }

}

 

 

applicationContext-compass.xml

 

<?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>

              <!-- index path -->

              <value>classpath:com/v512</value>

             

           </list>

       </property>

       <property name="connection">

           <value>/lucene/indexes</value>

       </property>

 

 

       <property name="classMappings">

           <list>

          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值