Php implementation of Search Class encapsulation example _ php instance

Source: Internet
Author: User
This article mainly introduces examples of php search encapsulation. Interested parties can refer to this article to share examples of php search encapsulation for your reference. The 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 the 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');} public function query ($ keyWord, $ row = 20, $ jnum = 0) {$ xs = new XS ($ This-> _ project); $ xs-> search-> setFuzzy (); $ xs-> search-> setAutoSynonyms (); $ xs-> search-> setQuery ($ keyWord); // you can search for synonyms. $ xs-> search-> setLimit ($ row, $ jnum) is enabled by default ); // set the maximum number of returned results to 5, and skip the first 10. $ docs = $ xs-> search (); // execute the search, save the search result document in the $ docs array $ count = $ xs-> search-> count (); // obtain the total number of matching results 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 ($ nu M, $ type);} public function expanded ($ keyWord) {return $ this-> _ xsearch-> getExpandedQuery ($ keyWord);} public function lastCount () {return $ this-> _ xsearch-> getLastCount ();} public function index ($ data, $ update = 0) {// create a Document Object $ doc = new XSDocument; $ doc-> setFields ($ data); // Add or update to index database if (! $ Update) {$ this-> _ xindex-> add ($ doc);} else {$ this-> _ xindex-> update ($ doc );}} public function delete ($ idArray) {// delete an index (primary key deletes an array ('1', '2', '3 ')) $ this-> _ xindex-> del ($ idArray);} public function addSynonym ($ word1, $ word2) {$ this-> _ xindex-> addSynonym ($ word1, $ word2);} public function clearIndex () {$ this-> _ xindex-> clean () ;}}?>

The above is all the content of this article. I hope it will help you learn php programming.

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.