Class: Redis::Search::Index
- Inherits:
-
Object
- Object
- Redis::Search::Index
- Defined in:
- lib/redis/search/index.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#condition_fields ⇒ Object
Returns the value of attribute condition_fields.
-
#exts ⇒ Object
Returns the value of attribute exts.
-
#id ⇒ Object
Returns the value of attribute id.
-
#prefix_index_enable ⇒ Object
Returns the value of attribute prefix_index_enable.
-
#score ⇒ Object
Returns the value of attribute score.
-
#title ⇒ Object
Returns the value of attribute title.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Index
constructor
A new instance of Index.
-
#redis ⇒ Object
end class << self.
- #save ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Index
Returns a new instance of Index.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/redis/search/index.rb', line 55 def initialize( = {}) # default data @condition_fields = [] @exts = [] @aliases = [] @prefix_index_enable = false # set attributes value from params .keys.each do |k| self.send("#{k}=", [k]) end @aliases << self.title @aliases.uniq! end |
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
5 6 7 |
# File 'lib/redis/search/index.rb', line 5 def aliases @aliases end |
#condition_fields ⇒ Object
Returns the value of attribute condition_fields.
5 6 7 |
# File 'lib/redis/search/index.rb', line 5 def condition_fields @condition_fields end |
#exts ⇒ Object
Returns the value of attribute exts.
5 6 7 |
# File 'lib/redis/search/index.rb', line 5 def exts @exts end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/redis/search/index.rb', line 5 def id @id end |
#prefix_index_enable ⇒ Object
Returns the value of attribute prefix_index_enable.
5 6 7 |
# File 'lib/redis/search/index.rb', line 5 def prefix_index_enable @prefix_index_enable end |
#score ⇒ Object
Returns the value of attribute score.
5 6 7 |
# File 'lib/redis/search/index.rb', line 5 def score @score end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/redis/search/index.rb', line 5 def title @title end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/redis/search/index.rb', line 5 def type @type end |
Class Method Details
.bm ⇒ Object
43 44 45 46 47 48 |
# File 'lib/redis/search/index.rb', line 43 def bm t1 = Time.now yield t2 = Time.now puts "spend (#{t2 - t1}s)" end |
.redis ⇒ Object
8 9 10 |
# File 'lib/redis/search/index.rb', line 8 def redis Redis::Search.config.redis end |
.remove(options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/redis/search/index.rb', line 12 def remove( = {}) type = [:type] self.redis.pipelined do self.redis.hdel(type,[:id]) self.redis.del(Search.mk_score_key(type,[:id])) words = self.split_words_for_index([:title]) words.each do |word| self.redis.srem(Search.mk_sets_key(type,word), [:id]) end # remove set for prefix index key self.redis.srem(Search.mk_sets_key(type,[:title]),[:id]) end end |
.split_words_for_index(title) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/redis/search/index.rb', line 29 def split_words_for_index(title) words = Search.split(title) if Search.config. # covert Chinese to pinyin to as an index = Search.(title) = .collect { |p| p[0] }.join("") words += words << = nil = nil end words.uniq end |
Instance Method Details
#redis ⇒ Object
end class << self
51 52 53 |
# File 'lib/redis/search/index.rb', line 51 def redis self.class.redis end |
#save ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/redis/search/index.rb', line 70 def save return if @title.blank? self.redis.pipelined do data = {title: @title, id: @id, type: @type} self.exts.each do |f| data[f[0]] = f[1] end # 将原始数据存入 hashes res = self.redis.hset(@type, @id, data.to_json) # 将目前的编号保存到条件(conditions)字段所创立的索引上面 self.condition_fields.each do |field| self.redis.sadd(Search.mk_condition_key(@type,field,data[field.to_sym]), @id) end # score for search sort self.redis.set(Search.mk_score_key(@type,@id),@score) # 保存 sets 索引,以分词的单词为key,用于后面搜索,里面存储 ids self.aliases.each do |val| words = Search::Index.split_words_for_index(val) next if words.blank? words.each do |word| self.redis.sadd(Search.mk_sets_key(@type,word), @id) end end # 建立前缀索引 save_prefix_index if prefix_index_enable end end |