sphinx/coreseek在php中应用

官方下载所需要文件:http://www.coreseek.cn/


require ("sphinxapicore.php");
$cl = new SphinxClient ();
$cl->SetServer ( '61.100.100.100', 3326);
$cl->SetConnectTimeout ( 3 ); //超时设置
$cl->SetArrayResult ( true ); //php返回数组
$cl->GetLastError();//错误   
$cl->SetFieldWeights(array("title" => 5, "content" => 1));
$cl->SetSortMode(SPH_SORT_EXPR,"@weight");
$cl->SetSortMode(SPH_SORT_RELEVANCE); //相关度排序
//$cl->SetMatchMode ( SPH_MATCH_EXTENDED2 );
//$cl->AddQuery ( "@title ".$keywords."" , "*" ); //过滤只与标题匹配
$cl->SetMatchMode(SPH_MATCH_ALL); //匹配所有查询词(默认模式); $m = isset($_GET['page'])?intval($_GET['page']):1; //分页设置
$cl->SetSortMode ( SPH_SORT_ATTR_DESC, "releaseTime" ); //设置排序
$n = 12;
$limit = ($m-1)*$n;
/**
 * [noGarbled 中文截取无码]
 * @param  [type] $string [description]
 * @param  [type] $length [description]
 * @return [type]         [description]
 */
function noGarbled($string, $length) {
    preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
    for($i=0; $i<count($info[0]); $i++) {
        $wordscut .= $info[0][$i];
        $j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
        if ($j > $length - 3) {
            return $wordscut." ...";
        }
    }
    return join('', $info[0]);
}



//高亮配置
$opts = array(  
                "before_match"      => "<font style='color:red;'>",  
                "after_match"       => '</font>'
                //"allow_empty"       =>  true ,
                //"exact_phrase"      => true,
                //"query_mode"        =>  true,
               // "force_all_words"   =>  true,
                //"emit_zones"        =>  true
            );

function heigthKeywordsSelf($title,$keywords,$opts,$cl,$num=110)
{
    $arr[] = strip_tags(noGarbled($title,$num));
    $res = $cl->BuildExcerpts($arr, 'complexsearchIndex', $keywords, $opts);
    return $res;
}



PHP调用数据输出



$cl->ResetFilters();

        $cl->SetLimits($limit,$n);
        $cl->SetFilter('type',array($type));
        $result = $cl->Query($keywords,"*");//news 表示索引名。 csft_mysql.conf里所有的索引,则用 * 代替
        //print_r($cl);
        //设置最多返回条件
        $total = $result['total'];
        //实际查寻总条件
        $total_found = $result['total_found'];
        //[time] => 0.007
        //耗时
        $time = $result['time'];
        //print_r($result);
        $arr_key = $result['matches'];//取出匹配结果的key值,形成一个新的数组

         foreach ($arr_key as $key => $value) {
             $ids .= $result['matches'][$key]['id'].',';
         }

         //if( ! @$result['matches'])
         //{
            //echo('没有搜索到相关结果');
         //}
         $ids = rtrim($ids,',');



$sql = "select title,content,type,releaseTime from house_complex where id in({$ids}) and type=".$type." order by releaseTime desc"; 

            $list = mysql_query($sql);
            while($row = mysql_fetch_array($list)){echo "xxxx";}
<!-- 分页 -->
       <div class="page">
       <?php
       if($total_found>0)
       {
        $current_page=isset($_GET['page'])?intval($_GET['page']):1;//获取用户GET提交的page,如果没有就默  
        $t = new Page($n, ($total_found>$total)?$total:$total_found, $current_page, 10);  
        echo $t->subPageCss2(); 
       }
       ?>
        <!-- 分页END -->




你可能感兴趣的:(coreseek,应用)