1、sphinx单条件搜索创建索引、开启搜索服务,以下是我创建的表单
<center>
<form action="index.php?r=sphinx/sphinx" method="post">
<input type="hidden" name="_csrf" value="<?= yii::$app->request->csrfToken?>" />
<p>作者:<input type="text" name="value"></p>
<p><input type="submit" value="搜索"></p>
</form>
</center>
2、然后将coreseek中的csft_mysql.conf类方法yii框架的 yii\vendor\composer\ 下
在yii\vendor\autoload.php中添加
require_once __DIR__ . '/composer' . '/sphinxapi.php';
让其自动加载,如下
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
require_once __DIR__ . '/composer' . '/sphinxapi.php';
return ComposerAutoloaderInit32b8eb537f8e12e57c5e7bade69d01f0::getLoader();/
3、在控制器中引用,然后编辑,如下,如果打印出数据库数据,就是成功了
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Search;
use SphinxClient;
class SphinxController extends Controller
{
public function actionIndex(){
return $this->render('index');
}
//sphinx单条件搜索
public function actionSphinx(){
//接收要搜索的值
$data=Yii::$app->request->post();
$sphinx= new SphinxClient(); //实例化sphinx
$sphinx->SetServer('127.0.0.1',9312);//创建链接,9312微端口号
$res=$sphinx->Query($data['title'],"*");//执行搜索
//sphinx搜素把搜索结果的ID放在matches中返回
$id=implode(',', array_keys($res['matches'])) ;//取到结果的ID,拼接字符串
//以结果的ID集,来循坏取到每一条数据
$where="id in ($id)";
$arr= Search::find()->where($where)->asArray()->all();
//打印查看结果
print_r($arr);die;
}
}