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
23
# File 'lib/sunspot/session.rb', line 18

def initialize(config = Configuration.build, connection = nil)
  @config = config
  yield(@config) if block_given?
  @connection = connection
  @updates = 0
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!



55
56
57
58
# File 'lib/sunspot/session.rb', line 55

def commit
  @updates = 0
  connection.commit
end

#commit_if_dirtyObject

See Sunspot.commit_if_dirty



113
114
115
# File 'lib/sunspot/session.rb', line 113

def commit_if_dirty
  commit if dirty?
end

#dirty?Boolean

See Sunspot.dirty?

Returns:

  • (Boolean)


106
107
108
# File 'lib/sunspot/session.rb', line 106

def dirty?
  @updates > 0
end

#index(*objects) ⇒ Object

See Sunspot.index



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

def index(*objects)
  objects.flatten!
  @updates += objects.length
  for object in objects
    setup_for(object).indexer(connection).add(object)
  end
end

#index!(*objects) ⇒ Object

See Sunspot.index!



47
48
49
50
# File 'lib/sunspot/session.rb', line 47

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

#remove(*objects) ⇒ Object

See Sunspot.remove



63
64
65
66
67
68
69
# File 'lib/sunspot/session.rb', line 63

def remove(*objects)
  objects.flatten!
  @updates += objects.length
  for object in objects
    setup_for(object).indexer(connection).remove(object)
  end
end

#remove!(*objects) ⇒ Object

See Sunspot.remove!



74
75
76
77
# File 'lib/sunspot/session.rb', line 74

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

#remove_all(*classes) ⇒ Object

See Sunspot.remove_all



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sunspot/session.rb', line 82

def remove_all(*classes)
  classes.flatten!
  if classes.empty?
    @updates += 1
    Indexer.remove_all(connection)
  else
    @updates += classes.length
    for clazz in classes
      Setup.for(clazz).indexer(connection).remove_all
    end
  end
end

#remove_all!(*classes) ⇒ Object

See Sunspot.remove_all!



98
99
100
101
# File 'lib/sunspot/session.rb', line 98

def remove_all!(*classes)
  remove_all(*classes)
  commit
end

#search(*types, &block) ⇒ Object

See Sunspot.search



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

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