php实现搜索类封装示例

前端之家收集整理的这篇文章主要介绍了php实现搜索类封装示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文为大家分享PHP实现搜索类封装示例,供大家参考,具体内容如下

PHP;"> class SoClass {

private $_xindex;

private $_xsearch;

private $_project;

public function __construct($project){

//载入引导文件
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); //<a href="/tag/zhichi/" target="_blank" class="keywords">支持</a>同义词<a href="/tag/sousuo/" target="_blank" class="keywords">搜索</a>,默认打开
$xs->search->setLimit($row,$jnum); //设置返回结果最多为 5 条,并跳过前 10 条
$docs = $xs->search->search(); //执行<a href="/tag/sousuo/" target="_blank" class="keywords">搜索</a>,将<a href="/tag/sousuojieguo/" target="_blank" class="keywords">搜索结果</a>文档保存在 $docs 数组中    
$count = $xs->search->count(); //<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="/tag/sousuojieguo/" target="_blank" class="keywords">搜索结果</a>的匹配总数估算值
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){

// 创建文档对象
$doc = new XSDocument;
$doc->setFields($data);

// <a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>或更新到索引<a href="/tag/shujuku/" target="_blank" class="keywords">数据库</a>中
if(!$update){
  $this->_xindex->add($doc);
}else{
  $this->_xindex->update($doc);
}

}

public function delete($idArray){

//<a href="/tag/shanchu/" target="_blank" class="keywords">删除</a>索引(主键<a href="/tag/shanchu/" target="_blank" class="keywords">删除</a>array('1','2','3'))
$this->_xindex->del($idArray); 

}

public function addSynonym($word1,$word2){

$this->_xindex->addSynonym($word1,$word2);

}

public function clearIndex(){

$this->_xindex->clean();

}

}

?>

以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助。

猜你在找的PHP相关文章