利用redis完成自动补全搜索功能(二)

2023-07-29,,

  前面介绍了自动完成的大致思路,现在把搜索次数的功能也结合上去。我采用的是hash表来做的,当然也可以在生成分词的时候,另外一个有序集合来维护排序, 然后2个有序集合取交集即可。这里介绍hash的方式来实现。

  产生分词 dist.php,此脚本可用linux的定时任务执行。

<?php
require './redis.php'; //分词
$words = ['花讯','nba','nba直播','nba赛事','nba季后赛','nba录像','花讯品牌','花讯女装','花','n']; //利用管道
Cache::getInstance()->pipeline();
foreach ($words as $val) { $len = mb_strlen($val,'utf-8');
for ($i=0;$i<=$len;$i++){ $key = mb_substr($val,0,$i+1,'utf-8');
if ($i == $len) {
$key .= '*';
//初始化关键词搜索次数
Cache::getInstance()->hSet("search:hits",$key,0);
}
//分词加入有序集合
Cache::getInstance()->zAdd('search',0,$key);
} } Cache::getInstance()->exec();

  自动提示 complete.php

<?php
require './redis.php'; $key = isset($_GET['key']) ? $_GET['key'] : 'nb' ; //获取索引
$index = Cache::getInstance()->zRank('search',$key);
//获取以某字符串开头的集合 并格式化
$res = Cache::getInstance()->zRange('search',$index,-1);
if (!empty($res)) { $arr = []; foreach ($res as $val) {
//strstr包含 (前缀匹配 开头即可)
if (strpos($val, $key) === 0 && strrev($val)[0] == '*') {
$arr[] = $val;
}
} //获取点击次数
if ( $result = Cache::getInstance()->hMget("search:hits", $arr) ){
//排序即可
arsort($result);
echo '<pre/>';
print_r($result);
}
}

 客户端请求接口后,只需要把最后一位标识符(*)截取渲染即可。

利用redis完成自动补全搜索功能(二)的相关教程结束。

《利用redis完成自动补全搜索功能(二).doc》

下载本文的Word格式文档,以方便收藏与打印。