首页 > 解决方案 > 中文搜索解决方案

中文搜索解决方案

2009年12月30日

中文社区的全文搜索功能一直是个让人头疼的问题,其实追根究底是由MySQL自身的问题造成的。当数据量大的时候,任何的RDBMS的查询都无法满足现今互联网的要求。

搜索引擎技术一直都作为”非大陆货”的角色存在,Sphinx的出现降低了搜索引擎的门槛、Coreseek的出现则降低了中文搜索引擎的门槛, 许多公司已经纷纷投入他们的怀抱。虽然Sphinx的功能及扩展性对于专业做搜索引擎的公司而言仍显简陋,但对于普通的全文搜索而言是太够了。

近期朋友的论坛刚刚转为Discuz!,就结合Discuz! 7.2版本标准帮朋友将这个毛病给治了。经常看到有站长被搜索功能搞的苦不堪言,不限制搜索就容易造成MySQL锁表等等,容易搞的站点很慢,限制搜索的话就放弃了一个如此便利的功能,Discuz!的使用者通常为中小站长,如果能配合Sphinx即可对搜索效率进行优化。

平台环境:

PHP+ SphinxClientAPI

SphinxClientAPI:

Download:

http://pecl.php.net/package/sphinx

Document:

http://php.net/manual/en/book.sphinx.php

Sphinx (Coreseek Fulltext Server)

Discuz 7.2(UTF-8)
关于Sphinx支持中文的安装方法:

http://hi.baidu.com/chancey/blog/item/2184746432926dfbf6365436.html
Sphinx的配置文件以及Discuz的搜索部分代码修改:

http://dzwithsphinx.googlecode.com/files/DZwithSphinx.zip
初始化:
1、修改配置文件:

修改sphinx.conf 替换其中的sql连接信息及sphinx服务信息:

sql_host
sql_user
sql_pass
sql_db
sql_port
listen

修改search.php 替换sphinx连接信息以及查询结果数:

$spx_server
$spx_port
$spx_limit

2、导入SQL

sph_counter.sql
3、执行:

./bin/indexer --config /data/opt/sphinx/etc/sphinx.conf dzbbs
./bin/indexer --config /data/opt/sphinx/etc/sphinx.conf threads
./bin/searchd --config etc/sphinx.conf

4、加入crontab计划任务:

# Incremental Index posts data
* 0-3 * * * /data/opt/sphinx/bin/indexer --config /data/opt/sphinx/etc/sphinx.conf dzbbs_delta --rotate
* 6-23 * * * /data/opt/sphinx/bin/indexer --config /data/opt/sphinx/etc/sphinx.conf dzbbs_delta --rotate
0 4 * * * /data/opt/sphinx/bin/indexer --config /data/opt/sphinx/etc/sphinx.conf dzbbs_merge --rotate && /data/opt/sphinx/bin/indexer --config /data/opt/sphi
nx/etc/sphinx.conf --merge dzbbs dzbbs_merge --rotate

# Incremental Index threads data
* 0-3 * * * /data/opt/sphinx/bin/indexer –config /data/opt/sphinx/etc/sphinx.conf threads_delta –rotate
* 6-23 * * * /data/opt/sphinx/bin/indexer –config /data/opt/sphinx/etc/sphinx.conf threads_delta –rotate
0 4 * * * /data/opt/sphinx/bin/indexer –config /data/opt/sphinx/etc/sphinx.conf threads_merge –rotate && /data/opt/sphinx/bin/indexer –config /data/opt/sp
hinx/etc/sphinx.conf –merge threads threads_merge –rotate

注:

1、xx_delta每分钟进行增量数据更新是为了能够及时查找到新的帖子和回复,但并不会更新sph_counter表的记录。此处不进行记录更新和merge是因为当数据量大以及论坛发帖频繁的时候一分钟之内有可能合并不完,间隔时间长的话又不能及时查找到。

2、dzbbs_merge、 threads_merge 是为了将第一次执行的主数据与之后的新的数据进行合并,如果长时间不合并,那么每次执行xx_delta的时间就会很长,一分钟之内执行不完。

文章转自(http://code.google.com/p/dzwithsphinx/)

Joe 解决方案 , , ,

  1. 目前还没有任何评论.
  1. 目前还没有任何 trackbacks 和 pingbacks.