PHP redis实现超迷你全文检索

时间:2021-09-15 14:46:14

情景: 我们平台有好多游戏, 运营的同事在查询某一款游戏的时候, 目前使用的是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
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//自动补全
  //不限输入汉字的前后顺序: 输入"国三杀" => 输出 "三国杀"
  function getAutoComplate()
  {
    //$word = $this->input->post('word');
    $word = '三国';
    if (empty($word)) {
      exit('0');
    }
    $intWordLength = mb_strlen($word, 'UTF-8');
 
    $this->load->library('iredis');
    if (1 == $intWordLength) {
      $arrGid = $this->iredis->getAutoComplate($word);
    } 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); //求交集,因为传入的参数个数不确定,因此不能直接求交集
      }
    }
 
    $arrGame = $this->gamemodel->getGameNameForAutoComplate($arrGid);
    // var_dump($arrGame);exit;
    $jsonGame = json_encode($arrGame);
    exit($jsonGame);
  }
 
  //自动补全, 建立索引
  function setAutoComplate()
  {
    $arrGame = $this->gamemodel->getAllGameNameForAutoComplate();
    $arrIndex = array();
    foreach ($arrGame as $gid => $gname) {
      $intGnameLength = mb_strlen($gname, 'UTF-8');
      for ($i=0; $i < $intGnameLength; $i++) {
        $strOne = mb_substr($gname, $i, 1, 'UTF-8');
        $arrIndex[$strOne][] = $gid;
      }
    }
    
    $this->load->library('iredis');
    foreach ($arrIndex as $word => $arrGid) {
      foreach ($arrGid as $gid) {
        $this->iredis->setAutoComplate($word, $gid);
      }
    }
    
  }

操作redis的方法

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。