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.
-
#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.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/redis-search/index.rb', line 42 def initialize( = {}) # default data @condition_fields = [] @exts = [] @aliases = [] # set attributes value from params .keys.each do |k| send("#{k}=", [k]) end @aliases << 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 |
#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
30 31 32 33 34 35 |
# File 'lib/redis-search/index.rb', line 30 def bm t1 = Time.now yield t2 = Time.now puts "spend (#{t2 - t1}s)" end |
.redis ⇒ Object
9 10 11 |
# File 'lib/redis-search/index.rb', line 9 def redis Redis::Search.config.redis end |
.remove(options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/redis-search/index.rb', line 13 def remove( = {}) type = [:type] redis.pipelined do redis.hdel(type, [:id]) redis.del(Search.mk_score_key(type, [:id])) words = split_words_for_index([:title]) words.each do |word| redis.srem(Search.mk_sets_key(type, word), [:id]) end # remove set for prefix index key redis.srem(Search.mk_sets_key(type, [:title]), [:id]) end end |
Instance Method Details
#redis ⇒ Object
end class << self
38 39 40 |
# File 'lib/redis-search/index.rb', line 38 def redis self.class.redis end |
#save ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/redis-search/index.rb', line 56 def save return if @title.blank? redis.pipelined do data = { title: @title, id: @id, type: @type } exts.each do |f| data[f[0]] = f[1] end # 将原始数据存入 hashes res = redis.hset(@type, @id, data.to_json) # 将目前的编号保存到条件(conditions)字段所创立的索引上面 condition_fields.each do |field| redis.sadd(Search.mk_condition_key(@type, field, data[field.to_sym]), @id) end # score for search sort redis.set(Search.mk_score_key(@type, @id), @score) # 保存 sets 索引,以分词的单词为key,用于后面搜索,里面存储 ids aliases.each do |val| words = Search::Index.split_words_for_index(val) next if words.blank? words.each do |word| redis.sadd(Search.mk_sets_key(@type, word), @id) end end # 建立前缀索引 save_prefix_index end end |