compass和spring 集成实现实时搜索 博客分类: Spring SpringquartzluceneXML.net
程序员文章站
2024-03-08 00:01:57
...
1.增加compass2.20的jar包
2.用annotation在pojo类里面增加实现搜索的功能
3.增加一个applicationContext-compass.xml文件,配置compass
2.用annotation在pojo类里面增加实现搜索的功能
Person类 import org.compass.annotations.Index; import org.compass.annotations.Searchable; import org.compass.annotations.SearchableId; import org.compass.annotations.SearchableProperty; import org.compass.annotations.Store; @Searchable public class Person { @SearchableId private int id; //@SearchableProperty(name="name") --- 这个默认的是分词,存储 @SearchableProperty(name="name") private String name; //不分词,用下面的annotation定义 @SearchableProperty(index = Index.UN_TOKENIZED, store = Store.YES) private String sex; @SearchableProperty(name="adress") private String address; @SearchableProperty(index = Index.UN_TOKENIZED, store = Store.YES) private String duty; @SearchableProperty(index = Index.UN_TOKENIZED, store = Store.YES) private String phone; @SearchableProperty(name="description") private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getDuty() { return duty; } public void setDuty(String duty) { this.duty = duty; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
3.增加一个applicationContext-compass.xml文件,配置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" class="org.compass.annotations.config.CompassAnnotationsConfiguration"> </bean> <bean id="compass" class="org.compass.spring.LocalCompassBean"> <!-- <property name="resourceDirectoryLocations"> <list> <value>classpath:com/oa</value> </list> </property> --> <property name="connection"> <value>/lucene/indexes</value> </property> <property name="classMappings"> <list> <value>com.oa.model.Person</value> </list> </property> <property name="compassConfiguration" ref="annotationConfiguration" /> <property name="transactionManager" ref="transactionManager" /> <property name="compassSettings"> <props> <!-- 指定索引的路径 --> <prop key="compass.engine.connection">e:/compass</prop> <!-- 指定分词器 --> <prop key="compass.engine.analyzer.default.type">net.paoding.analysis.analyzer.PaodingAnalyzer</prop> <prop key="compass.transaction.factory"> org.compass.spring.transaction.SpringSyncTransactionFactory </prop> </props> </property> </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> <!-- 同步更新索引 通过compassGps--> <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="com.struts2.service.impl.CompassIndexBuilder" lazy-init="false"> <property name="compassGps" ref="compassGps" /> <property name="buildIndex" value="true" /> <property name="lazyTime" value="10" /> </bean> </beans>