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.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Configuration.build, connection = nil, master_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:



32
33
34
35
36
37
38
# File 'lib/sunspot/session.rb', line 32

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

Class Attribute Details

.connection_classObject

For testing purposes



17
18
19
# File 'lib/sunspot/session.rb', line 17

def connection_class #:nodoc:
  @connection_class ||= RSolr
end

Instance Attribute Details

#configObject (readonly)

Sunspot::Configuration object for this session



25
26
27
# File 'lib/sunspot/session.rb', line 25

def config
  @config
end

Instance Method Details

#batchObject

See Sunspot.batch



184
185
186
187
188
# File 'lib/sunspot/session.rb', line 184

def batch
  indexer.start_batch
  yield
  indexer.flush_batch
end

#commitObject

See Sunspot.commit



86
87
88
89
# File 'lib/sunspot/session.rb', line 86

def commit
  @adds = @deletes = 0
  master_connection.commit
end

#commit_if_delete_dirtyObject

See Sunspot.commit_if_delete_dirty



177
178
179
# File 'lib/sunspot/session.rb', line 177

def commit_if_delete_dirty
  commit if delete_dirty?
end

#commit_if_dirtyObject

See Sunspot.commit_if_dirty



163
164
165
# File 'lib/sunspot/session.rb', line 163

def commit_if_dirty
  commit if dirty?
end

#delete_dirty?Boolean

See Sunspot.delete_dirty?

Returns:

  • (Boolean)


170
171
172
# File 'lib/sunspot/session.rb', line 170

def delete_dirty?
  @deletes > 0
end

#dirty?Boolean

See Sunspot.dirty?

Returns:

  • (Boolean)


156
157
158
# File 'lib/sunspot/session.rb', line 156

def dirty?
  (@deletes + @adds) > 0
end

#index(*objects) ⇒ Object

See Sunspot.index



69
70
71
72
73
# File 'lib/sunspot/session.rb', line 69

def index(*objects)
  objects.flatten!
  @adds += objects.length
  indexer.add(objects)
end

#index!(*objects) ⇒ Object

See Sunspot.index!



78
79
80
81
# File 'lib/sunspot/session.rb', line 78

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

#new_search(*types) ⇒ Object

See Sunspot.new_search



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sunspot/session.rb', line 43

def new_search(*types)
  types.flatten!
  if types.empty?
    raise(ArgumentError, "You must specify at least one type to search")
  end
  setup =
    if types.length == 1
      Setup.for(types.first)
    else
      CompositeSetup.for(types)
      end
  Search.new(connection, setup, Query::Query.new(types), @config)
end

#remove(*objects) ⇒ Object

See Sunspot.remove



94
95
96
97
98
99
100
# File 'lib/sunspot/session.rb', line 94

def remove(*objects)
  objects.flatten!
  @deletes += objects.length
  objects.each do |object|
    indexer.remove(object)
  end
end

#remove!(*objects) ⇒ Object

See Sunspot.remove!



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

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

#remove_all(*classes) ⇒ Object

See Sunspot.remove_all



134
135
136
137
138
139
140
141
142
143
# File 'lib/sunspot/session.rb', line 134

def remove_all(*classes)
  classes.flatten!
  if classes.empty?
    @deletes += 1
    Indexer.remove_all(master_connection)
  else
    @deletes += classes.length
    classes.each { |clazz| indexer.remove_all(clazz) }
  end
end

#remove_all!(*classes) ⇒ Object

See Sunspot.remove_all!



148
149
150
151
# File 'lib/sunspot/session.rb', line 148

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

#remove_by_id(clazz, id) ⇒ Object

See Sunspot.remove_by_id



113
114
115
116
117
118
119
120
121
# File 'lib/sunspot/session.rb', line 113

def remove_by_id(clazz, id)
  class_name =
    if clazz.is_a?(Class)
      clazz.name
    else
      clazz.to_s
    end
  indexer.remove_by_id(class_name, id)
end

#remove_by_id!(clazz, id) ⇒ Object

See Sunspot.remove_by_id!



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

def remove_by_id!(clazz, id)
  remove_by_id(clazz, id)
  commit
end

#search(*types, &block) ⇒ Object

See Sunspot.search



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

def search(*types, &block)
  search = new_search(*types)
  search.build(&block) if block
  search.execute!
end