PHP+MySQL+sphinx+scws实现全文检索功能详解(3)
启动sphinx
[root@MevHost ~]# pkill searchd [root@MevHost ~]# /usr/local/sphinx2/bin/indexer --config /usr/local/sphinx2/etc/sphinx.conf --all [root@MevHost ~]# /usr/local/sphinx2/bin/searchd --config /usr/local/sphinx2/etc/sphinx.conf
如果出现下面的报错
"Oops! It seems that sphinx was built with wrong endianess (cross-compiling?)
either reconfigure and rebuild, defining ac_cv_c_bigendian=no in the environment of
./configure script,
either ensure that '#define USE_LITTLE_ENDIAN = 1' in config/config.h"
我是直接把sphinx下面的config/config.h 改成了他提示的这个 #define USE_LITTLE_ENDIAN = 1,之后make的,
接下来的这段是我们的PHP代码了
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form method="post" action='test.php'> <p>输入:</p> <input type="text" name="q" autocomplete="false"> </form> </body> </html> <?php // phpinfo();die; ini_set('display_errors','1'); error_reporting(E_ALL); header("Content-type: text/html; charset=utf-8"); if($_POST){ $b_time = microtime(true); $key = $_POST['q']; $index = "users"; //========================================分词 $so = scws_new(); $so->set_charset('utf-8'); //默认词库 $so->add_dict(ini_get('scws.default.fpath') . '/dict.utf8.xdb'); //自定义词库 // $so->add_dict('./dd.txt',SCWS_XDICT_TXT); //默认规则 $so->set_rule(ini_get('scws.default.fpath') . '/rules.utf8.ini'); //设定分词返回结果时是否去除一些特殊的标点符号 $so->set_ignore(true); //设定分词返回结果时是否复式分割,如“中国人”返回“中国+人+中国人”三个词。 // 按位异或的 1 | 2 | 4 | 8 分别表示: 短词 | 二元 | 主要单字 | 所有单字 //1,2,4,8 分别对应常量 SCWS_MULTI_SHORT SCWS_MULTI_DUALITY SCWS_MULTI_ZMAIN SCWS_MULTI_ZALL $so->set_multi(false); //设定是否将闲散文字自动以二字分词法聚合 $so->set_duality(false); //设定搜索词 $so->send_text($key); $words_array = $so->get_result(); $words = ""; foreach($words_array as $v) { $words = $words.'|('.$v['word'].')'; } //加入全词 #$words = '('.$key.')'.$words; $words = trim($words,'|'); $so->close(); echo '<p>输入:'.$key.'</p>'."\r\n"; echo '<p>分词:'.$words.'</p>'."\r\n"; //========================================搜索 $sc = new SphinxClient(); $sc->SetServer('127.0.0.1',9312); #$sc->SetMatchMode(SPH_MATCH_ALL); $sc->SetMatchMode(SPH_MATCH_ANY); $sc->SetArrayResult(TRUE); $res = $sc->Query($words,$index); echo "<hr>"; echo "<pre>"; print_r($res); $e_time = microtime(true); $time = $e_time - $b_time; echo $time; } exit; ?>
内容版权声明:除非注明,否则皆为本站原创文章。