使用sphinx

SQL   结构化查询语言(是一种标准,所有的关系型数据库Mysql,sqlserver,oracle)

sphinx的使用两种方式:

第一种:  使用sphinx的API来操作sphinx   (常用)   
     sphinx可以使用API类,也可以将api编译到PHP中做为扩展
第二种:  使用mysql的sphinx的存储引擎


sphinx  这是英文的全文检索引擎

coreseek  这是支持中文词库的全文检索引擎


区别:
    英文的文章 怎么能区分哪个词    以空格来区分词的
    中文的文章 我爱北京天安门      中文词库


使用sphinx的步骤:

1、安装
cd E:/usr/local/coreseek-3.2.14-win32
2、配置(配置文件 csft_mysql.conf)
   配置文件格式
    1、数据源定义 (指向你数据的来源 ,通常是mysql)
        2、索引 (索引的一些配置信息)
        3、indexer  (内用的内存---基本不需要设置)
        4、searchd  (服务器的一些配置---)
   注意:
    1、一个配置文件可以配置多个数据源和索引 ((1,2),(1,2),nnnn)
    2、sql_query  主查询(把数据库表中的哪些字段查询出来--根据你页面的搜索条件)



                  select 第一个字段(一定是主键)
cd month9/date0410/vendor/yiisoft/yii2/sphinx

3、indexer.exe -c 配置文件 --all(参数--all 表示创建全部索引)
.\bin\indexer.exe -c .\etc\csft_mysql.conf --all
4、启动searchd 服务  searchd.exe -c 配置文件
.\bin\searchd.exe -c .\etc\csft_mysql.conf
5、php 操作sphinx

.\bin\indexer.exe -c .\etc\csft_mysql.conf --all
.\bin\searchd.exe -c .\etc\csft_mysql.conf
.\bin\indexer.exe -c .\etc\csft_mysql.conf delta

//多表索引
http://0x0d.im/archives/using-coreseek-with-PHP-with-multiple-indexes.html




//高亮显示
        foreach($countries as $k=>$v){
            $countries[$k]['name']=$cl->BuildExcerpts([$v['name']],'mysql','zhangsan',array('before_match'=>"<font style='font-weight:bold;color:#f00;'>",'after_match'=>"</font>"))[0];
        }

//name//键值
//mysql//索引名称
//zhangsan//搜索的高亮名称



http://www.coreseek.cn/docs/coreseek_3.2-sphinx_0.9.9.html#matching-modes




    $name=Yii::$app->request->get('name');
        $starttime=Yii::$app->request->get('starttime');
        $endtime=Yii::$app->request->get('endtime');
        //实例化sphnix
        $cl=new SphinxClient();
        $cl->SetServer('127.0.0.1',9312);
        //设置时间
        $cl->SetConnectTimeOut(3);
        $cl->SetArrayResult(true);
        if(empty($name))
        {
            $cl->SetMatchMode(SPH_MATCH_FULLSCAN);

        }else{
            $cl->SetMatchMode(SPH_MATCH_ANY);

        }
        if($starttime && $endtime)
        {
            $cl->SetFilterRange('time',$starttime,$endtime);
        }
        //执行搜索
        $arr=$cl->Query($name,'*');
        // print_r($arr);die;
        if($arr['matches']){
            foreach($arr['matches'] as $k=>$v) {
                $data[]=$v['id'];
            }

        }
        $query = Users::find()->where(['id'=>$data]);
       
        $countQuery = clone $query;
        $pages = new Pagination([
            'totalCount' => $countQuery->count(),
            'defaultPageSize' => 4,]);
        $models = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();

         return $this->render('sou', [
             'models' => $models,
             'pages' => $pages,
             'name'=>$name,
             'starttime'=>$starttime,
             'endtime'=>$endtime
         ]);
    







posted on 2016-06-28 15:10  大灰狼~彦  阅读(383)  评论(0编辑  收藏  举报