您好,登錄后才能下訂單哦!
使用php怎么實(shí)現(xiàn)一個(gè)搜索類?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
具體內(nèi)容如下
<?php /** * SoClass.php * 索引與搜索類 */ class SoClass { private $_xindex; private $_xsearch; private $_project; public function __construct($project){ //載入引導(dǎo)文件 require_once 'lib/XS.php'; //初始化 $xs = new XS($project); $this->_project = $project; $this->_xindex = $xs->index; $this->_xsearch = $xs->search; $this->_xsearch->setCharset('UTF-8'); } public function query($keyWord,$row=20,$jnum=0){ $xs = new XS($this->_project); $xs->search->setFuzzy(); $xs->search->setAutoSynonyms(); $xs->search->setQuery($keyWord); //支持同義詞搜索,默認(rèn)打開(kāi) $xs->search->setLimit($row, $jnum); //設(shè)置返回結(jié)果最多為 5 條,并跳過(guò)前 10 條 $docs = $xs->search->search(); //執(zhí)行搜索,將搜索結(jié)果文檔保存在 $docs 數(shù)組中 $count = $xs->search->count(); //獲取搜索結(jié)果的匹配總數(shù)估算值 if($count){ $data = array(); foreach ($docs as $key=>$doc){ $data[$key]['pid'] = $doc->pid; $data[$key]['nid'] = $doc->nid; $data[$key]['category'] = $doc->category; $data[$key]['url'] = $doc->url; $data[$key]['name'] = $xs->search->highlight(htmlspecialchars($doc->name)); $data[$key]['message'] = $xs->search->highlight(htmlspecialchars($doc->message)); } return array('data'=>$data,'count'=>$count); } return array(); } public function hotWord($num,$type='lastnum'){ return $this->_xsearch->getHotQuery($num,$type); } public function expanded($keyWord){ return $this->_xsearch->getExpandedQuery($keyWord); } public function lastCount(){ return $this->_xsearch->getLastCount(); } public function index($data,$update=0){ // 創(chuàng)建文檔對(duì)象 $doc = new XSDocument; $doc->setFields($data); // 添加或更新到索引數(shù)據(jù)庫(kù)中 if(!$update){ $this->_xindex->add($doc); }else{ $this->_xindex->update($doc); } } public function delete($idArray){ //刪除索引(主鍵刪除array('1','2','3')) $this->_xindex->del($idArray); } public function addSynonym($word1,$word2){ $this->_xindex->addSynonym($word1,$word2); } public function clearIndex(){ $this->_xindex->clean(); } } ?>
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。