0
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....
问题•       10   +•   词            顺   输   查询•
进
运        Redis       键词                                   缀SADD       Sets    SREM                                  ZADD  ...
Redis-Search   结构
:Ask{ id : 1, title : Ruby on Rails 为                   , score : 4 }{ id : 2, title : Ruby 编        应该         书     , sc...
缀             键词             Score   Sorted Sets   Sets               Sets                 topic:rails   1. r           [2...
实际                                        HashesTopictopic:1   { id : 1, name : Ruby }topic:2   { id : 2, name : Rails }to...
缀                          过     输                    r                                    ru              rub            ...
词                     过    输              Ruby                      Ruby                              Ruby              书 ...
so...
Redis-SearchActiveRecord
Redis-Search•   iMac       100   +             达   10ms/    应      ;•   实时•          词   (rmmseg-cpp)•     缀•   No-SQL -  ...
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   r...
config/initializers/redis_search.rbrequire "redis"require "redis-namespace"require "redis-search"redis = Redis.new(:host =>...
Modelclass User  include Mongoid::Document  include Redis::Search    field   :name    field   :tagline    field   :email  ...
Redis-Search  Create, Update, Destroy   时   动Redis                   Hash                       问题
查询 缀         :rails c> Redis::Search.complete(User, hua, :conditions => {:sex => 1}, :limit => 20)     词     :rails c> Red...
项http://github.com/huacnlee/redis-search
Thanks
Rails App 运用 Redis 构建高性能的实时搜索
Rails App 运用 Redis 构建高性能的实时搜索
Upcoming SlideShare
Loading in...5
×

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

21,609

Published on

Published in: Technology
3 Comments
61 Likes
Statistics
Notes
  • niubility......

    我去弄个 php 版本.....
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • very good。
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • 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
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
21,609
On Slideshare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
723
Comments
3
Likes
61
Embeds 0
No embeds

No notes for slide
  • \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
  • Transcript of "Rails App 运用 Redis 构建高性能的实时搜索"

    1. 1. Rails App 运 Redis 构 实时 华顺
    2. 2. Name: 华顺 (Jason Lee)Twitter: @huacnleeGithub: http://github.com/huacnlee MED
    3. 3.
    4. 4. 讲 们!
    5. 5. Background• (zheye.org)这 ;• 实现 Quora 样• Ruby on Rails 发 MongoDB 库• 词•
    6. 6. • 键盘输 间 应 结• MongoDB• 杂 查询 单 为•• 词• 实时•
    7. 7. 为 Sphinx 项• 查询 满 键 间 应•对 MongoDB 暂 现 组•
    8. 8. 实现 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
    9. 9. 问题• 10 +• 词 顺 输 查询•
    10. 10.
    11. 11. 运 Redis 键词 缀SADD Sets SREM ZADD Sorted SetsSINTER SUNION ZRANK ZRANGE 实 HSET Hashes HDEL HMGET
    12. 12. Redis-Search 结构
    13. 13. :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 }
    14. 14. 缀 键词 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‣ 动 ...... ......
    15. 15. 实际 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 发应该 发 较 }
    16. 16. 缀 过 输 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
    17. 17. 词 过 输 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 }
    18. 18. so...
    19. 19. Redis-SearchActiveRecord
    20. 20. Redis-Search• iMac 100 + 达 10ms/ 应 ;• 实时• 词 (rmmseg-cpp)• 缀• No-SQL - 查询 库• 汉语 (chinese_pinyin)
    21. 21. Redis-Search• 针对 )• 选项• = > < ...
    22. 22. 应 场•• 户••• Tag• 书 电
    23. 23. How to use it?
    24. 24. 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
    25. 25. 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
    26. 26. 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
    27. 27. Redis-Search Create, Update, Destroy 时 动Redis Hash 问题
    28. 28. 查询 缀 :rails c> Redis::Search.complete(User, hua, :conditions => {:sex => 1}, :limit => 20) 词 :rails c> Redis::Search.query(Ask, Ruby 发, :conditions => {:state => 1}, :limit => 20)
    29. 29. 项http://github.com/huacnlee/redis-search
    30. 30. Thanks
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×