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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/redis/search/index.rb', line 55

def initialize(options = {})
  # default data
  @condition_fields    = []
  @exts                = []
  @aliases             = []
  @prefix_index_enable = false

  # set attributes value from params
  options.keys.each do |k|
    self.send("#{k}=", options[k])
  end
  @aliases << self.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

#prefix_index_enableObject

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

#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



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

.redisObject



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

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

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

    # remove set for prefix index key
    self.redis.srem(Search.mk_sets_key(type,options[:title]),options[: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.pinyin_match
    # covert Chinese to pinyin to as an index
    pinyin_full = Search.split_pinyin(title)
    pinyin_first = pinyin_full.collect { |p| p[0] }.join("")
    words += pinyin_full
    words << pinyin_first
    pinyin_full = nil
    pinyin_first = nil
  end
  words.uniq
end

Instance Method Details

#redisObject

end class << self



51
52
53
# File 'lib/redis/search/index.rb', line 51

def redis
  self.class.redis
end

#saveObject



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