October 31, 2014 11:45:39
Scenario: Our platform has a lot of games, the operation of colleagues in the query of a game, the current use of the HTML Select drop-down list of the presentation form, the operation of the colleagues have to find one by one, and then select, time-consuming and cost-eye
Effect: Enter "three countries" or "country three", will automatically list all the games containing the "Three Kingdoms" name, enter an unlimited order; For example, the "Three Kingdoms" will still be found in the game "kill"
Implementation: I use the Redis collection +php array_intersect () and MB series functions to achieve a super mini full-Text search function
Principle: Avenue But two or three words, is not worth a penny, haha
1. Read all the game names and split them into individual characters
2, these Chinese characters as keys to the Redis collection, write to Redis, the value in each collection is the ID of all those games whose name contains this character
3, when the user input text through the Ajax asynchronous request, the user input to PHP
4, the input text is split into a single Chinese character, to find the set value of these Chinese characters in Redis
5, take out, seek the intersection, found the same time contains the characters of the game ID
6, finally to the database to find the corresponding game information can be
PHP writes to Redis and retrieves the code:
1 //Auto-Complete2 //Unlimited input of Chinese characters before and after order: input "country three kill" and output "Three Kingdoms Kill"3 functiongetautocomplate ()4 {5//$word=$this->input->post (' word ');6 $word= ' Three Kingdoms ';7 if(Empty($word)) {8 Exit(' 0 ');9 }Ten $intWordLength= Mb_strlen ($word, ' UTF-8 '); One A $this->load->library (' Iredis '); - if(1 = =$intWordLength) { - $arrGid=$this->iredis->getautocomplate ($word); the}Else { - $arrGid=Array(); - for($i= 0;$i<$intWordLength;$i++) { - $strOne= Mb_substr ($word,$i, 1, ' UTF-8 '); + $ARRGIDTMP=$this->iredis->getautocomplate ($strOne); - $arrGid=Empty($arrGid) ?$ARRGIDTMP:Array_intersect($arrGid,$ARRGIDTMP);//intersection, because the number of arguments passed in is indeterminate and therefore cannot be directly intersecting + } A } at - $arrGame=$this->gamemodel->getgamenameforautocomplate ($arrGid); - //Var_dump ($arrGame); exit; - $jsonGame= Json_encode ($arrGame); - Exit($jsonGame); - } in - //Auto-completion, index creation to functionsetautocomplate () + { - $arrGame=$this->gamemodel->getallgamenameforautocomplate (); the $arrIndex=Array(); * foreach($arrGame as $gid=$gname) { $ $intGnameLength= Mb_strlen ($gname, ' UTF-8 ');Panax Notoginseng for($i= 0;$i<$intGnameLength;$i++) { - $strOne= Mb_substr ($gname,$i, 1, ' UTF-8 '); the $arrIndex[$strOne][] =$gid; + } A } the + $this->load->library (' Iredis '); - foreach($arrIndex as $word=$arrGid) { $ foreach($arrGid as $gid) { $ $this->iredis->setautocomplate ($word,$gid); - } - } the -}
Ways to operate Redis
1 //Auto-complete function2 Public functionSetautocomplate ($key,$value)3 {4 $youxikey= ' Youxi_ '.$key;5 $this->sadd ($youxikey,$value);6 }7 8 //Auto-complete function9 Public functionGetautocomplate ($key)Ten { One $youxikey= ' Youxi_ '.$key; A return $this->smembers ($youxikey); -}
Php+redis for ultra-mini Full text Search