Codeigniter与Sphinx结合

在写这篇之前,有介绍过了 Sphinx 一篇针对繁体中文检索的教学,大家可以先去参考看看,把 Sphinx 服务架设起来,在搭配 CodeIgniter MVC Framework 使用,把 Sphinx 提供的 PHP API 放到 CI 的 Library 里面就可以运作了,首先去官网下载套件,然后参考官方的安装文件, 大致上就差不多了,下载的档案里面会有 api 文件夹,里面提供了 python, ruby, java, php 的 client端档案,让您去自由呼叫,PHP 部份可以看 sphinxapi.php 这个档案,我们也只需要把这个档案放入 CI 的 Library里面,不过写法有些改变,请看底下


1. 非常简单,开启 sphinxapi.php 然后前面加上:

这是 Library 的扩充写法,可以参考中文文件。存盘之后放入 application/libraries �n名取 Sphinxapi.php 就可以了
2. 如何使用:预先加载到建构子
代码: 选择全部
function __construct()
{
  parent::Controller();
  $this->load->library("sphinxapi");
}


或者是加入到 config/autoload.php 里面也是可以的
3. 使用 sphinxapi,请先设定 sphinxapi 里面的 host 跟 port,设定完之后,就可以正常连接了
代码: 选择全部
$index = 'test2'; // 您要索引的名称
$start = 0; // 抓取起始笔数
$limit = 100;   // 一次抓取 100 笔
$this->sphinxclient->SetWeights(array(100,1));
$this->sphinxclient->SetMatchMode(SPH_MATCH_ALL);
$this->sphinxclient->SetLimits($start,$limit);
$this->sphinxclient->SetArrayResult(true);
// $keyword 你要查询的字符串
$res = $this->sphinxclient->Query($keyword,$index);


$res 会回传我们想要的数据,拿到之后,利用 $total = $res['total_found']; 取得查到的笔数,然后利用底下程序代码取得数据 auto increment ID
代码: 选择全部
foreach($res['matches'] as $row)
{
    $vedio[] = $row['id'];
}


在丢到 model 找到自己想要的数据,这样就可以了,其实还有很多进阶作法,这里就不多说了,可以参考官方文件,都是设定 sphinx.conf 档案。最后附上 sphinxapi.php
 

你可能感兴趣的:(python,下载,教学,的,繁体中文)