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

来源:互联网 发布:大学生的网络购物情况 编辑:程序博客网 时间:2024/05/18 15:56

一 : 配置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);
        }

1 0