网站快速加入全文检索.docx
《网站快速加入全文检索.docx》由会员分享,可在线阅读,更多相关《网站快速加入全文检索.docx(23页珍藏版)》请在冰豆网上搜索。
![网站快速加入全文检索.docx](https://file1.bdocx.com/fileroot1/2023-2/28/16d9d306-8b34-46e1-b1ea-43e1ebecda8c/16d9d306-8b34-46e1-b1ea-43e1ebecda8c1.gif)
网站快速加入全文检索
网站快速加入全文检索功能
前言:
本文档“网站快速加入全文功能”是在系统的前期开发工作基本上已经完成,技术的架构不发生重在修改的情况下加入的,加入全文检索功能后,网站的搜索功能将十分强大,搜索效率高,搜索用时不到一杪。
效果不错;我们的网站使用的是SSH架构,而compass(指南针)是基于JAVA的Compass开放源码的搜索引擎框架,依靠顶级的Lucene搜索引擎,Compass结合了,像Hibernate和Sprin的流行的框架,.编码更少,查找数据更便捷。
1、compass基础知识入门
compass的模块只要包括:
compasscore,CompassGps,CompassSpring等。
一:
compasscore是compass最基础的部分,它对lucene做了事务索引、搜索引擎抽象、对象关系映射、事务管理集成、不同的影射技术(OSEM,XSEM,RSEM)等扩展。
它的目标是应用于不同的场景和环境,简化搜索引擎的核心操作。
二:
CompassGps的目标是继承不同的内容来源,最主要的特征是和不同的ORM框架,比如Hibernate,JPA,JDO,OJB等的集成,允许在搜索引擎和一种以ORM方式从数据库中取内容的持久框架进行透明方式的集成。
除ORM框架外,还支持与JDBC的集成,允许通过配置SQL表达式抽取内容达到索引数据库内容的目的。
三:
CompassSpring主要完成Compass和Springframework的集成。
spring是一个基于依赖注射和其它技术提供简化开发模式的应用框架。
compass在springframework的代码基础上以和集成ORM框架相同简单的方式集成spring,而且也集成了Spring抽象事务层、AOP支持以及MVC库。
2、技术架构及环境准备软件的准备
1、MyEclipse6.0
2、Tomcat6:
3、compass包:
在开发过程中要用到以下几的包
包名
作用
compass-2.2.0的开发包
基本
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代码
@SearchableProperty(name="content",index=Index.TOKENIZED,store=Store.YES,converter="htmlPropertyConverter")
@Lob
@Column(name="CONTENT")
privateStringcontent;
format="yyyy-MM-ddHH:
mm:
ss这个写法要与数据库的类型一致,否则出错
@SearchableProperty(index=Index.NOT_ANALYZED,store=Store.YES,format="yyyy-MM-ddHH:
mm:
ss")
privateDatecreateTime;
五、加入compass配置文件
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xmlns:
context="http:
//www.springframework.org/schema/context"
xsi:
schemaLocation="
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
class="pass.annotations.config.CompassAnnotationsConfiguration">
--指定索引存放的位置-->
classpath:
com
/lucene/indexes
--指定要进行搜索的POJO类-->
.contract.entity.ns.Compass_View
ref="annotationConfiguration"/>
--指定摘要文本的长度-->
key="compass.engine.highlighter.default.fragmenter.simple.size">
125
pass.spring.transaction.SpringSyncTransactionFactory
--指定摘要文本的长度用IKAnalyzer分词方法
org.wltea.analyzer.lucene.IKAnalyzer-->
--指定摘要文本的长度用PaodingAnalyzer分词方法
key="compass.engine.analyzer.MMAnalyzer.CustomAnalyzer">
net.paoding.analysis.analyzer.PaodingAnalyzer
-->
key="compass.engine.analyzer.MMAnalyzer.CustomAnalyzer">
org.mira.lucene.analysis.IK_CAnalyzer
--高亮显示配置start-->
key="compass.engine.highlighter.default.formatter.simple.pre">
[CDATA[red;'>]]>
key="compass.engine.highlighter.default.formatter.simple.post">
[CDATA[]]>
--高亮显示配置end-->
class="pass.impl.HtmlPropertyConverter"/>
class="pass.gps.device.hibernate.HibernateGpsDevice">
hibernateDevice
true
说明:
1、
--指定索引存放的位置-->
classpath:
com
2、指定要进行搜索的POJO类一定要与在同一行
--指定要进行搜索的POJO类-->
.contract.entity.ns.Compass_View
3、
--指定摘要文本的长度-->
key="compass.engine.highlighter.default.fragmenter.simple.size">
125
4、
--指定用IKAnalyzer分词方法、
org.mira.lucene.analysis.IK_CAnalyzer>
5、
--高亮显示配置start-->
key="compass.engine.highlighter.default.formatter.simple.pre">
[CDATA[red;'>]]>
key="compass.engine.highlighter.default.formatter.simple.post">
[CDATA[]]>
--高亮显示配置end-->
六、建立compass定时生成策略
xmlversion="1.0"encoding="GBK"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xmlns:
context="http:
//www.springframework.org/schema/context"
xsi:
schemaLocation="
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-2.5.xsd">
--配置索引策略-->
--定时重建索引(利用quartz)或随SpringApplicationContext启动而重建索引-->
class="pass.impl.TimePrinter"
lazy-init="true">
${.centforsoft.refinework.bpm.creatCompass.begin}
--包装任务调度Bean-->
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
--定义目标Bean-->
--定义目标Bean中的方法-->
--配置触发器-->
class="org.springframework.scheduling.quartz.CronTriggerBean">
--确定任务调度的目标任务-->
--每周周一至周五凌晨4:
30执行一次重建索引这里读的是配置文件里的时间-->
${.centforsoft.refinework.bpm.creatCompass.upTime}
--配置启动触发器-->
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
--指定触发器列表-->
七、加入核心的搜索构造类
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;
System.out.println("完成重建索引-->线程结束.用时"+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;
Listlist=null;
try{
init();
hashMap=newHashMap();
CompassHitshits=null;
Compasscompass=compassTemplate.getCompass();
session=compass.openSession();
tx=session.beginTransaction();
//按相似度查询
if(selectType=