lib/flare/session.rb in flare-1.6.1 vs lib/flare/session.rb in flare-1.7.0

- old
+ new

@@ -1,8 +1,7 @@ module Flare class Session - RESULT_LIMIT = 1000 PER_PAGE = 16 def initialize(url) @connection = RSolr.connect(:url => url) end @@ -65,85 +64,43 @@ end private def execute(*args) options = args.extract_options! - - options.assert_valid_keys(:q, :fq, :types, :page, :per_page, :limit, :fl, :sort, :facet, :mlt, :mm) - + options.reverse_merge!({ - :page => 1, - :per_page => PER_PAGE, - :limit => RESULT_LIMIT, - :fields => '* score', + :start => 0, + :rows => PER_PAGE }) - - query = { - :q => Array(options[:q] || (args.blank? ? "*:*" : args)).flatten, - :fq => Array(options[:fq]).flatten, - :fl => options[:fields], - :start => start = (options[:page] -1) * options[:per_page], - :rows => options[:per_page], - :sort => options[:sort] - } - if options[:facet] - query["facet"] = true - query["facet.field"] = options[:facet][:fields] - query["facet.query"] = options[:facet][:queries] - query["facet.mincount"] = options[:facet][:mincount] || 1 - query["facet.limit"] = options[:facet][:limit] - - query["facet.missing"] = @params[:facet][:missing] - query["facet.mincount"] = @params[:facet][:mincount] - query["facet.prefix"] = @params[:facet][:prefix] - query["facet.offset"] = @params[:facet][:offset] - query["facet.offset"] = 'count' - end - - if options[:mlt] - query['mlt'] = true - query['mlt.fl'] = Array(options[:mlt][:fields]).flatten.join(',') - query['mlt.count'] = options[:mlt][:count] if options[:mlt][:count] - end - - if options[:mm] - query['mm'] = options[:mm] - end + options[:q] = Array.wrap(options[:q] || (args.blank? ? "*:*" : args)) + options[:fq] = Array.wrap(options.delete(:fq)) if options[:types] - query[:fq] << Array(options[:types]).map {|type| "type:#{type}"}.join(" OR ") + options[:fq] << Array.wrap(options.delete(:types)).map {|type| "type:#{type}"}.join(" OR ") end Flare.log(<<-SOLR.squish) \e[4;32mSolr Query:\e[0;1m - #{query[:q].join(', ')} - #{"(#{query[:fq].join(' AND ')})," if query[:fq] } - sort: #{query[:sort]} - start: #{query[:start]}, - rows: #{query[:rows]} + #{options[:q].join(', ')} + #{"(#{options[:fq].join(' AND ')})," if options[:fq] } + sort: #{options[:sort]} + start: #{options[:start]}, + rows: #{options[:rows]} SOLR - response = connection.select(query) - response[:request] = query - response[:request][:page] = options[:page] - response[:request][:per_page] = options[:per_page] + response = connection.select(options) + response[:request] = options + response[:request][:page] = options[:start] + 1 + response[:request][:per_page] = options[:rows] response.with_indifferent_access end - + def ensure_searchable(*objects) Array(objects).flatten.select { |object| object.class.searchable? } end - - def prepare_query(query) - query[:page] ||= query[:page] ? query[:page].to_i : 1 - query[:per_page] ||= PER_PAGE - query[:limit] ||= RESULT_LIMIT - query[:fields] ||= '* score' - query - end - + # I dont like this... Can we move it to hash if a library like ActiveSupport doesn't already have it? def symbolize_keys(hash) hash.inject({}){|result, (key, value)| new_key = case key when String then key.to_sym