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.