Module: Spinel::Searcher

Included in:
Client
Defined in:
lib/spinel/searcher.rb

Instance Method Summary collapse

Instance Method Details

#search(term, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spinel/searcher.rb', line 4

def search term, options = {}
  options = { limit: Spinel.match_limit, cache: true }.merge(options)

  words = squish(term).split.reject{|w| w.size < Spinel.min_complete}.sort
  return [] if words.empty?

  tmp_cachekey = cachekey(words)

  unless options[:cache] && Spinel.redis.exists(tmp_cachekey)
    interkeys = words.map{ |w| index w }
    Spinel.redis.zinterstore(tmp_cachekey, interkeys)
    Spinel.redis.expire(tmp_cachekey, Spinel.cache_expire)
  end

  ids = Spinel.redis.zrevrange(tmp_cachekey, 0, options[:limit] - 1)
  ids.empty? ? [] : Spinel.redis.hmget(database, *ids).compact.map{|json| MultiJson.decode(json)}

end