PHP+redis實現超迷你全文檢索


2014年10月31日 11:45:39

情景: 我們平台有好多游戲, 運營的同事在查詢某一款游戲的時候, 目前使用的是html的select下拉列表的展現形式, 運營的同事得一個個去找,然后選中,耗時又費眼

效果: 輸入"三國"或者"國三", 將自動列出所有包含"三國"的游戲名字, 輸入不限順序; 例如輸入"殺三國",仍然會將"三國殺"這款游戲找出來

實現: 我用redis的集合+PHP的array_intersect()和mb系列函數, 實現了一個超迷你的全文檢索功能

原理: (大道不過兩三言,說穿不值一文錢,哈哈)

1, 將所有的游戲名字讀出來,拆分成單個漢字

2, 將這些漢字作為redis集合的鍵,寫入redis,每個集合里的值是所有那些游戲名字中包含此漢字的游戲的id

3, 當用戶輸入文字的時候通過ajax異步請求,將用戶輸入傳給PHP

4, 將輸入的文字拆分成單個漢字, 分別找到這些漢字在redis中的集合值

5, 取出來,求交集,就找到了同時包含這幾個漢字的游戲的id

6, 最后到數據庫里查出來相應的游戲信息即可

缺點: 刪除數據不方便

 PHP寫入redis和檢索的代碼:

 1     //自動補全
 2     //不限輸入漢字的前后順序: 輸入"國三殺" => 輸出 "三國殺"
 3     function getAutoComplate()
 4     {
 5         //$word = $this->input->post('word');
 6         $word = '三國';
 7         if (empty($word)) {
 8             exit('0');
 9         }
10         $intWordLength = mb_strlen($word, 'UTF-8');
11 
12         $this->load->library('iredis');
13         if (1 == $intWordLength) {
14             $arrGid = $this->iredis->getAutoComplate($word);
15         } else {
16             $arrGid = array();
17             for ($i=0; $i < $intWordLength; $i++) {
18                 $strOne = mb_substr($word, $i, 1, 'UTF-8');
19                 $arrGidTmp = $this->iredis->getAutoComplate($strOne);
20                 $arrGid = empty($arrGid) ? $arrGidTmp : array_intersect($arrGid, $arrGidTmp); //求交集,因為傳入的參數個數不確定,因此不能直接求交集
21             }
22         }
23 
24         $arrGame = $this->gamemodel->getGameNameForAutoComplate($arrGid);
25         // var_dump($arrGame);exit;
26         $jsonGame = json_encode($arrGame);
27         exit($jsonGame);
28     }
29 
30     //自動補全, 建立索引
31     function setAutoComplate()
32     {
33         $arrGame = $this->gamemodel->getAllGameNameForAutoComplate();
34         $arrIndex = array();
35         foreach ($arrGame as $gid => $gname) {
36             $intGnameLength = mb_strlen($gname, 'UTF-8');
37             for ($i=0; $i < $intGnameLength; $i++) {
38                 $strOne = mb_substr($gname, $i, 1, 'UTF-8');
39                 $arrIndex[$strOne][] = $gid;
40             }
41         }
42         
43         $this->load->library('iredis');
44         foreach ($arrIndex as $word => $arrGid) {
45             foreach ($arrGid as $gid) {
46                 $this->iredis->setAutoComplate($word, $gid);
47             }
48         }
49         
50     }

 操作redis的方法

 1     //自動補全功能
 2     public function setAutoComplate($key, $value)
 3     {
 4         $youxikey = 'youxi_'.$key;
 5         $this->sAdd($youxikey, $value);
 6     }
 7 
 8     //自動補全功能
 9     public function getAutoComplate($key)
10     {
11         $youxikey = 'youxi_'.$key;
12         return $this->sMembers($youxikey);
13     }

 

 

Finger PHP 框架


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM