1.开始 -> 运行 -> 输入cmd回车,打开命令行工具 进入sphinx 文件所在的位置如:
我的位置是:
cd E:/usr/local/coreseek-3.2.14-win32
2.indexer.exe -c 配置文件 --all(参数--all
表示创建全部索引)
.\bin\indexer.exe -c .\etc\csft_mysql.conf --all
3 .启动searchd 服务 searchd.exe -c 配置文件:
.\bin\searchd.exe -c .\etc\csft_mysql.conf
果启动服务出现1067错误后 查看配置文件 和配置文件的文字编码.
4.php 操作sphinx
将sphinx安装目录api文件夹下的sphinxapi.php文件复制到Yii框架的 vendor\composer目录下
require_once __DIR__ . '/composer' . '/sphinxapi.php';
5.打开Yii框架vendor目录下的 autoload.php文件加入下面一行,使其自定加载redis的类
6.现在我们在控制器中操作了
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
//实例化出来sphinx类
use SphinxClient;
use app\models\Documents;
header("content-type:text/html;charset=utf-8");
class SiteController extends Controller
{
public function actionSou(){
//接收要搜索的值
$title=Yii::$app->request->post('title');
$sphinx= new SphinxClient(); //实例化sphinx
$sphinx->SetServer('127.0.0.1',9312);//创建链接
$res=$sphinx->Query($title,"*");//执行搜索
//sphinx搜素把搜索结果的ID放在matches中返回
$data=$res['matches'];//取到结果的ID
//以结果的ID集,来循坏取到每一条数据
foreach($data as $k=>$v){
$arr[]= Documents::find()->where(['id'=>$k])->asArray()->one();
}
//标红,循环结果集
foreach($arr as $k=>$v){
//str_replace('需要标红的值','用来替换的值','在那个值上操作')
$arr[$k]=str_replace($title,"<font color='red'>$title</font>",$v);
}
//打印查看结果
print_r($arr);die;
}
}