yii2框架中使用sphinx使用搜索引擎 多条件选择搜索

来源:互联网 发布:决战武林进阶数据地煞8 编辑:程序博客网 时间:2024/05/18 12:31

首先,你需要安装spinx,具体安装可以百度一份如何安装,网上有很多,就不说了,

那么,安装完成后,打开sphinx(即你所建的sphinx安装目录),

找到这个文件,sphinx/etc/csft_mysql.conf文件,

在编译器中,打开这个文件,修改sphinx的源文件,配置

改完配置后,停止sphinx服务,打开cmd,进入到你安装的sphinx安装目录中

建立索引,

索引建立成功,开启sphinx服务

在使用sphinx之前,你需要把sphinx/api/sphinxapi.PHP文件,复制一份,放到yii2的web中,

与你的入口文件保持同级,方便调用

创建控制器,

[html] view plain copy
  1. <?php  
  2.   
  3. namespace frontend\controllers;  
  4.   
  5. use Yii;  
  6. use app\models\Position;  
  7. //use yii\data\Pagination;//分页类  
  8.   
  9. use yii\db\Query;//搜索类  
  10.   
  11. class IndexController extends \yii\web\Controller  
  12. {  
  13.   
  14.     //下拉选项字段 搜索值  
  15.     public function actionSearch_val()  
  16.     {  
  17.         $set = Yii::$app->request->get('set','');//接收搜索类型  
  18.         $key = Yii::$app->request->get('key','');//接收值  
  19.         require ( "sphinxapi.php" );//引入类  
  20.         if(yii::$app->request->isAjax){  
  21.             //echo $key.$set;die;  
  22.             $cl = new SphinxClient ();  
  23.             $cl->SetServer ( '127.0.0.1', 9312);   
  24.             $cl->SetConnectTimeout ( 3 );  
  25.             $cl->SetArrayResult ( true );  
  26.                 if(empty($key)){  
  27.                     $cl->SetMatchMode ( SPH_MATCH_FULLSCAN );  
  28.                 }else{  
  29.                     $cl->SetMatchMode ( SPH_MATCH_EXTENDED2 );  
  30.                     if($set == 1){  
  31.                         $key = $key;    
  32.                     }else if($set == 2){  
  33.                         $key = '@post_tempt ' .$key;  
  34.                     }else if($set == 3){  
  35.                         $key = '@ask_for ' .$key;    
  36.                     }   
  37.                 }  
  38.                 $res = $cl->Query ( $key, "mysql" );  
  39.                   
  40.                 if($res['total_found'] > 0){  
  41.                     $ids = [];  
  42.                     foreach ( $res['matches'] as $key => $row ) {  
  43.                         $ids[] = $row['id'];  
  44.                     }  
  45.                     $query = new Query();  
  46.                     $list = $query->select('j_id, post_tempt, ask_for')->from('job')->where(['in', 'j_id', $ids])->all();  
  47.                     //print_r($list);  
  48.                 } else {  
  49.                     $list = [];  
  50.                 }          
  51.                 Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;  
  52.                 return ['list' => $list ];  
  53.             }  
  54.         return $this->render('search2');  
  55.     }  

然后,在Views对应控制器的文件夹下,创建一个文件,search2.php
[html] view plain copy
  1. <?php  
  2.   
  3. use yii\bootstrap\ActiveForm;  
  4. use yii\helpers\Html;  
  5. use yii\helpers\Url;  
  6.   
  7. ?>  
  8. <form action="" method="get">  
  9.     <input type="hidden" name="_csrf" value="dkZkUVdiTl8lDxQCZip9Ky4eLx4mViIWQXALAQMMOR4BEQMOZVZ8aA==">  
  10.   
  11.     <div class="col-md-1">  
  12.         <select id="set">  
  13.             <option value="1">全部</option>  
  14.             <option value="2">标题</option>  
  15.             <option value="3">内容</option>  
  16.         </select>  
  17.     </div>  
  18.     <div class="col-md-3">  
  19.         <input type="text" class="form-control" placeholder="keyword" id="key">  
  20.     </div>  
  21.     <button type="button" class="btn btn-default">Search</button>  
  22. </form>  
  23.   
  24. <p>  
  25.   
  26. <div id="content"></div>      
  27.   
  28. <?php $this->beginBlock('index') ?>  
  29.   
  30. $(function(){  
  31.     $('.btn-default').click(function(){  
  32.         var set = $('#set').val();  
  33.         var key = $('#key').val();  
  34.         var url = '<?php echo Url::toRoute(['index/search_val'])?>';  
  35.   
  36.         $.getJSON(url, {'key':key, 'set':set}, function(data){  
  37.             //alert(data);  
  38.             console.log(data);  
  39.             var listsdata.list;  
  40.             //console.log(data.length)  
  41.               
  42.             var html = '<table class="table">';//这里用的是拼接  
  43.             for(var i = 0; i < lists.length; i++ ) {  
  44.                 html += '<tr>';  
  45.                 html += '<td>'+lists[i].j_id+'</td>';  
  46.                 html += '<td>'+lists[i].post_tempt+'</td>';  
  47.                 html += '<td>'+lists[i].ask_for+'</td>';  
  48.                 html += '</tr>';  
  49.             }   
  50.             html += '</table>';  
  51.             $('#content').html(html);  
  52.               
  53.         });  
  54.     });  
  55. });  
  56. <?php $this->endBlock('index') ?>  
  57. <?php $this->registerJs($this->blocks['index'], \yii\web\View::POS_END);?>  
这样就完成了,试一下效果:

完成!
0 0
原创粉丝点击