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



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

def commit
  @updates = 0
  connection.commit
end

#commit_if_dirtyObject

See Sunspot.commit_if_dirty



132
133
134
# File 'lib/sunspot/session.rb', line 132

def commit_if_dirty
  commit if dirty?
end

#dirty?Boolean

See Sunspot.dirty?

Returns:

  • (Boolean)


125
126
127
# File 'lib/sunspot/session.rb', line 125

def dirty?
  @updates > 0
end

#index(*objects) ⇒ Object

See Sunspot.index

– FIXME The fact that we have to break this out by class and index each

class separately is artificial, imposed by the fact that indexers
are initialized with a particular setup, and are responsible for
sending add messages to Solr. It might be worth considering a
singleton indexer (per session) and have the indexer itself find
the appropriate setup to use for each object.


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

def index(*objects)
  objects.flatten!
  @updates += objects.length
  objects.group_by { |object| object.class }.each_pair do |clazz, objs|
    indexer_for(objs.first).add(objs)
  end
end

#index!(*objects) ⇒ Object

See Sunspot.index!



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

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

#new_search(*types) ⇒ Object

See Sunspot.new_search



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

def new_search(*types)
  types.flatten!
  Search.new(connection, Query.new(types, @config))
end

#remove(*objects) ⇒ Object

See Sunspot.remove



82
83
84
85
86
87
88
# File 'lib/sunspot/session.rb', line 82

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

#remove!(*objects) ⇒ Object

See Sunspot.remove!



93
94
95
96
# File 'lib/sunspot/session.rb', line 93

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

#remove_all(*classes) ⇒ Object

See Sunspot.remove_all



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sunspot/session.rb', line 101

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!



117
118
119
120
# File 'lib/sunspot/session.rb', line 117

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

#search(*types, &block) ⇒ Object

See Sunspot.search



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

def search(*types, &block)
  options = types.last.is_a?(Hash) ? types.pop : {}
  search = new_search(*types)
  search.query.build(&block) if block
  search.query.options = options
  search.execute!
end