用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; }}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>
<!-- 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>