安装
假设你的系统上已经安装好饿 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 // 所连接的 ipsql_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-, Andrew AksyonoffCopyright (c) -, Sphinx Technologies Inc ()using config file '/usr/local/Cellar/sphinx/2.2.10/etc/sphinx.conf'...indexing index 'test1'...collected 4 docs, 0.0 MBsorted 0.0 Mhits, 100.0% donetotal 4 docs, 193 bytestotal 0.160 sec, 1198 bytes/sec, 24.84 docs/secindexing index 'test1stemmed'...collected 4 docs, 0.0 MBsorted 0.0 Mhits, 100.0% donetotal 4 docs, 193 bytestotal 0.005 sec, 32339 bytes/sec, 670.24 docs/secskipping non-plain index 'dist1'...skipping non-plain index 'rt'...total 8 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avgtotal 24 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avgrotating indices: successfully sent SIGHUP to searchd (pid=1342).
第四步:启动 searchd
➜ ~ searchd
输入如下信息:
Sphinx 2.2.10-id64-release (2c212e0)Copyright (c) 2001-, Andrew AksyonoffCopyright (c) -, Sphinx Technologies Inc ()using config file '/usr/local/Cellar/sphinx/2.2.10/etc/sphinx.conf'...listening on all interfaces, port=9312listening on all interfaces, port=9306precaching index 'test1'precaching index 'test1stemmed' precaching index 'rt' precached 3 indexes in 0.003 sec
出现上面这些信息,说明启动成功!
第五步:使用 PHP 检测 Sphinx 及扩展是否安装成功
<?phpheader('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"=> "<b>","after_match" => "</b>","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=0n=1, res=this is my <b>test</b> <b>text</b> to be highlighted, ... n=2, res=another <b>test</b> <b>text</b> to be highlighted, below limitn=3, res=<b>test</b> number three, without phrase matchn=4, res=final <b>test</b>, not only ... with swapped phrase <b>text</b> <b>test</b> as wellexact_phrase=1n=1, res=this is my <b>test text</b> to be highlighted, ... n=2, res=another <b>test text</b> to be highlighted, below limitn=3, res=test number three, without phrase matchn=4, res=final test, not only without phrase match, but also above ...
搞定!