Class: Sunspot::Session

Inherits:
Object
  • Object
show all
Defined in:
sunspot/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

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

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
# File 'sunspot/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

Class Attribute Details

+ (Object) connection_class

For testing purposes



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

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

Instance Attribute Details

- (Object) config (readonly)

Sunspot::Configuration object for this session



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

def config
  @config
end

Instance Method Details

- (Object) batch

See Sunspot.batch



224
225
226
227
228
# File 'sunspot/lib/sunspot/session.rb', line 224

def batch
  indexer.start_batch
  yield
  indexer.flush_batch
end

- (Object) commit

See Sunspot.commit



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

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

- (Object) commit_if_delete_dirty

See Sunspot.commit_if_delete_dirty



217
218
219
# File 'sunspot/lib/sunspot/session.rb', line 217

def commit_if_delete_dirty
  commit if delete_dirty?
end

- (Object) commit_if_dirty

See Sunspot.commit_if_dirty



203
204
205
# File 'sunspot/lib/sunspot/session.rb', line 203

def commit_if_dirty
  commit if dirty?
end

- (Boolean) delete_dirty?

See Sunspot.delete_dirty?

Returns:

  • (Boolean)


210
211
212
# File 'sunspot/lib/sunspot/session.rb', line 210

def delete_dirty?
  @deletes > 0
end

- (Boolean) dirty?

See Sunspot.dirty?

Returns:

  • (Boolean)


196
197
198
# File 'sunspot/lib/sunspot/session.rb', line 196

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

- (Object) index(*objects)

See Sunspot.index



88
89
90
91
92
# File 'sunspot/lib/sunspot/session.rb', line 88

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

- (Object) index!(*objects)

See Sunspot.index!



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

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

- (Object) more_like_this(object, *types, &block)

See Sunspot.more_like_this



80
81
82
83
# File 'sunspot/lib/sunspot/session.rb', line 80

def more_like_this(object, *types, &block)
  mlt = new_more_like_this(object, *types, &block)
  mlt.execute
end

- (Object) new_more_like_this(object, *types, &block)

See Sunspot.new_more_like_this



65
66
67
68
69
70
71
72
73
74
75
# File 'sunspot/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

- (Object) new_search(*types, &block)

See Sunspot.new_search



42
43
44
45
46
47
48
49
50
51
52
# File 'sunspot/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

- (Object) optimize

See Sunspot.optimize



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

def optimize
  @adds = @deletes = 0
  connection.optimize
end

- (Object) remove(*objects, &block)

See Sunspot.remove



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'sunspot/lib/sunspot/session.rb', line 121

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

- (Object) remove!(*objects)

See Sunspot.remove!



145
146
147
148
# File 'sunspot/lib/sunspot/session.rb', line 145

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

- (Object) remove_all(*classes)

See Sunspot.remove_all



174
175
176
177
178
179
180
181
182
183
# File 'sunspot/lib/sunspot/session.rb', line 174

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

- (Object) remove_all!(*classes)

See Sunspot.remove_all!



188
189
190
191
# File 'sunspot/lib/sunspot/session.rb', line 188

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

- (Object) remove_by_id(clazz, id)

See Sunspot.remove_by_id



153
154
155
156
157
158
159
160
161
# File 'sunspot/lib/sunspot/session.rb', line 153

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

- (Object) remove_by_id!(clazz, id)

See Sunspot.remove_by_id!



166
167
168
169
# File 'sunspot/lib/sunspot/session.rb', line 166

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

- (Object) search(*types, &block)

See Sunspot.search



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

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