• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Rails App 运用 Redis 构建高性能的实时搜索
 

Rails App 运用 Redis 构建高性能的实时搜索

on

  • 19,595 views

 

Statistics

Views

Total Views
19,595
Views on SlideShare
8,329
Embed Views
11,266

Actions

Likes
58
Downloads
685
Comments
2

42 Embeds 11,266

http://blog.nosqlfan.com 8074
http://www.oschina.net 1338
http://huacnlee.com 822
http://blog.51osos.com 193
http://qplus.yuemee.com 184
http://feed.feedsky.com 128
http://wap.oschina.net 119
http://www.kuqin.com 47
http://iredis.com 41
http://www.iredis.com 35
http://zhuaxia.com 31
http://reader.youdao.com 30
http://127.0.0.1 27
http://www.woshifans.info 27
http://xianguo.com 26
http://www.note4u.net 21
http://www.zhuaxia.com 20
http://blog.newitfarmer.com 15
http://blog.woshifans.info 15
http://cache.baidu.com 15
http://m.oschina.net 15
http://codingcool.com 9
http://coderwall.com 5
http://ask.test.51js.com 3
http://www.hanrss.com 3
http://www.yuemee.com 3
http://seekr-artemis.heroku.com 2
http://news.uplook.cn 2
http://note.sdo.com 2
http://feeds.feedburner.com 2
http://www.r66r.net 1
https://twitter.com 1
http://cache.baiducontent.com 1
http://us-w1.rockmelt.com 1
https://abs.twimg.com 1
http://reader.googleusercontent.com 1
https://si0.twimg.com 1
http://a0.twimg.com 1
http://translate.googleusercontent.com 1
http://vm-192-168-12-28.shengyun.grandcloud.cn 1
http://184.82.255.26 1
http://xue.uplook.cn 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • very good。
    Are you sure you want to
    Your message goes here
    Processing…
  • Best one
    Hope you are in good health. My name is AMANDA . I am a single girl, Am looking for reliable and honest person. please have a little time for me. Please reach me back amanda_n14144@yahoo.com so that i can explain all about myself .
    Best regards AMANDA.
    amanda_n14144@yahoo.com
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Rails App 运用 Redis 构建高性能的实时搜索 Rails App 运用 Redis 构建高性能的实时搜索 Presentation Transcript

  • Rails App 运 Redis 构 实时 华顺
  • Name: 华顺 (Jason Lee)Twitter: @huacnleeGithub: http://github.com/huacnlee MED
  • 讲 们!
  • Background• (zheye.org)这 ;• 实现 Quora 样• Ruby on Rails 发 MongoDB 库• 词•
  • • 键盘输 间 应 结• MongoDB• 杂 查询 单 为•• 词• 实时•
  • 为 Sphinx 项• 查询 满 键 间 应•对 MongoDB 暂 现 组•
  • 实现 set keys * 键词* mgetclass Ask after_create do key = "quora:#{self.title.downcase}" $redis.set(key,{:id => self.id,:title => self.title, :type => self.type}) end before_destroy do $redis.del("quora:#{self.title_was.downcase}") end def search(text,limit = 10) words = RMMSeg.split(text) keys = $redis.keys("*#{words.collect(&:downcase).join("*")}*")[0,limit] result = $redis.mget(*keys) items = [] result.each do |r| items << JSON.parse(r) end items.sort { |b,a| a[type] <=> b[type] } return items endend
  • 问题• 10 +• 词 顺 输 查询•
  • 运 Redis 键词 缀SADD Sets SREM ZADD Sorted SetsSINTER SUNION ZRANK ZRANGE 实 HSET Hashes HDEL HMGET
  • Redis-Search 结构
  • :Ask{ id : 1, title : Ruby on Rails 为 , score : 4 }{ id : 2, title : Ruby 编 应该 书 , score : 20 }{ id : 3, title : Ruby Python ? , score : 13 }{ id : 4, title : Python 发应该 发 较 , score : 5 }Topic prefix_index_enable = true{ id : 1, name : Ruby , score : 5 }{ id : 2, name : Rails , score : 18 }{ id : 3, name : Rubies, score : 10 }{ id : 4, name : Rake, score : 4 }{ id : 5, name : Python , score : 2 }
  • 缀 键词 Score Sorted Sets Sets Sets topic:rails 1. r [2] ask:_score_:1 2. ra ask:rails 4 3. rai [1] ask:_score_:2 4. rail topic:ruby 20 [1] ask:_score_:3 5. rails* ask:ruby 13 6. rak [1,2,3] ask:_score_:4 7. rake* 5 8. ru topic:rails [4] topic:_score_:1 9. rub 18 topic:rubies 10.rubi topic:_score_:2 [5] 11.rubie 10 ask:python 12.rubies* [3,4] topic:_score_:3 13.ruby* ask: 4 topic:_score_:4‣* 项 实际词 [1,2,4] 2‣ 动 ...... ......
  • 实际 HashesTopictopic:1 { id : 1, name : Ruby }topic:2 { id : 2, name : Rails }topic:3 { id : 3, name : Rubies }topic:4 { id : 4, name : Rake }topic:5 { id : 5, name : Python }Askask:1 { id : 1, title : Ruby on Rails 为 }ask:2 { id : 2, title : Ruby 编 应该 书 }ask:3 { id : 3, title : Ruby Python ? }ask:4 { id : 4, title : Python 发应该 发 较 }
  • 缀 过 输 r ru rub ruby redis> ZRANK r 标 1 8 9 13 1. r 2. ra redis> ZRANGE 1 100+1 3. rai 4. rail 标1 101 间 缀 带* 项 5. rails* 6. rak [rails,rake,rubies,ruby] [rubies,ruby] [ruby] 7. rake* 8. ru 9. rub键词 redis> SUNIONSTORE topic:rubies+ruby topic:rubies topic:ruby 10. rubi 11. rubie 12. rubies* redis> SORT topic:rubies+ruby BY topic:_score_:* DESC LIMIT 0 10 13. ruby*redis-search [2,3,1,4] [2,1] [1] redis> HMGET ask 2,3,1,4 { 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 Ruby 书 词 [ruby] [ruby, ] [ruby, ,书 ] redis> SINTERSTORE ask:ruby+ +书 ask:ruby ask: ask:书 (in Redis) [1,2,3] [1,2] [2] redis> SORT ask:ruby+ +书 BY ask:_score_:* DESC LIMIT 0 10编 redis-search [2,3,1] [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 }
  • so...
  • Redis-SearchActiveRecord
  • Redis-Search• iMac 100 + 达 10ms/ 应 ;• 实时• 词 (rmmseg-cpp)• 缀• No-SQL - 查询 库• 汉语 (chinese_pinyin)
  • Redis-Search• 针对 )• 选项• = > < ...
  • 应 场•• 户••• Tag• 书 电
  • How to use it?
  • Gemfilegem redis,>= 2.1.1gem chinese_pinyin, 0.4.1gem rmmseg-cpp-huacnlee, 0.2.9gem redis-namespace,~> 1.1.0gem redis-search, 0.7.0shell> bundle install
  • config/initializers/redis_search.rbrequire "redis"require "redis-namespace"require "redis-search"redis = Redis.new(:host => "127.0.0.1",:port => "6379")redis.select(3)# 设 间 项 发redis = Redis::Namespace.new("your_app_name:search", :redis => redis)Redis::Search.configure do |config| config.redis = redis # 缀 阀值 设 缀 长 config.complete_max_length = 100 # 启 config.pinyin_match = trueend
  • Modelclass User include Mongoid::Document include Redis::Search field :name field :tagline field :email field :followers_count, :type => Integer, :default => 0 field :sex, :type => Integer, :default => 0 # 启 Model # title_field # prefix_index_enable # score_field # condition_fields # ext_fields Hash , 为 redis-search 查询 库显 请 义 这 redis_search_index(:title_field => :name, :prefix_index_enable => true, :score_field => :followers_count, :condition_fields => [:sex] :ext_fields => [:email,:tagline])end
  • Redis-Search Create, Update, Destroy 时 动Redis Hash 问题
  • 查询 缀 :rails c> Redis::Search.complete(User, hua, :conditions => {:sex => 1}, :limit => 20) 词 :rails c> Redis::Search.query(Ask, Ruby 发, :conditions => {:state => 1}, :limit => 20)
  • 项http://github.com/huacnlee/redis-search
  • Thanks