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

用compass高速给你的网站添加搜索功能<一>

2012-10-27 
用compass快速给你的网站添加搜索功能一用compass快速给你的网站添加搜索功能?????? 如果你的网站架构采

用compass快速给你的网站添加搜索功能<一>

用compass快速给你的网站添加搜索功能

?????? 如果你的网站架构采用的是spring+hibernate。用现在比较流行的开源搜索引擎框架compass可以快速的给你的网站添加强大的搜索功能。从几十万条数据中,只需几毫秒的时间就可以搜索出你想要的数据。?????? 我现在只讲快速的把搜索功能构建到你的系统中。至于配置的细节,可能点到为止。望能够原谅。让我们开始吧。????? 第一步:为你要搜索的表建立索引。我们不是通过关系数据库中的表直接建立索引的。而是借助于已通过hibernate这个中间桥梁而间接的给库表建立索引。我们知道hibernate主要完成对象到库表的映射。而我们是在对象的基础上建立索引的。假如我们的库表有一个叫video(影视表)的表。有字段,id(主键,唯一编号,递增),c_name(中文名),e_name(英文名),alias(别名),genre(类型),director(导演),create_time,update_time....这个表应该对应一个对象,也就是我们常说的pojo.Video.javapackage com.jack.videopublic class Video{?private Integer id;
?private String CName;
?private String EName;
?private String alias;
?private String genre;
?pivate String director;
?private Date createTime;?private Date updateTime;
??public Video(){}? public Integer getId() {
??return this.id;
?}?public void setId(Integer id) {
??this.id = id;
?}?public String getCName() {
??return this.CName;
?}?public void setCName(String CName) {
??this.CName = CName;
?}?public String getEName() {
??return this.EName;
?}?public void setEName(String EName) {
??this.EName = EName;
?}?public String getAlias() {
??return this.alias;
?}?public void setAlias(String alias) {
??this.alias = alias;
?}?public String getGenre() {
??return this.genre
?}?public void setGenre(String genre) {
??this.genre= genre;
?}??public String getDirector() {
??return this.director;
?}?public void setDirector(String director) {
??this.director = director;
?}??public Date getCreateTime() {
??return this.ceateTime;
?}?public void setCreateTime(Date ceateTime) {
??this.ceateTime = ceateTime;
?}public Date getUpdateTime() {
??return this.updateTime;
?}?public void setUpdateTime(Date updateTime) {
??this.updateTime = updateTime;
?}
}?? 上面这个简单的pojo没什么可讲的,大家一看就很熟悉了。??? 接下来我们要针对这个pojo建立索引,也就是建立一个cpm文件。具体配置如下:Video.cpm.xml<!DOCTYPE compass-core-mapping PUBLIC
??????? "-//Compass/Compass Core Mapping DTD 1.0//EN"
??????? "http://www.opensymphony.com/compass/dtd/compass-core-mapping.dtd">
<compass-core-mapping package="com.jack.video"<class name="Video" alias="video"
index="video-index">
??<id name="id" />
??<property name="CName">
???<meta-data index="tokenized">CName</meta-data>
??</property>
??<property name="EName">
???<meta-data index="tokenized">EName</meta-data>
??</property>
??<property name="alias">
???<meta-data index="tokenized">alias</meta-data>
??</property>
??<property name="genre">
???<meta-data index="un_tokenized">genre</meta-data>
??</property>
????<property name="director">
???<meta-data index="tokenized">director</meta-data>
??</property>
??<property name="createTime">
???<meta-data index="no">trueCreateTime</meta-data>
??</property>
??<property name="updateTime">
???<meta-data index="no">updateTime</meta-data>
??</property>
?</class>
</compass-core-mapping>?? 简单的配置文件讲解一下,这个文件的package属性是指导pojo(Video.java)所在的包。<class name="Video" alias="video"
index="video-index"> .name就是类名了。alias就是给这个类在起一个别名。index是指生成索引所在的文件夹的名字。后面你就会明白的。?<property name="CName">
???<meta-data index="tokenized">CName</meta-data>
??</property>? 这个标签中的name就是Video.java中的那些属性名。<meta-data index="tokenized">这个属性指明了,该字段索引的策略,index有三个值,默认是tokenized表示先分词在索引,no表示既不分词也不索引,un_tokenized表示不分词但索引。其中还有一个属性store表示是否存储,它有两个值, yes/no.默认就是yes.表示对该字段存储。这是常用到的两个属性,其他还有很多,但是不是常用的。这两个就够了。至于其他的属性我们一般不再这里配置,一般在另外一个配置文件中统一配置。每个cpm文件都可以用到的。?第二步:配置compass文件。<!-- compass主配置 -->
?<bean id="compass" />
??</property>
??<property name="resourceLocations">
???<list>
????<value>
?????classpath:com/jack/video/Video.cpm.xml
????</value>
???</list>
??</property>
?</bean>
?<!-- 同步更新索引 -->
?<bean id="hibernateGps"
??init-method="start"
??destroy-method="stop">
??<property name="compass">
???<ref bean="compass" />
??</property>
??<property name="gpsDevices">
???<list>
????<bean
?????ref="hibernateGpsDevice" />
????</bean>
???</list>
??</property>
?</bean>
?<!--hibernate驱动 链接compass和hibernate -->
?<bean id="hibernateGpsDevice"
??/>
??</property>
?</bean>? 在这里我并没有配置compass.cfg.xml这个文件。我把它里面该配置的都统一配置到了上面那个文件里了。

热点排行