yii 里sphinx使用

来源:互联网 发布:linq.js where 编辑:程序博客网 时间:2024/05/18 01:38

大家都知道sphinx自带的一些匹配模式。主要有setMatchMode:SPH_MATCH_ALL匹配所有查询词(默认模式)SPH_MATCH_ANY匹配查询词中的任意一个SPH_MATCH_PHRASE将整个查询看作一个词组,要求按顺序完整匹配SPH_MATCH_BOOLEAN将查询看作一个布尔表达式SPH_MATCH_EXTENDED将查询看作一个Sphinx内部查询语言的表达式SPH_MATCH_FULLSCAN使用完全扫描,忽略查询词汇SPH_MATCH_EXTENDED2类似 SPH_MATCH_EXTENDED ,并支持评分和权重.

控制器:

//下拉选项字段 搜索值  

    public function actionSearch_val()  

    {  

        $set = Yii::$app->request->get('set','');//接收搜索类型  

        $key = Yii::$app->request->get('key','');//接收值  

        if(yii::$app->request->isAjax){  

            $key1 = '';

            require ( "sphinxapi.php" );//引入类

            //echo $key.$set;die;  

            $cl = new \SphinxClient();  

            $cl->SetServer ( '127.0.0.1', 9312);   

            $cl->SetArrayResult ( true );  //果集(ResultSet)是数据中查询结果返回的一种对象,

                if(empty($key)){  

                    $cl->SetMatchMode ( SPH_MATCH_FULLSCAN );  //匹配模式使用完全扫描,忽略查询词汇

                }else{

                    $cl->SetMatchMode ( SPH_MATCH_EXTENDED2 );  //使用多字段模式

                    if($set == 1){  

                        $key1 = $key;    

                    }else{  

                        $key1 = '@'.$set.' ('.$key.')';  

                    }

                }  

                $res = $cl->Query ( $key1, "test2" );  

                if($res['total_found'] > 0){  

                    $ids = [];  

                    foreach ( $res['matches'] as $k => $row ) {  

                        $ids[] = $row['id'];  

                    }  

                    $query = new Query();  

                    $list = $query->from('position')->where(['in', 'id', $ids])->all();

//注意:from("自己的表名")

                    foreach($list as $k=>$v){

              $list[$k]=str_replace($key,"<font color='red'>{$key}</font>",$v);

              }   

                } else {  

                    $list = [];  

                }          

                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;  //yii 返回值为json 格式

                return ['list' => $list ];  

            }  

        return $this->render('search2');  

}

视图:

<?php  

  

use yii\helpers\Html;  

use yii\helpers\Url;  

  

?>  

<form action="" method="get">  

    <input type="hidden" name="_csrf" value="">  

  

    <div class="col-md-1">  

        <select id="set">  

            <option value="1">全部</option>  

            <option value="position">职位</option>  

            <option value="biaoqian">标签</option>  

            <option value="xueli">学历</option>  

        </select>  

    </div>  

    <div class="col-md-3">  

        <input type="text" class="form-control" placeholder="keyword" id="key">  

    </div>  

    <button type="button" class="btn btn-default">Search</button>  

</form>  

  

<p>  

  

<div id="content"></div>      

  

<?php $this->beginBlock('index') ?>  

  

$(function(){  

    $('.btn-default').click(function(){  

        var set = $('#set').val();  

        var key = $('#key').val();  

        var url = '<?php echo Url::toRoute(['hello/search_val'])?>';  

  

        $.getJSON(url, {'key':key, 'set':set}, function(data){  

            //alert(data);  

            console.log(data);  

            var lists= data.list;  

            //console.log(data.length)  

              

            var html = '<table class="table">';//这里用的是拼接  

            for(var i = 0; i < lists.length; i++ ) {  

                html += '<tr>';  

//注意以下为数据表相对应的字段

                html += '<td>'+lists[i].id+'</td>';  

                html += '<td>'+lists[i].position+'</td>';  

                html += '<td>'+lists[i].biaoqian+'</td>';  

                html += '<td>'+lists[i].xueli+'</td>';  

                html += '</tr>';  

            }   

            html += '</table>';  

            $('#content').html(html);  

              

        });  

    });  

});  

<?php $this->endBlock('index') ?>  

<?php $this->registerJs($this->blocks['index'], \yii\web\View::POS_END);?>

原创粉丝点击