php實現(xiàn)搜索類封裝示例
更新時間:2016年03月31日 11:45:56 作者:mickelfeng
這篇文章主要為大家詳細介紹了php實現(xiàn)搜索類封裝示例,感興趣的小伙伴們可以參考一下
本文為大家分享了php實現(xiàn)搜索類封裝示例,供大家參考,具體內(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); //支持同義詞搜索,默認打開
$xs->search->setLimit($row, $jnum); //設(shè)置返回結(jié)果最多為 5 條,并跳過前 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)建文檔對象
$doc = new XSDocument;
$doc->setFields($data);
// 添加或更新到索引數(shù)據(jù)庫中
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)容,希望對大家學(xué)習(xí)php程序設(shè)計有所幫助。
相關(guān)文章
PHP+Mysql+jQuery中國地圖區(qū)域數(shù)據(jù)統(tǒng)計實例講解
使用過百度統(tǒng)計或者cnzz統(tǒng)計的童鞋應(yīng)該知道,后臺有一個地圖統(tǒng)計,不同訪問量的省份顯示的顏色也不一樣,今天我將帶領(lǐng)大家開發(fā)一個這樣的案例。2015-10-10
Thinkphp搭建包括JS多語言的多語言項目實現(xiàn)方法
這篇文章主要介紹了Thinkphp搭建包括JS多語言的多語言項目實現(xiàn)方法,可實現(xiàn)通過針對js語言包的調(diào)用達到構(gòu)建多語言站點的效果,是非常實用的技巧,需要的朋友可以參考下2014-11-11
PHP數(shù)組和explode函數(shù)示例總結(jié)
有關(guān)php分割字符串explode函數(shù)的用法,使用explode函數(shù)將字符串分割到數(shù)組,這里給大家總結(jié)了幾個示例,需要的朋友參考下。2015-05-05

