Thinkphp 整合Sphinx 代码应用实例

来源: 老季博客
日期: 2017-3-1
作者: 腾讯云/服务器VPS推荐评测/Vultr
阅读数: 56

前面我们介绍了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 \Think\Page($total, $listRows);
$p = $page->show();
$this->assign('_page', $p? $p: '');
$this->assign('_total',$total);
$this->assign('_time',$time);
$this->display();

 

链接到文章: https://jiloc.com/43178.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注