Redis入门--头歌实验使用Redis构建主动补全组件

打印 上一主题 下一主题

主题 1010|帖子 1010|积分 3030

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
主动补全与输入联想功能已经是大多数网站的标配,给表单加入主动补全功能大大节省了用户输入时间,而输入联想功能则起到了预测用户喜好的作用,两个功能都是提拔用户体验的利器。
本实训,我们通过实现搜索汗青、主动补全和搜索预测三大常勤奋能,领导大家编写实用的程序组件。


搜索汗青功能

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. import redis
  4. conn = redis.Redis()
  5. # 将最新搜索词记录到搜索记录列表中
  6. def add_search_history(user_id, keyword):
  7.     # 将最新搜索词记录到指定用户的搜索记录列表中
  8.     # user_id: 用户ID
  9.     # keyword: 搜索关键词
  10.     history_list = "recent:search:" + user_id
  11.     pipe = conn.pipeline()
  12.     pipe.multi()
  13.     pipe.lrem(history_list, 0, keyword)  # 移除已存在的关键词
  14.     pipe.lpush(history_list, keyword)  # 将关键词插入到列表头部
  15.     pipe.ltrim(history_list, 0, 49)  # 保留最新的50个搜索记录
  16.     pipe.execute()
  17. # 删除搜索记录列表中的指定搜索词
  18. def remove_search_history(user_id, keyword):
  19.     # 从指定用户的搜索记录列表中删除指定的搜索词
  20.     # user_id: 用户ID
  21.     # keyword: 待删除的搜索关键词
  22.     conn.lrem("recent:search:" + user_id, 0, keyword)
  23. # 获取到自动匹配的搜索词列表
  24. def fetch_autocomplete_list(user_id, prefix):
  25.     # 获取指定用户搜索记录列表中以指定前缀开头的搜索词列表
  26.     # user_id: 用户ID
  27.     # prefix: 搜索关键词前缀
  28.     candidates = conn.lrange("recent:search:" + user_id, 0, -1)
  29.     matches = [candidate.decode('utf-8') for candidate in candidates if candidate.decode('utf-8').startswith(prefix)]
  30.     return matches
复制代码
主动补全功能

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. import uuid
  4. import redis
  5. import bisect
  6. conn = redis.Redis()
  7. # 生成起始元素和结束元素
  8. def find_prefix_range(prefix):
  9.     # 根据前缀生成起始元素和结束元素,用于查找匹配的提示词
  10.     # prefix: 给定的前缀字符串
  11.     characters = "`abcdefghijklmnopqrstuvwxyz{"
  12.     posn = bisect.bisect_left(characters, prefix[-1:])
  13.     suffix = characters[(posn or 1) - 1]
  14.     return prefix[:-1] + suffix + '{', prefix + '{'
  15. # 获取匹配提示词列表
  16. def autocomplete_on_prefix(prefix):
  17.     # 根据前缀获取匹配的提示词列表
  18.     # prefix: 给定的前缀字符串
  19.     zset_name = 'autocomplete:candidates'
  20.     start, end = find_prefix_range(prefix)
  21.     identifier = str(uuid.uuid4())
  22.     start += identifier
  23.     end += identifier
  24.     conn.zadd(zset_name, {start: 0, end: 0})
  25.     sindex = conn.zrank(zset_name, start)
  26.     eindex = conn.zrank(zset_name, end)
  27.     erange = min(sindex + 9, eindex - 2)
  28.     pipe = conn.pipeline()
  29.     pipe.multi()
  30.     pipe.zrem(zset_name, start, end)
  31.     pipe.zrange(zset_name, sindex, erange)
  32.     items = pipe.execute()[-1]
  33.     return [item for item in items if '{' not in item]
复制代码
搜索预测功能

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. import redis
  4. conn = redis.Redis()
  5. # 记录搜索词频次
  6. def add_keyword_frequency(keyword):
  7.     # 根据搜索词记录频次并维护搜索词的有序集合
  8.     # keyword: 搜索词
  9.     for i in range(len(keyword)):
  10.         zset_name = "keyword:" + keyword[0:i+1]
  11.         conn.zincrby(zset_name, 1, keyword)
  12.         conn.zremrangebyrank(zset_name, 20, -1)
  13.         conn.expire(zset_name, 86400)
  14. # 获取搜索预测列表
  15. def get_search_suggestions(prefix):
  16.     # 根据前缀获取搜索预测列表
  17.     # prefix: 搜索词前缀
  18.     return conn.zrevrange("keyword:" + prefix, 0, -1)
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

盛世宏图

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表