module Lunar class Search attr :sets, :prefix, :search_identifier def initialize(prefix, keywords) if keywords.is_a?(Hash) @sets = keywords.inject([]) { |a, (field, query)| a | Sets.new(prefix, query, field) } @search_identifier = keywords.hash else @sets = Sets.new(prefix, keywords) @search_identifier = keywords.hash end @prefix = prefix # Default finder, uses Ohm style finding @finder = lambda { |id| prefix[id] } end def results(&block) return [] if sets.empty? # TODO : cache this for X seconds, X seconds being customizable Lunar.redis.zunion dist_key, sets.size, *sets if block_given? ResultSet.new(dist_key, &block) else ResultSet.new(dist_key, &@finder) end end protected def dist_key Lunar.nest[:Results][search_identifier] end end end