随着自己电影网站资源逐渐增多,增加电影资源搜索服务成为必然。直接操作数据库的搜索,IO口请求增多减低了搜索性能。之前项目中有sphinx的使用基础,加之支持中文检索服务,最后决定采用基于sphinx的Coreseek搜索服务。
下载安装步骤【本人采用 linux环境下 4.1版本,系统支持mysql和xml数据源】:
解压gz包,tar zxvf coreseek-4.1-beta.tar.gz
编译安装 mmseg【中文分词包】
./bootstrap
./configure --prefix=/usr/local/mmseg3
make && make install
编译安装 coreseek
sh buildconf.sh #输出的warning信息可以忽略,如果出现error则需要解决
./configure --prefix=/usr/local/coreseek --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql #with-mmseg-libs就是mmseg中文分词路径
make && make install
电影网站更新周期比较长,sphinx采用主索引+增量索引方式进行索引,最后合并两个索引文件。下面开始部署自己的搜索配置文件:
进入coreseek安装目录下的etc文件,新建或修改 .conf配置文件
配置source源
source movie
{
type = mysql
sql_host = localhost #mysql数据库host
sql_user = root #mysql用户
sql_pass = #mysql用户密码
sql_db = movie #movie
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET NAMES utf8
#建立增量索引
sql_query_pre = REPLACE INTO movie_sph_counter SELECT 1, MAX(id) FROM movie
sql_query = SELECT id, UNIX_TIMESTAMP(cdate) AS date ,id AS movie_id ,name, year, type,status,sync_status FROM movie WHERE id<=( SELECT max_movie_id FROM movie_sph_counter WHERE counter_id=1 )#搜索返回字段
sql_attr_uint = movie_id
sql_attr_uint = year
sql_attr_uint = type
sql_attr_uint = date
sql_attr_uint = status
sql_attr_uint = sync_status
sql_field_string = name
sql_query_info_pre = SET NAMES utf8 #命令行查询时,设置正确的字符集
sql_query_info = SELECT * FROM movie WHERE id=$id #命令行查询时,从数据库读取原始数据信息
}#增量索引源
source delta : movie
{
sql_query_pre = SET NAMES utf8
sql_query = SELECT id, UNIX_TIMESTAMP(cdate) AS date ,id AS movie_id ,name , year, type ,status,sync_status FROM movie WHERE id>( SELECT max_movie_id FROM movie_sph_counter WHERE counter_id=1 )
sql_query_post_index = REPLACE INTO movie_sph_counter SELECT 1, MAX(id) FROM movie
}配置索引
#index定义
index movie
{
source = movie #对应的source名称
path = /usr/local/coreseek/var/data/movie #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
#中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
charset_dictpath = /usr/local/mmseg/etc/ #BSD、Linux环境下设置,/符号结尾 mmseg路径
charset_type = zh_cn.utf-8 #中文编码
}
index delta : movie
{
source = delta
path = /usr/local/coreseek/var/data/movie_delta #注意!!不要和主索引路径名称一样
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
charset_dictpath = /usr/local/mmseg/etc/
charset_type = zh_cn.utf-8
}配置搜索服务
#searchd服务定义
searchd
{
listen = 9312 #端口号,可以自己定义
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
compat_sphinxql_magics=0
pid_file = /usr/local/coreseek/var/log/searchd_mysql.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = /usr/local/coreseek/var/log/searchd_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = /usr/local/coreseek/var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
binlog_path = #关闭binlog日志
}执行命令建立索引: /usr/local/coreseek/bin/indexer -c movie.conf --all
后台开启搜索服务运行:/usr/local/coreseek/bin/searchd -c movie.conf
建立定时任务,执行增量索引:/usr/local/coreseek/bin/indexer -c csft_movie.conf delta --rotate
建立定时任务,合并索引:/usr/local/coreseek/bin/indexer -c csft_movie.conf --merge movie delta --merge-dst-range deleted 0 0 --rotate
至此基于sphinx+mysql的搜索服务已经搭建完毕,接下来就是根据sphinxapi.php开发搜索接口代码……
第一次自己搭建sphinx搜索服务,最后测试网站搜索,速度杠杠的。
特此分享,希望对大家有所帮助
来源:oschina
链接:https://my.oschina.net/u/189901/blog/610864