Home > Backend Development > PHP Tutorial > PHP implementation of search class encapsulation example, PHP implementation of encapsulation example_PHP tutorial

PHP implementation of search class encapsulation example, PHP implementation of encapsulation example_PHP tutorial

WBOY
Release: 2016-07-12 08:55:22
Original
997 people have browsed it

php implementation of search class encapsulation example, php implementation encapsulation example

This article shares an example of php implementation of search class encapsulation for your reference, the specific content is as follows

<&#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

The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

Articles you may be interested in:

  • PHP site search and highlight keyword implementation code
  • php ajax imitates Baidu search drop-down automatic prompt box (with examples )
  • Code written in PHP to obtain the crawling records of each search spider
  • PHP function to obtain the source of search engine keywords (supports search engines such as Baidu and Google)
  • Youdao Search API interface (PHP application) with IP138 IP
  • PHP blocks spider access codes and HTTP_USER_AGENT of common search engines
  • How to search for associated array key values ​​and obtain them in php
  • ThinkPHP’s method to keep pagination in search status
  • jquery php implements automatic prompting of the search box
  • ThinkPHP’s method to keep homepage paging and search pages in conditional paging when searching

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117054.htmlTechArticlephp implementation search class encapsulation example, php implementation encapsulation example This article shares with you php implementation search class encapsulation example, for everyone For your reference, the specific content is as follows php/** * SoClass.php * Index and search...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template