目录
0x00 搜索的几种解决方案
sql语句 like '%str%' (不推荐)
全文索引(看数据量)
coreseek中国(暂时没人维护了)
sphinx搜索(推荐)
0x01 sphinx在linux上的安装和使用
cd /root/src
wget http://sphinxsearch.com/files/sphinx-2.2.11-release.tar.gz
解压
tar xf sphinx-2.2.11-release.tar.gz
进入目录
cd sphinx-2.2.11-release/
编译
./configure --prefix=/usr/local/sphinx --with-mysql
安装
make && make install
配置:
cd /usr/local/sphinx/etc
vim sphinx.conf.dist
配置查询的sql语句:
打开这两个地方就表示要支持中文搜索
egrep -v '#|^$' sphinx.conf.dist > sphinx.conf
vim sphinx.conf
因为以上修改sphinx.conf的过程比较复杂,所以下面直接给出修改好的配置文件:将该配置文件放在/usr/local/sphinx/etc/下即可。
source articles
{
type = mysql
sql_host = localhost
sql_user = 。。。。
sql_pass = 。。。。
sql_db = 。。。。。
sql_sock = /tmp/mysql.sock
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query = SELECT id,id as auth_id,title,title as attr_title,create_at,create_at as attr_create_at,editorValue,editorValue as
attr_editorValue FROM blog_article
#上面配置字段都会被sphinx从mysql中查出来存成自己的data文件
#上面凡是 加了as attr_xxx的字段都被会搜索
#下面配置的字段都会被返回给查询者。
sql_attr_uint =auth_id
sql_attr_string =attr_title
sql_attr_string =attr_create_at
sql_attr_string =attr_editorValue
sql_ranged_throttle = 0
}
index article
{
source = articles
path = /usr/local/sphinx/var/data/article
docinfo = extern
dict = keywords
mlock = 0
morphology = none
min_word_len = 1
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
html_strip = 0
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /usr/local/sphinx/var/log/searchd.log
query_log = /usr/local/sphinx/var/log/query.log
read_timeout = 5
client_timeout = 300
max_children = 30
persistent_connections_limit = 30
pid_file = /usr/local/sphinx/var/log/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
mva_updates_pool = 1M
max_packet_size = 8M
max_filters = 256
max_filter_values = 4096
max_batch_queries = 32
}
cd ../bin
./indexer --all 生成索引文件 即将mysql中的相关数据抽离出来生成对应sphinx的二进制索引文件
特别注意:为什么新发布的文章sphinx搜不到?就是因为发布文章后虽然文章已经存入了数据库,但是因为没有重新生成一边索引文件,所以sphinx还是搜不到。
运行:
./searchd
原生开发:
cd /root/src/sphinx-2.2.11-release/api
cp sphinxapi.php /home/my_tp5_blog/public/
cp test.php /home/my_tp5_blog/public/
原生开发使用教程:见视屏
tp5框架:
cd /home/my_tp5_blog
下载中文搜索解决方案之sphinx
composer require lampol/sphinx
这个里面内置好了 配置文件 和 sphinx的启动脚本。
cd vendor/lampol/sphinx/
复制启动脚本
cp sphinx /etc/init.d/
chkconfig --add sphinx 添加到 开机管理列表
chkconfig sphinx on 设置开机自启动
/etc/init.d/sphinx start 启动sphinx
使用教程:
use lampol\SphinxClient;
$sc = new SphinxClient ();
$words = $_POST['keywords'];
$host = "localhost";
$port = 9312;
$index = "user"; //和sphinx.conf中index 要保持一致
//设置 sphinx服务以及端口 默认是localhost 端口9312
$sc->SetServer ( $host, $port );
//设置连接的超时时间
$sc->SetConnectTimeout ( 1 );
//返回数组的数据格式
$sc->SetArrayResult ( true );
//设置返回的条数 分页
//$offset=30;
//$limit=10;
$sc->SetLimits ( $offset, $limit);
//开始查询
$res = $sc->Query ( $words, $index );
echo '<pre>';
var_dump($res);
echo '</pre>';
0x02 定期生成sphinx索引
设置服务器级别的定时任务:
crontab -e
输入:
*****
分 时 日 月 周
*/3 * * * * /etc/init.d/sphinx restart > /dev/null 2>&1
cat /var/log/cron
查看正在执行的定时任务