This article mainly for you to introduce the PHP implementation of the Search class encapsulation example, interested in the small partners can refer to. We hope to help you.
Specific content is as follows
<?php/** * soclass.php * Index and search class */class Soclass {private $_xindex; Private $_xsearch; Private $_project; Public function __construct ($project) {//Load Boot file require_once ' lib/xs.php '; Initialize $XS = new XS ($project); $this->_project = $project; $this->_xindex = $xs->index; $this->_xsearch = $xs->search; $this->_xsearch->setcharset (' UTF-8 '); The Public Function query ($keyWord, $row =20, $jnum =0) {$xs = new xs ($this->_project); $xs->search->setfuzzy (); $xs->search->setautosynonyms (); $xs->search->setquery ($keyWord); Support synonym search, open by default $xs->search->setlimit ($row, $jnum); Set the return result to a maximum of 5 and skip the first 10 $docs = $xs->search->search (); Perform a search to save the search results document in a $docs array $count = $xs->search->count (); Gets the total number of matches for the search result 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 (); The Public Function Hotword ($num, $type = ' lastnum ') {return $this->_xsearch->gethotquery ($num, $type); The public function expanded ($keyWord) {return $this->_xsearch->getexpandedquery ($keyWord); } public Function LastCount () {return $this->_xsearch->getlastcount (); Public Function Index ($data, $update =0) {//Create Document Object $doc = new Xsdocument; $doc->setfields ($data); Add or update to the index database if (! $update) {$this->_xindex->add ($doc); }else{$this->_xindex->update ($doc); }} Public Function Delete ($idArray) {//delete index (primary key Delete Array (' 1 ', ' 2 ', ' 3 ')) $this->_xindex->del ($idArRay); } Public Function addsynonym ($word 1, $word 2) {$this->_xindex->addsynonym ($word 1, $word 2); Public Function Clearindex () {$this->_xindex->clean (); }}?>
Related recommendations:
PHP Package GD Library
PHP Classes and constructors
Full knowledge of PHP classes and objects