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.
Sunspot::Configuration object for this session
# File lib/sunspot/session.rb, line 105 def atomic_update(clazz, updates = {}) @adds += updates.keys.length indexer.add_atomic_update(clazz, updates) end
# File lib/sunspot/session.rb, line 113 def atomic_update!(clazz, updates = {}) atomic_update(clazz, updates) commit end
See Sunspot.batch
# File lib/sunspot/session.rb, line 240 def batch indexer.start_batch yield indexer.flush_batch end
See Sunspot.commit
# File lib/sunspot/session.rb, line 121 def commit(soft_commit = false) @adds = @deletes = 0 connection.commit :commit_attributes => {:softCommit => soft_commit} end
See Sunspot.commit_if_delete_dirty
# File lib/sunspot/session.rb, line 233 def commit_if_delete_dirty(soft_commit = false) commit soft_commit if delete_dirty? end
# File lib/sunspot/session.rb, line 219 def commit_if_dirty(soft_commit = false) commit soft_commit if dirty? end
# File lib/sunspot/session.rb, line 226 def delete_dirty? @deletes > 0 end
See Sunspot.dirty?
# File lib/sunspot/session.rb, line 212 def dirty? (@deletes + @adds) > 0 end
See Sunspot.index
# File lib/sunspot/session.rb, line 88 def index(*objects) objects.flatten! @adds += objects.length indexer.add(objects) end
See Sunspot.index!
# File lib/sunspot/session.rb, line 97 def index!(*objects) index(*objects) commit end
# File lib/sunspot/session.rb, line 80 def more_like_this(object, *types, &block) mlt = new_more_like_this(object, *types, &block) mlt.execute end
See Sunspot.new_more_like_this
# File lib/sunspot/session.rb, line 65 def new_more_like_this(object, *types, &block) types[0] ||= object.class mlt = Search::MoreLikeThisSearch.new( connection, setup_for_types(types), Query::MoreLikeThisQuery.new(object, types), @config ) mlt.build(&block) if block mlt end
# File lib/sunspot/session.rb, line 42 def new_search(*types, &block) types.flatten! search = Search::StandardSearch.new( connection, setup_for_types(types), Query::StandardQuery.new(types), @config ) search.build(&block) if block search end
See Sunspot.optimize
# File lib/sunspot/session.rb, line 129 def optimize @adds = @deletes = 0 connection.optimize end
See Sunspot.remove
# File lib/sunspot/session.rb, line 137 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!
# File lib/sunspot/session.rb, line 161 def remove!(*objects, &block) remove(*objects, &block) commit end
# File lib/sunspot/session.rb, line 190 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
# File lib/sunspot/session.rb, line 204 def remove_all!(*classes) remove_all(*classes) commit end
# File lib/sunspot/session.rb, line 169 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
# File lib/sunspot/session.rb, line 182 def remove_by_id!(clazz, *ids) remove_by_id(clazz, ids) commit end
See Sunspot.search
# File lib/sunspot/session.rb, line 57 def search(*types, &block) search = new_search(*types, &block) search.execute end
Sessions are initialized with a Sunspot configuration and a Solr connection. Usually you will want to stick with the default arguments when instantiating your own sessions.
# File lib/sunspot/session.rb, line 32 def initialize(config = Configuration.build, connection = nil) @config = config yield(@config) if block_given? @connection = connection @deletes = @adds = 0 end