Class: Sunspot::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/session.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Configuration.build, connection = nil) {|@config| ... } ⇒ Session

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.

Yields:



18
19
20
21
22
# File 'lib/sunspot/session.rb', line 18

def initialize(config = Configuration.build, connection = nil)
  @config = config
  yield(@config) if block_given?
  @connection = connection
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/sunspot/session.rb', line 11

def config
  @config
end

Instance Method Details

#commitObject

See Sunspot.commit!



53
54
55
# File 'lib/sunspot/session.rb', line 53

def commit
  connection.commit
end

#index(*objects) ⇒ Object

See Sunspot.index



35
36
37
38
39
40
# File 'lib/sunspot/session.rb', line 35

def index(*objects)
  objects.flatten!
  for object in objects
    setup_for(object).indexer(connection).add(object)
  end
end

#index!(*objects) ⇒ Object

See Sunspot.index!



45
46
47
48
# File 'lib/sunspot/session.rb', line 45

def index!(*objects)
  index(*objects)
  commit
end

#remove(*objects) ⇒ Object

See Sunspot.remove



60
61
62
63
64
65
# File 'lib/sunspot/session.rb', line 60

def remove(*objects)
  objects.flatten!
  for object in objects
    setup_for(object).indexer(connection).remove(object)
  end
end

#remove_all(*classes) ⇒ Object

See Sunspot.remove_all



70
71
72
73
74
75
76
77
78
79
# File 'lib/sunspot/session.rb', line 70

def remove_all(*classes)
  classes.flatten!
  if classes.empty?
    Indexer.remove_all(connection)
  else
    for clazz in classes
      Setup.for(clazz).indexer(connection).remove_all
    end
  end
end

#search(*types, &block) ⇒ Object

See Sunspot.search



27
28
29
30
# File 'lib/sunspot/session.rb', line 27

def search(*types, &block)
  types.flatten!
  Search.new(connection, @config, *types, &block).execute!
end