Home > php教程 > php手册 > body text

php实现搜索类封装示例,php实现封装示例

WBOY
Release: 2016-06-13 08:42:47
Original
931 people have browsed it

php实现搜索类封装示例,php实现封装示例

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

<&#63;php
/**
 * SoClass.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); //支持同义词搜索,默认打开
    $xs->search->setLimit($row, $jnum); //设置返回结果最多为 5 条,并跳过前 10 条
    $docs = $xs->search->search(); //执行搜索,将搜索结果文档保存在 $docs 数组中    
    $count = $xs->search->count(); //获取搜索结果的匹配总数估算值
    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);
 
    // 添加或更新到索引数据库中
    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();
  }
 
}
 
&#63;>
Copy after login

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

您可能感兴趣的文章:

  • php站内搜索并高亮显示关键字的实现代码
  • php+ajax做仿百度搜索下拉自动提示框(有实例)
  • PHP写的获取各搜索蜘蛛爬行记录代码
  • PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)
  • 有道搜索和IP138的IP的API接口(PHP应用)
  • PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT
  • php中怎么搜索相关联数组键值及获取之
  • ThinkPHP让分页保持搜索状态的方法
  • jquery+php实现搜索框自动提示
  • Thinkphp搜索时首页分页和搜索页保持条件分页的方法
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!