1.首先先在服務(wù)器上安裝好sphinx并且建立好索引文件,具體安裝步驟參考以下鏈接
windows下:?https://www.cnblogs.com/life_lt/p/15348746.html
linux下:https://www.cnblogs.com/life_lt/p/15348533.html
2.下載?sphinxapi.php(點(diǎn)擊下載)并且放入到自己的項(xiàng)目中
3.調(diào)用接口,代碼如下
public function getSphinxData(){ $searchWord = "元旦"; $sp=new SphinxClient(); $sp->SetServer('127.0.0.1',9312); $sp->SetArrayResult(true); $sp->SetMatchMode(SPH_MATCH_ALL); $sp->SetSortMode(SPH_SORT_ATTR_DESC,'updatetime'); //排序字段 $sp->SetLimits(0,200); //條數(shù)限制為200條 $res=$sp->Query($searchWord,'newindex'); //通過索引查找索索結(jié)果,newindex為配置中的索引名稱 if(isset($res['matches'])&&count($res['matches'])>0) { $sql='select * from yourtable where id in('; foreach ($res['matches'] as $v) { $sql.=$v['id'].','; } $sql=trim($sql,',').')'; $searchModel = M('search'); //實(shí)例化model,這里是我自己程序中的實(shí)例化,用的時候改成自己的操作 $result = $searchModel->fetchAll($sql); //查庫操作,fetchAll()是我自己定義的方法,改成自己數(shù)據(jù)庫操作即可 $total = $res['total']; //查詢到的總量 $finalResult = array('result'=>$result,'total'=>$total); print_r($finalResult); } else { return 0; } }
結(jié)果展示:
Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => title [1] => img_cover [2] => url_link [3] => ispublish ) [attrs] => Array ( [sid] => 1 [updatetime] => 2 ) //匹配結(jié)果 [matches] => Array ( [0] => Array ( [id] => 79476 [weight] => 2613 [attrs] => Array ( [sid] => 718737 [updatetime] => 1543464537 ) ) [1] => Array ( [id] => 79475 [weight] => 2613 [attrs] => Array ( [sid] => 718736 [updatetime] => 1543464085 ) ) ) [total] => 117 //查詢結(jié)果總量 [total_found] => 117 [time] => 0.000 [words] => Array ( [元] => Array ( [docs] => 625 [hits] => 638 ) [旦] => Array ( [docs] => 122 [hits] => 124 ) )
最終返回結(jié)果如下
?
本文摘自 :https://www.cnblogs.com/