安装
假设你的系统上已经安装好饿 Homebrew,执行以下命令:
➜ ~ brew install mysql
启动MySQL:
➜ ~ mysql.server start
然后关闭MySQL:
➜ ~ mysql.server stop
安装Sphinx并将其支持MySQL:
➜ ~ brew install sphinx --with-mysql
# Sphinx 默认安装在 /usr/local/Celler/sphinx/[版本号]/
安装PHP的Sphinx扩展
➜ ~ brew install homebrew/php/php56-sphinx
# PHP-Sphinx 扩展默认安装在 /usr/local/Cellar/php56-sphinx/[版本号]/
检查 Sphinx 及扩展安装是否成功
第一步:配置 Sphinx 与数据库连接
配置文件:/usr/local/Celler/sphinx/sphinx.conf
# 如果配置文件不存在,复制 sphinx.conf.dist 至 sphinx.conf
# 下面是配置:
source src1
{
type = mysql // 数据库类型
sql_host = localhost // 所连接的 ip
sql_user = user // 数据库用户名
sql_pass = pass // 数据库密码
sql_db = test // 数据库名称
sql_port = 3306 // 数据库端口
....
默认情况下只需要修改数据库用户名和密码就可以了。
第二步:在数据库中新建一个需要被 Sphinx 索引的测试数据库
➜ ~ mysql -u root -p // 登录数据库
mysql> create database test; // 创建名为 test 的数据库
mysql> exit; // 退出mysql
// 导入测试数据
mysql -u [数据库用户名] -p [数据库密码] < /usr/local/Cellar/sphinx/[版本号]/etcexample.sql
如果没有出现什么错误就说明数据库已经创建成功了。接下来建立索引。
第三步:使用 Indexer 建立索引
➜ ~ /usr/local/Cellar/sphinx/[版本号]/bin/indexer --all
输出如下信息(版本号可能会有出入):
Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/usr/local/Cellar/sphinx/2.2.10/etc/sphinx.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.160 sec, 1198 bytes/sec, 24.84 docs/sec
indexing index 'test1stemmed'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.005 sec, 32339 bytes/sec, 670.24 docs/sec
skipping non-plain index 'dist1'...
skipping non-plain index 'rt'...
total 8 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 24 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
rotating indices: successfully sent SIGHUP to searchd (pid=1342).
第四步:启动 searchd
➜ ~ searchd
输入如下信息:
Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/usr/local/Cellar/sphinx/2.2.10/etc/sphinx.conf'...
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'test1'
precaching index 'test1stemmed'
precaching index 'rt'
precached 3 indexes in 0.003 sec
出现上面这些信息,说明启动成功!
第五步:使用 PHP 检测 Sphinx 及扩展是否安装成功
header('Content-type: text/html; charset=utf-8');
// 检测 PHP-Spinx 模块是否安装成功
if (!in_array('sphinx', get_loaded_extensions())) {
die('模块不存在,请检查!');
}
$docs = array
(
"this is my test text to be highlighted, and for the sake of the testing we need to pump its length somewhat",
"another test text to be highlighted, below limit",
"test number three, without phrase match",
"final test, not only without phrase match, but also above limit and with swapped phrase text test as well",
);
$words = "test text";
$index = "test1";
$opts = array
(
"before_match" => "",
"after_match" => "",
"chunk_separator" => " ... ",
"limit" => 60,
"around" => 3,
);
foreach ( array(0,1) as $exact )
{
$opts["exact_phrase"] = $exact;
print "exact_phrase=$exact\n";
$cl = new SphinxClient ();
$res = $cl->BuildExcerpts ( $docs, $index, $words, $opts );
if ( !$res )
{
die ( "ERROR: " . $cl->GetLastError() . ".\n" );
} else
{
$n = 0;
foreach ( $res as $entry )
{
$n++;
print "n=$n, res=$entry\n";
}
print "\n";
}
}
以上源码输出:
exact_phrase=0
n=1, res=this is my test text to be highlighted, ...
n=2, res=another test text to be highlighted, below limit
n=3, res=test number three, without phrase match
n=4, res=final test, not only ... with swapped phrase text test as well
exact_phrase=1
n=1, res=this is my test text to be highlighted, ...
n=2, res=another test text to be highlighted, below limit
n=3, res=test number three, without phrase match
n=4, res=final test, not only without phrase match, but also above ...
搞定!