win下如何在yii框架下面使用sphinx

一 : 配置sphinx

              

二 : 啓動sphinx

             創建索引     

                   ./bin/indexer.exe -c ./etc/csft_mysql.conf --all

             開啓sphinx    

                  ./bin/searchd.exe -c ./etc/csft_mysql.conf


1: 把sphinx中的sphinxapi.php文件放入vendor下面的composer中

2: 在vendor文件夾中引入sphinxapi.php

3:在控制器中實例化sphinx

       //實例化sphinx

        $request = \Yii::$app->request;
        $data = $request->post();       

        $sphinx = new \SphinxClient();
        $sphinx->SetServer('127.0.0.1',9312);
        $result = $sphinx->Query($data['title'].$data['author'],'*');       //$data['title'].$data['author']     他們是要查詢的字段

        //獲取搜索內容
        $id = $result['matches'];
        //處理
        $list=array();
        foreach($id as $k=>$v){
            $con = Article::find()->where(['id'=>$k])->asArray()->one();
            $list[] = str_replace($con['title'],"<font color='red'>".$data['title']."</font>",$con);
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章