Sha256: 24895f30798a2729fee7b878d8289c231fdb0e91c7c79c8b2606bc8a236e9ddf

Contents?: true

Size: 997 Bytes

Versions: 2

Compression:

Stored size: 997 Bytes

Contents

module Soulmate
  class Matcher < Base

    def matches_for_term(term, options = {})
      options = { :limit => Soulmate.max_results, :cache => true }.merge(options)

      words = normalize(term).split(' ').reject do |w|
        w.size < Soulmate.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, Soulmate.cache_time)
      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

2 entries across 2 versions & 1 rubygems

Version Path
soulmate_rails-0.3.2 lib/soulmate/matcher.rb
soulmate_rails-0.3.1 lib/soulmate/matcher.rb