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



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

def commit
  @updates = 0
  connection.commit
end

#commit_if_dirtyObject

See Sunspot.commit_if_dirty



122
123
124
# File 'lib/sunspot/session.rb', line 122

def commit_if_dirty
  commit if dirty?
end

#dirty?Boolean

See Sunspot.dirty?

Returns:

  • (Boolean)


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

def dirty?
  @updates > 0
end

#index(*objects) ⇒ Object

See Sunspot.index



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

def index(*objects)
  objects.flatten!
  @updates += objects.length
  indexer_for(objects.first).add(objects)
end

#index!(*objects) ⇒ Object

See Sunspot.index!



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

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



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

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!



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

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

#remove_all(*classes) ⇒ Object

See Sunspot.remove_all



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sunspot/session.rb', line 91

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!



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

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