<span style="font-family:SimHei;font-size:14px;">
</span>
1、sphinx多条件搜索创建索引、开启搜索服务,以下是我创建的表单,option标签的value微数据库字段
<span style="font-family:SimHei;font-size:14px;"><center>
<form action="index.php?r=sphinx/sphinx" method="post">
<input type="hidden" name="_csrf" value="<?= yii::$app->request->csrfToken?>" />
<p>搜索字段:<select name="set">
<option value="1">全部</option>
<option value="title">标题</option>
<option value="man">作者</option>
</select></p>
<p>作者:<input type="text" name="value"></p>
<p><input type="submit" value="搜索"></p>
</form>
</center></span>
2、然后将coreseek中的csft_mysql.conf类方法yii框架的 yii\vendor\composer\ 下
在yii\vendor\autoload.php中添加
require_once __DIR__ . '/composer' . '/sphinxapi.php';
让其自动加载,如下
<span style="font-family:SimHei;font-size:14px;"><?php3、
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
require_once __DIR__ . '/composer' . '/sphinxapi.php';
return ComposerAutoloaderInit32b8eb537f8e12e57c5e7bade69d01f0::getLoader();/
</span>
<span style="font-family:SimHei;font-size:14px;">在控制器中引用,然后编辑,如下,如果打印出数据库数据,就是成功了</span>
<span style="font-family:SimHei;font-size:14px;">public function actionSphinx(){$data=Yii::$app->request->post(); $sphinx= new SphinxClient(); //实例化sphinx $sphinx->SetServer('127.0.0.1',9312);//创建链接,9312微端口号 $sphinx->SetMatchMode ( SPH_MATCH_EXTENDED2 ); if ($data['set']==1) { $key=$data['value']; }else{ $key='@'.$data['set'].' '.$data['value']; } //echo $key;die; $res=$sphinx->Query($key,"mysql"); $id=implode(',', array_keys($res['matches'])) ;//取到结果的ID,拼接字符串 $where="id in ($id)"; $arr= Search::find()->where($where)->asArray()->all(); print_r($arr);die; }</span>注意多条件查询,还要实例化一个类
<span style="font-family:SimHei;font-size:14px;"> $sphinx->SetMatchMode ( SPH_MATCH_EXTENDED2 ); </span>其中$key的格式为"@字段名 值"。