1. 程式人生 > >PHP+Mysql+Sphinx高效的站內搜尋引擎2之 例項

PHP+Mysql+Sphinx高效的站內搜尋引擎2之 例項

public function getSearch()
{ //tp3.2框架

    header("Access-Control-Allow-Origin: *");
    vendor('sphinxapi');
    $st=time();
    $cl = new \SphinxClient ();
    $s = I('get.s'); //搜尋內容
    $sql = "";
    $host = "127.0.0.1";
    $port = 9312;
    $cl->SetServer ( $host, $port );
    $cl->SetConnectTimeout(10);
    $cl->SetArrayResult(true);
    $page = (int)I('get.p')?(int)I('get.p'):1;

    $cate=(int)I('get.c')?I('get.c'):1;
    switch ($cate) {
        case 1:
            $pagesize=16;
            $cl->SetLimits(($page - 1) * $pagesize , $pagesize);
            // $cl->setSortMode(SPH_SORT_EXTENDED,'id desc');
            $video= $cl->Query ( $s, 'video' );
            $video_sum=$video['total']?$video['total']:0;
            if ($video_sum==0) {
                $result['data']= M('kqy_video')->alias('v')
                    ->where('v.result=2 and v.is_delete=0 and notshow=1')
                    ->field('v.id,v.title,v.pictures,v.hits,u.user_login,v.newvideotime,u.website,u.kqy_rank_id,u.companyname1,u.companyname,u.user_nicename,u.avatar,c.rewrite_url')
                    ->join('left join spb_users u on u.user_login=v.username')
                    ->join('left join kqy_video_cate c on c.id=v.cate')
                    ->order('id desc')
                    ->limit(20)
                    ->select();
                $result['code']=0;
                break;
            }
            $arrid=array_column($video['matches'],'id');
            // rsort($arrid);
            $where['v.id']=array('in',implode(',',$arrid));
            $result['data']= M('kqy_video')->alias('v')
                ->where($where)
                ->field('v.id,v.title,v.pictures,v.hits,u.user_login,v.newvideotime,u.website,u.kqy_rank_id,u.companyname1,u.companyname,u.user_nicename,u.avatar,c.rewrite_url')
                ->join('left join spb_users u on u.user_login=v.username')
                ->join('left join kqy_video_cate c on c.id=v.cate')
                ->select();
            break;
        case 2:
            $pagesize=8;
            $cl->SetLimits(($page - 1) * $pagesize , $pagesize);
            // $cl->setSortMode(SPH_SORT_EXTENDED,'id desc');
            $article=$cl->Query($s,'article');
            $article_sum=$article['total']?$article['total']:0;
            if ($article_sum==0) {
                $result['data']=M('kqy_article')
                    ->alias('t1')
                    ->field('t1.id,t1.timer,t1.title,t1.article_introduction,t1.content,t1.createtime,t1.clicks,t1.imgpath,t2.catename,t3.user_login username,t3.user_nicename,t3.avatar_type,t3.avatar,t3.website,t3.kqy_rank_id,t3.companyname1,t3.companyname')
                    ->join('left join kqy_article_cate as t2 on t2.id=t1.cate_new')
                    ->join('left join spb_users as t3 on t3.user_login=t1.username')
                    ->where('t1.statu=0 and t1.is_delete=0')
                    ->order('id desc')
                    ->limit(20)
                    ->select();
                $result['code']=0;
                break;
            }
            $arrid=array_column($article['matches'],'id');
            rsort($arrid);
            $where['t1.id']=array('in',implode(',',$arrid));
            $result['data'] =M('kqy_article')
                ->alias('t1')
                ->field('t1.id,t1.timer,t1.title,t1.article_introduction,t1.content,t1.createtime,t1.clicks,t1.imgpath,t2.catename,t3.user_login username,t3.user_nicename,t3.avatar_type,t3.avatar,t3.website,t3.kqy_rank_id,t3.companyname1,t3.companyname')
                ->join('left join kqy_article_cate as t2 on t2.id=t1.cate_new')
                ->join('left join spb_users as t3 on t3.user_login=t1.username')
                ->where($where)
                ->select();
            break;
        case 3:
            $pagesize=8;
            $cl->SetLimits(($page - 1) * $pagesize , $pagesize);
            $person=$cl->Query($s,'person');
            $person_sum=$person['total']?$person['total']:0;
            $host = C('WEBSETHOST');
            if ($person_sum==0) {
                $s=mt_rand(30,90);
                $result['data'] = M('spb_users')
                    ->alias('t3')
                    ->field('t3.id,t3.user_login,t3.user_nicename,t3.kqy_rank_id,t3.create_time,t3.avatar,t3.user_login,t3.website,t3.companyname1,t3.companyname,t3.content,t3.province,p.professionname,c.name as cityname,count(*)')
                    ->join('left join kqy_creator_infos as t4 on t4.uid=t3.id')
                    ->join('left join kqy_profession p on p.profession=t4.profession')
                    ->join('left join city c on c.code=t3.gcity')
                    ->join('left join kqy_video v on v.username=t3.user_login')
                    ->where('t3.kqy_rank_id!=2 and v.is_delete=0')
                    ->group('v.username')
                    ->order('count(*) desc')
                    ->limit($s,6)
                    ->cache(36000)
                    ->select();
                $type=1;
            }else{
                $arrid=array_column($person['matches'],'id');
                rsort($arrid);
                $where['t3.id']=array('in',implode(',',$arrid));
                $result['data']= M('spb_users')
                    ->alias('t3')
                    ->field('t3.id,t3.user_login,t3.user_nicename,t3.kqy_rank_id,t3.create_time,t3.avatar,t3.user_login,t3.website,t3.companyname1,t3.companyname,t3.content,t3.province,p.professionname,c.name as cityname')
                    ->join('left join kqy_creator_infos as t4 on t3.id=t4.uid')
                    ->join('left join kqy_profession p on p.profession=t4.profession')
                    ->join('left join city c on c.code=t3.gcity')
                    ->where($where)
                    ->select();
            }
            if ($type==1) {
                $result['code']=0;
            }
            break;
    }

    echo json_encode($result);
}