PHP redis实现超迷你全文检索

2025-05-27 0 46

情景: 我们平台有好多游戏, 运营的同事在查询某一款游戏的时候, 目前使用的是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);

}

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP redis实现超迷你全文检索 https://www.kuaiidc.com/73429.html

相关文章

发表评论
暂无评论