读书人

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

发布时间: 2012-12-28 10:29:04 作者: rapoo

用compass快速给你的网站添加搜索功能
如果你的网站架构采用的是spring+hibernate。用现在比较流行的开源搜索引擎框架compass可以快速的给你的网站添加强大的搜索功能。从几十万条数据中,只需几毫秒的时间就可以搜索出你想要的数据。
我现在只讲快速的把搜索功能构建到你的系统中。至于配置的细节,可能点到为止。望能够原谅。让我们开始吧。
第一步:为你要搜索的表建立索引。我们不是通过关系数据库中的表直接建立索引的。而是借助于已通过hibernate这个中间桥梁而间接的给库表建立索引。我们知道hibernate主要完成对象到库表的映射。而我们是在对象的基础上建立索引的。假如我们的库表有一个叫video(影视表)的表。有字段,id(主键,唯一编号,递增),c_name(中文名),e_name(英文名),alias(别名),genre(类型),director(导演),create_time,update_time....这个表应该对应一个对象,也就是我们常说的pojo.
Video.java

package 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这个文件。我把它里面该配置的都统一配置到了上面那个文件里了。
在上一篇文章中主要讲了,配置要索引的表和compass与spring整合时的配置。接下来我把余下的两部分写出来。
第三步:配置手动创建索引的功能。
这个其实只需在第一次生成索引的时候用,当系统正常运行时,compass中的hibernateGps会自动检测数据的变动,同时同步索引文件的。
首先在applicationContext.xml中配置bean.
<!-- 手工生成索引 -->
<bean id="buildIndexController"
ref="hibernateGps" />
<property name="indexView" value="/ftl/create.ftl" />
<property name="indexResultsView" value="/ftl/create.ftl" />
</bean>
同时我们也要配置与之相应的请求映射。
<bean id="urlHandlerMapping"
action="/buildindex.htm" method="post">
<INPUT type="hidden" name="doIndex" value="true">
<input type="submit" value="手工重建索引" />
</form>
<#if indexResults?exists>
本次索引耗时${indexResults.indexTime}毫秒!
</#if>
<p>
</body>
</html>
当我们点击手工重建索引按钮时,会在我们指定的索引目录下<prop key="compass.engine.connection">E:/video</prop>也就先在E:/video下生成gpsindex/video-index当索引生成完以后,会返回生成索引所用的毫秒数。这时就会把gpsindex文件夹下的video-index覆盖到E:/video/index/下的video-index文件夹。这时gpsindex/video-index也消失了。这就是简单的生成索引的过程。接下来我们要说最后一步了。索引我们已建好了,怎么去搜索我们想要的数据了。

读书人网 >开源软件

热点推荐