Class: Redis::Search::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/redis-search/index.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  # default data
  @condition_fields    = []
  @exts                = []
  @aliases             = []

  # set attributes value from params
  options.keys.each do |k|
    send("#{k}=", options[k])
  end
  @aliases << title
  @aliases.uniq!
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



5
6
7
# File 'lib/redis-search/index.rb', line 5

def aliases
  @aliases
end

#condition_fieldsObject

Returns the value of attribute condition_fields.



5
6
7
# File 'lib/redis-search/index.rb', line 5

def condition_fields
  @condition_fields
end

#extsObject

Returns the value of attribute exts.



5
6
7
# File 'lib/redis-search/index.rb', line 5

def exts
  @exts
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/redis-search/index.rb', line 5

def id
  @id
end

#scoreObject

Returns the value of attribute score.



5
6
7
# File 'lib/redis-search/index.rb', line 5

def score
  @score
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/redis-search/index.rb', line 5

def title
  @title
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/redis-search/index.rb', line 5

def type
  @type
end

Class Method Details

.bmObject



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

.redisObject



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(options = {})
  type = options[:type]

  redis.pipelined do
    redis.hdel(type, options[:id])
    redis.del(Search.mk_score_key(type, options[:id]))

    words = split_words_for_index(options[:title])
    words.each do |word|
      redis.srem(Search.mk_sets_key(type, word), options[:id])
    end

    # remove set for prefix index key
    redis.srem(Search.mk_sets_key(type, options[:title]), options[:id])
  end
end

Instance Method Details

#redisObject

end class << self



38
39
40
# File 'lib/redis-search/index.rb', line 38

def redis
  self.class.redis
end

#saveObject



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