yii框架中sphinx單條件搜索

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;    
	}
}



發佈了33 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章