module Sunspot # # A Sunspot session encapsulates a connection to Solr and a set of # configuration choices. Though users of Sunspot may manually instantiate # Session objects, in the general case it's easier to use the singleton # stored in the Sunspot module. Since the Sunspot module provides all of # the instance methods of Session as class methods, they are not documented # again here. # class Session class < {:softCommit => soft_commit} end # # See Sunspot.optimize # def optimize @adds = @deletes = 0 connection.optimize end # # See Sunspot.remove # def remove(*objects, &block) if block types = objects conjunction = Query::Connective::Conjunction.new if types.length == 1 conjunction.add_positive_restriction(TypeField.instance, Query::Restriction::EqualTo, types.first) else conjunction.add_positive_restriction(TypeField.instance, Query::Restriction::AnyOf, types) end dsl = DSL::Scope.new(conjunction, setup_for_types(types)) Util.instance_eval_or_call(dsl, &block) indexer.remove_by_scope(conjunction) else objects.flatten! @deletes += objects.length objects.each do |object| indexer.remove(object) end end end # # See Sunspot.remove! # def remove!(*objects, &block) remove(*objects, &block) commit end # # See Sunspot.remove_by_id # def remove_by_id(clazz, *ids) class_name = if clazz.is_a?(Class) clazz.name else clazz.to_s end indexer.remove_by_id(class_name, ids) end # # See Sunspot.remove_by_id! # def remove_by_id!(clazz, *ids) remove_by_id(clazz, ids) commit end # # See Sunspot.remove_all # def remove_all(*classes) classes.flatten! if classes.empty? @deletes += 1 indexer.remove_all else @deletes += classes.length classes.each { |clazz| indexer.remove_all(clazz) } end end # # See Sunspot.remove_all! # def remove_all!(*classes) remove_all(*classes) commit end # # See Sunspot.dirty? # def dirty? (@deletes + @adds) > 0 end # # See Sunspot.commit_if_dirty # def commit_if_dirty(soft_commit = false) commit soft_commit if dirty? end # # See Sunspot.delete_dirty? # def delete_dirty? @deletes > 0 end # # See Sunspot.commit_if_delete_dirty # def commit_if_delete_dirty(soft_commit = false) commit soft_commit if delete_dirty? end # # See Sunspot.batch # def batch indexer.start_batch yield indexer.flush_batch end private # # Retrieve the Solr connection for this session, creating one if it does not # already exist. # # ==== Returns # # RSolr::Connection::Base:: The connection for this session # def connection @connection ||= self.class.connection_class.connect(:url => config.solr.url, :read_timeout => config.solr.read_timeout, :open_timeout => config.solr.open_timeout) end def indexer @indexer ||= Indexer.new(connection) end def setup_for_types(types) if types.empty? raise(ArgumentError, "You must specify at least one type to search") end if types.length == 1 Setup.for(types.first) else CompositeSetup.for(types) end end end end