|
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 }
|
|
|