基于Redis的快速检索实现


基于Redis的快速检索实现 < 杨昌明 > 背景 • 瑞知社区的问题、回答以及用户的搜索 • 存储使用Redis • 数据量 304000 question 688000! answer 150 user 需求 •支持中文分词检索 •支持逐字匹配 •支持中文全拼检索(非首字母) •支持自定义检索结果排序 •支持检索结果分页 •实时更新 •按键瞬间响应 Sorted Sets ZADD ZRANK ZRANGE 逐字匹配索引 SINTER SUNION Sets SADD SREM 关键词匹配索引 Hashes HMGET HDEL HSET 实体数据 SORT –Johnny Appleseed “需求 ” Sorted Sets Sets topic:rails [2] ask:rails [1] topic:ruby [1] ask:ruby [1,2,3] topic:rails [4] topic:rubies [5] ask:python [3,4] ask: [1,2,4] ...... Score ask:_score_:1 4 ask:_score_:2 20 ask:_score_:3 13 ask:_score_:4 5 topic:_score_:1 18 topic:_score_:2 10 topic:_score_:3 4 topic:_score_:4 2 ...... Sets 1.  r 2.  ra 3.  rai 4.  rail •  rails* 1.  rak •  rake* •  ru •  rub •  rubi •  rubie •  rubies* •  ruby* ‣  * ‣  r 1 [rails,rake,rubies,ruby] ru 8 [rubies,ruby] ruby 13 [ruby] 1 101 * redis> ZRANGE 1 100+1 redis> SORT topic:rubies+ruby BY topic:_score_:* DESC LIMIT 0 10 [2,3,1,4] redis-search [2,1] [1] redis> HMGET ask 2,3,1,4 1.  r 2.  ra 3.  rai 4.  rail •  rails* 1.  rak •  rake* •  ru •  rub •  rubi •  rubie •  rubies* •  ruby* redis> ZRANK r rub 9 redis> SUNIONSTORE topic:rubies+ruby topic:rubies topic:ruby { 'id' : 2, 'name' : 'Rails' , 'score' : 18 } { 'id' : 3, 'name' : 'Rubies', 'score' : 10 } { 'id' : 1, 'name' : 'Ruby' , 'score' : 5 }{ 'id' : 4, 'name' : 'Rake', 'score' : 4 } http://antirez.com/post/autocomplete-with-redis.html : Ruby [ruby] [1,2,3] Ruby [ruby, ] [1,2] Ruby [ruby, , ] [2] (in Redis) redis> SINTERSTORE ask:ruby+ + ask:ruby ask: ask: redis> SORT ask:ruby+ + BY ask:_score_:* DESC LIMIT 0 10 [2,3,1] redis-search [2,1] [2] redis> HMGET ask 2,3,1 { 'id' : 2, 'title' : 'Ruby ', 'score' : 20 } { 'id' : 3, 'title' : 'Ruby Python ?' , 'score' : 13 } { 'id' : 1, 'title' : 'Ruby on Rails ' , 'score' : 4 } SUNIONSTORE 中文搜索: Search consume: [ 630 ] ms 英文搜索: Search consume: [ 228 ] ms 拼音搜索: Search consume: [ 723 ] ms 145315 keywords index 185475 prefix index 空间换时间 [ 增加拼音检索关联数据结构,每个拼音对应一个set,存储该拼音对应的所有汉字 ] 中文搜索: Search consume: [ 258 ] ms 英文搜索: Search consume: [ 241 ] ms 拼音搜索: Search consume: [ 262 ] ms 145315 keywords index 185475 prefix index API public void keywordsIndex(String type, String words, String id) public void keywordsIndex(String type, String sortedKey, String sortedValue, String words, String id) public PageVO searchByPagination(String type, String sortedKey,int sortedType, int offset, String keywords) public Map> search(String keywords) 谢 谢
还剩13页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享pdf获得金币 ] 1 人已下载

下载pdf