1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//下载 wget http://pecl.php.net/get/sphinx-1.3.2.tgz //解压缩 tar -zxvf sphinx-1.3.2.tgz //进入安装目录 cd /sphinx-1.3.2 //执行php扩展命令 /usr/local/php/bin/phpize //如果报错:configure: error: Cannot find libsphinxclient headers cd /sphinx/coreseek-4.1-beta/csft-4.1/api/libsphinxclient ./configure --prefix=/usr/local/libsphinxclient //如果报错:cannot find input file: Makefile.in //可能因为configure.in的文件格式不对(是dos格式),将其保存为unix格式 vim configure.in :set fileformat=unix :x aclocal autoconf automake -a //再次执行 ./configure --prefix=/usr/local/libsphinxclient make //如果报错 cp /usr/bin/libtool ./libtool make install //回去继续安装 cd /sphinx-1.3.2 ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/libsphinxclient make make install //安装成功,提示如下 Installing shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/ //在php.ini增加sphinx的配置 extension=sphinx.so //重启nginx /etc/init.d/nginx restart //重启php /etc/init.d/php-fpm restart |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php //http://php.net/manual/zh/book.pdo.php try{ $db = new \PDO('mysql:host=127.0.0.1;port=3306;dbname=test;','root','123456',array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'UTF8'",PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch (\Exception $e) { die($e->getMessage()); } //http://php.net/manual/zh/class.sphinxclient.php $sphinx = new \SphinxClient(); $sphinx->SetServer('127.0.0.1',9312); //设置匹配模式为匹配关键字中的任意一个 $sphinx->setMatchMode(SPH_MATCH_ANY); //第一个参数是关键字,第二个参数是索引 *表示在所有索引中搜素 $rs = $sphinx->query('世界','*'); if(isset($rs['matches'])) { $ids = implode(',', array_keys($rs['matches'])); $res = $db->query("select * from test where id in ({$ids})"); $res = $res->fetchAll(PDO::FETCH_ASSOC); $options = array( 'before_match' => '<b style="color:red;">', 'after_match' => '</b>' ); foreach($res as $k=>$v) { //高亮显示搜索关键字 main是索引名 世界是搜索关键字 $res[$k] = $sphinx->buildExcerpts($v, 'main', '世界', $options); } echo '<pre>'; print_r($res); } |