php 操作sphinx
1 public function conn_sphinx(){ 2 $sphinx_config=array( 3 "host"=>"192.168.1.127", 4 "port"=>9312, 5 ); 6 import("@.Vendor.Sphinxapi"); 7 $s = new SphinxClient; 8 $s->setServer($sphinx_config["host"], $sphinx_config["port"]); 9 return $s; 10 } 11 12 public function getModelNum($where){ 13 header("Content-type:text/html;charset=utf8"); 14 $s=$this->conn_sphinx(); 15 $s->SetArrayResult(true); 16 $s->SetMatchMode(SPH_MATCH_PHRASE); 17 $s->setGroupDistinct('s_type'); 18 $s->setGroupBy("s_type",SPH_GROUPBY_ATTR); 19 $word=$where["word"]; 20 $s->SetSelect('s_type,@count as total_num'); 21 $res = $s->query($word, 'main'); 22 $result=array(); 23 foreach($res["matches"] as $value){ 24 $result[$value["attrs"]["s_type"]]=$value["attrs"]["total_num"]; 25 } 26 return $result; 27 } 28 29 public function check_model($where){ 30 header("Content-type:text/html;charset=utf8"); 31 $s=$this->conn_sphinx(); 32 33 $s->SetArrayResult(true); 34 $s->SetMatchMode(SPH_MATCH_PHRASE); 35 $s->SetConnectTimeout ( 3 ); 36 $s->setMaxQueryTime(30); //设置最大搜索时间 37 $s->SetArrayResult(true); //是否将Matches的key用ID代替 38 //$s->SetSelect ( "*" ); //设置返回信息的内容,等同于SQL 39 //$s->SetRankingMode(SPH_RANK_BM25); //设置评分模式,SPH_RANK_BM25可能使包含多个词的查询的结果质量下降。 40 41 $s->SetSortMode(SPH_SORT_EXTENDED,"s_puttime desc,id desc"); //设置匹配项的排序模式, SPH_SORT_EXTENDED按一种类似SQL的方式将列组合起来,升序或降序排列。 42 //$weights = array ('company_name' => 20); 43 //$s->SetFieldWeights($weights); //设置字段权重 44 45 $total=isset($where["search_total_num"])?$where["search_total_num"]:$this->search_total_num; 46 if(isset($where["all"])){ 47 $s->SetLimits ( 0,$total, $total, 0); 48 }else{ 49 $page=isset($where["numOfPage"])?$where["numOfPage"]:$this->numOfPage; 50 $offset=($where["page_num"]-1)*$page; 51 $s->SetLimits ( $offset, $page, $total, 0); 52 } 53 54 $s->SetFilter ( "s_type", array($where["type"]) ); 55 if(isset($where["good_model"])){ 56 $s->SetFilter ( "good_model", array($where["good_model"]) ); 57 } 58 if(isset($where["limits"])){ 59 $s->SetFilter ( "s_manual_id", $where["limits"] ); 60 } 61 $word=$where["word"]; 62 63 $res = $s->query('@* "'.$word.'"','main'); 64 $result["total"]=$res["total"]; 65 if($result["total"]>0){ 66 foreach($res["matches"] as $value){ 67 $result["info"][]=$value["attrs"]; 68 $result["manual_id"][]=$value["attrs"]["s_manual_id"]; 69 } 70 } 71 return $result; 72 73 }