网站快速加入全文检索Word下载.docx
《网站快速加入全文检索Word下载.docx》由会员分享,可在线阅读,更多相关《网站快速加入全文检索Word下载.docx(23页珍藏版)》请在冰点文库上搜索。
spring-framework-2.5
IKAnalyzer3.2.0Stable_bin
分词器
paoding
3、开发与实现
一、把compass-2.2.0的所有开发包加入到系统的lib目录下。
二、把spirng的开发版本从2.0升级至2.5。
三、加IKAnalyzer或paoding分词器。
IKAnalyzer分词器:
它的安装部署十分简单,将IKAnalyzer3.X.jar部署于项目的lib目录中;
IKAnalyzer.cfg.xml
与ext_stopword.dic文件放置在代码根目录(对于web项目,通常是WEB-INF/classes
目录,同hibernate、log4j等配置文件相同)下即可。
paoding分词器:
将paoding-analysis.jar部署于项目的lib,paoding-dic-home.properties加入至项目的根目录,同hibernate、log4j等配置文件相同)下即可。
四、修改实体类:
Compass的开发方式与hibernate是一样的,所以只需要对要进行检索的表进行一下修改即可,而且不会影响系统原有的功能;
在要进行检索的表的POJO类中的class前加入”@Searchable”这就表示此类具有被检索功能;
在POJO类的各个属性字段前加入搜索标志(如图)
对不同的字段进行索引说明:
只做索引,不做检索
@SearchableProperty(name="
htmUrl"
store=Store.NO,index=Idex.UN_TOKENIZED)
@Temporal(TemporalType.TIMESTAMP)
@Column(name="
HTMURL"
)
privateStringkeyWork;
使用“htmlPropertyConverter“这个类来对content这个属性所代表的内容进行过滤HTML代码
content"
index=Index.TOKENIZED,store=Store.YES,converter="
htmlPropertyConverter"
@Lob
CONTENT"
privateStringcontent;
format="
yyyy-MM-ddHH:
mm:
ss这个写法要与数据库的类型一致,否则出错
@SearchableProperty(index=Index.NOT_ANALYZED,store=Store.YES,format="
ss"
privateDatecreateTime;
五、加入compass配置文件
<
?
xmlversion="
1.0"
encoding="
UTF-8"
>
beansxmlns="
http:
//www.springframework.org/schema/beans"
xmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
aop="
//www.springframework.org/schema/aop"
tx="
//www.springframework.org/schema/tx"
context="
//www.springframework.org/schema/context"
xsi:
schemaLocation="
http:
//www.springframework.org/schema/beans
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
//www.springframework.org/schema/tx
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="
true"
<
beanid="
annotationConfiguration"
class="
pass.annotations.config.CompassAnnotationsConfiguration"
/bean>
compass"
class="
pass.spring.LocalCompassBean"
<
propertyname="
resourceDirectoryLocations"
<
list>
!
--指定索引存放的位置-->
<
value>
classpath:
com<
/value>
/list>
/property>
connection"
/lucene/indexes<
classMappings"
--指定要进行搜索的POJO类-->
.contract.entity.ns.Compass_View<
compassConfiguration"
ref="
/>
compassSettings"
props>
--指定摘要文本的长度-->
prop
key="
compass.engine.highlighter.default.fragmenter.simple.size"
125
/prop>
propkey="
compass.transaction.factory"
pass.spring.transaction.SpringSyncTransactionFactory
--指定摘要文本的长度用IKAnalyzer分词方法
<
compass.engine.analyzer.default.type"
org.wltea.analyzer.lucene.IKAnalyzer<
-->
--指定摘要文本的长度用PaodingAnalyzer分词方法
compass.engine.analyzer.MMAnalyzer.CustomAnalyzer"
net.paoding.analysis.analyzer.PaodingAnalyzer
org.mira.lucene.analysis.IK_CAnalyzer
--高亮显示配置start-->
compass.engine.highlighter.default.formatter.simple.pre"
[CDATA[<
spanstyle='
color:
red;
'
]]>
compass.engine.highlighter.default.formatter.simple.post"
/span>
--高亮显示配置end-->
/props>
transactionManager"
ref="
convertersByName"
map>
entrykey="
bean
class="
pass.impl.HtmlPropertyConverter"
/entry>
/map>
hibernateGpsDevice"
pass.gps.device.hibernate.HibernateGpsDevice"
name"
hibernateDevice<
sessionFactory"
mirrorDataChanges"
true<
/beans>
说明:
1、
2、指定要进行搜索的POJO类一定要与<
在同一行
3、
4、
--指定用IKAnalyzer分词方法、
org.mira.lucene.analysis.IK_CAnalyzer>
5、
六、建立compass定时生成策略
GBK"
--配置索引策略-->
--定时重建索引(利用quartz)或随SpringApplicationContext启动而重建索引-->
TimePrinter"
pass.impl.TimePrinter"
lazy-init="
compassGps"
buildIndex"
${.centforsoft.refinework.bpm.creatCompass.begin}<
--包装任务调度Bean-->
jobTask"
org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
--定义目标Bean-->
targetObject"
--定义目标Bean中的方法-->
targetMethod"
value="
run"
--配置触发器-->
cronTrigger"
org.springframework.scheduling.quartz.CronTriggerBean"
--确定任务调度的目标任务-->
jobDetail"
--每周周一至周五凌晨4:
30执行一次重建索引这里读的是配置文件里的时间-->
cronExpression"
${.centforsoft.refinework.bpm.creatCompass.upTime}<
--配置启动触发器-->
scheduler"
org.springframework.scheduling.quartz.SchedulerFactoryBean"
--指定触发器列表-->
triggers"
refbean="
七、加入核心的搜索构造类
packagepass.impl;
importjava.util.*;
importpass.gps.CompassGps;
importorg.springframework.beans.factory.InitializingBean;
publicclassTimePrinterextendsThreadimplementsInitializingBean{
privatebooleanbuildIndex=true;
privateCompassGpscompassGps;
//索引操作线程延时启动的时间,单位为秒
privateintlazyTime=10;
publicvoidrun(){
try{
Thread.sleep(lazyTime*100);
System.out.println("
开始重建索引-->
线程开始..."
);
longbeginTime=System.currentTimeMillis();
//重建索引.
//如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引,
//索引完成后再进行覆盖.
compassGps.index();
longcostTime=System.currentTimeMillis()-beginTime;
完成重建索引-->
线程结束.用时"
+costTime+"
milliseconds"
);
}catch(InterruptedExceptione){
//simplyproceed
}
}
publicCompassGpsgetCompassGps(){
returncompassGps;
publicvoidsetCompassGps(CompassGpscompassGps){
passGps=compassGps;
//如果buildIndex=ture这个构造类就会生效
publicvoidafterPropertiesSet()throwsException{
if(buildIndex){
run();
publicbooleanisBuildIndex(){
returnbuildIndex;
publicvoidsetBuildIndex(booleanbuildIndex){
this.buildIndex=buildIndex;
}
八、加入索引搜索类
publicHashMapsearchNsArticle(Pagepage,StringqueryString,intselectType){
CompassSessionsession=null;
CompassTransactiontx=null;
HashMaphashMap=null;
List<
Compass_View>
list=null;
init();
hashMap=newHashMap();
CompassHitshits=null;
Compasscompass=compassTemplate.getCompass();
session=compass.openSession();
tx=session.beginTransaction();
//按相似度查询
if(selectType=