Thinkphp 整合Sphinx 代码应用实例

前面我们介绍了CentOS6.4 64位下安装sphinx及sphinx for php扩展,由于coreseek已经无法下载,所以我们暂时只能使用原生的sphinx,可惜原生的sphinx不支持中文检索,下面给出一段Thinkphp下整合sphinx扩展的代码实例:

$nowPage = I('p');//当前页  import('ORG.Util.Page');  $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;  $nowPage = $nowPage>1 ? $nowPage : 1;  $off=($nowPage-1)*$PageSize;    $sphinx = new SphinxClient;  $sphinx->setServer("localhost", 9312);  $sphinx->setMatchMode(SPH_MATCH_ANY);   //匹配模式 ANY为关键词自动拆词,ALL为不拆词匹配(完全匹配)  $sphinx->SetFilterString('status',array(1));  $sphinx->SetSortMode(SPH_SORT_EXTENDED,"@weight desc, update_time desc, id desc");  $sphinx->SetArrayResult ( true );	//返回的结果集为数组  $sphinx->SetLimits($off,$listRows);//传递当前页面所需的数据条数的参数  $result = $sphinx->query( $s ,"*");	//星号为所有索引源    $total=$result['total'];		//查到的结果条数  $time=$result['time'];		//耗时  $lists=$result['matches'];		//结果集  $sphinx->close();  $page = new ThinkPage($total, $listRows);  $p = $page->show();  $this->assign('_page', $p? $p: '');  $this->assign('_total',$total);  $this->assign('_time',$time);  $this->display();

 

腾讯云限时秒杀【点击购买】

搬瓦工,CN2高速线路,1GB带宽,电信联通优化KVM,延迟低,速度快,建站稳定,搬瓦工BandwagonHost VPS优惠码BWH26FXH3HIQ,支持<支付宝> 【点击购买】!

Vultr$3.5日本节点,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快【点击购买】!

阿里云香港、新加坡VPS/1核/1G/25G SSD/1T流量/30M带宽/年付¥288【点击购买】