PHP Encapsulation Search Class instance

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.