Sha256: 3a85997f7e05f7f1960b055eae42ce7d74adf3df4b9a4eabf2b3b31e1639276b

Contents?: true

Size: 988 Bytes

Versions: 4

Compression:

Stored size: 988 Bytes

Contents

module Soulmate
  class Matcher < Base

    def matches_for_term(term, options = {})
      options = { :limit => 5, :cache => true }.merge(options)
      
      words = normalize(term).split(' ').reject do |w|
        w.size < MIN_COMPLETE or Soulmate.stop_words.include?(w)
      end.sort

      return [] if words.empty?

      cachekey = "#{cachebase}:" + words.join('|')

      if !options[:cache] || !Soulmate.redis.exists(cachekey)
        interkeys = words.map { |w| "#{base}:#{w}" }
        Soulmate.redis.zinterstore(cachekey, interkeys)
        Soulmate.redis.expire(cachekey, 10 * 60) # expire after 10 minutes
      end

      ids = Soulmate.redis.zrevrange(cachekey, 0, options[:limit] - 1)
      if ids.size > 0
        results = Soulmate.redis.hmget(database, *ids)
        results = results.reject{ |r| r.nil? } # handle cached results for ids which have since been deleted
        results.map { |r| MultiJson.decode(r) }
      else
        []
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
soulmate_rails-0.3.0 lib/soulmate/matcher.rb
soulmate_rails-0.2.1 lib/soulmate/matcher.rb
soulmate_rails-0.2.1.beta lib/soulmate/matcher.rb
soulmate_rails-0.2.0.alpha lib/soulmate/matcher.rb