<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;"><?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
require_once __DIR__ . '/composer' . '/sphinxapi.php';
return ComposerAutoloaderInit32b8eb537f8e12e57c5e7bade69d01f0::getLoader();/
</span>
3、<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的格式爲"@字段名
值"。