Class: Sunspot::SessionProxy::ShardingSessionProxy

Inherits:
AbstractSessionProxy show all
Defined in:
lib/sunspot/session_proxy/sharding_session_proxy.rb

Overview

This is a generic abstract implementation of a session proxy that allows Sunspot to be used with a distributed (sharded) Solr deployment. Concrete subclasses should implement the #session_for method, which takes a searchable object and returns a Session that points to the appropriate Solr shard for that object. Subclasses should also implement the #all_sessions object, which returns the collection of all sharded Session objects.

The class is initialized with a session that points to the Solr instance used to perform searches. Searches will have the :shards param injected, containing references to the Solr instances returned by #all_sessions.

For more on distributed search, see: wiki.apache.org/solr/DistributedSearch

The following methods are not supported (although subclasses may in some cases be able to support them):

  • batch

  • config

  • remove_by_id

  • remove_by_id!

  • remove_all with an argument

  • remove_all! with an argument

Instance Method Summary collapse

Methods inherited from AbstractSessionProxy

delegate, not_supported

Constructor Details

#initialize(search_session = Sunspot.session.new) ⇒ ShardingSessionProxy

search_session is the session that should be used for searching.



37
38
39
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 37

def initialize(search_session = Sunspot.session.new)
  @search_session = search_session
end

Instance Method Details

#all_sessionsObject

Return all shard sessions.

<strong>Concrete subclasses must implement this method.</strong>



55
56
57
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 55

def all_sessions
  raise NotImplementedError
end

#commitObject

Commit all shards. See Sunspot.commit



118
119
120
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 118

def commit
  all_sessions.each { |session| session.commit }
end

#commit_if_delete_dirtyObject

Commit all delete-dirty sessions. Only delete-dirty sessions will be committed.

See Sunspot.commit_if_delete_dirty



137
138
139
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 137

def commit_if_delete_dirty
  all_sessions.each { |session| session.commit_if_delete_dirty }
end

#commit_if_dirtyObject

Commit all dirty sessions. Only dirty sessions will be committed.

See Sunspot.commit_if_dirty



127
128
129
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 127

def commit_if_dirty
  all_sessions.each { |session| session.commit_if_dirty }
end

#delete_dirty?Boolean

True if any shard session is delete-dirty. Note that directly using the #commit_if_delete_dirty method is more efficient if that’s what you’re trying to do, since in that case only the delete-dirty sessions are committed.

Returns:

  • (Boolean)


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

def delete_dirty?
  all_sessions.any? { |session| session.delete_dirty? }
end

#dirty?Boolean

True if any shard session is dirty. Note that directly using the #commit_if_dirty method is more efficient if that’s what you’re trying to do, since in that case only the dirty sessions are committed.

See Sunspot.dirty?

Returns:

  • (Boolean)


186
187
188
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 186

def dirty?
  all_sessions.any? { |session| session.dirty? }
end

#index(*objects) ⇒ Object

See Sunspot.index



62
63
64
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 62

def index(*objects)
  using_sharded_session(objects) { |session, group| session.index(group) }
end

#index!(*objects) ⇒ Object

See Sunspot.index!



69
70
71
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 69

def index!(*objects)
  using_sharded_session(objects) { |session, group| session.index!(group) }
end

#more_like_this(object, &block) ⇒ Object



170
171
172
173
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 170

def more_like_this(object, &block)
  #FIXME should use shards
  new_more_like_this(object, &block).execute
end

#new_more_like_this(object, &block) ⇒ Object



175
176
177
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 175

def new_more_like_this(object, &block)
  @search_session.new_more_like_this(object, &block)
end

#new_search(*types) ⇒ Object

Instantiate a new Search object, but don’t execute it. The search will have an extra :shards param injected into the query, which will tell the Solr instance referenced by the search session to search across all shards.

See Sunspot.new_search



149
150
151
152
153
154
155
156
157
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 149

def new_search(*types)
  shard_urls = all_sessions.map { |session| session.config.solr.url }
  search = @search_session.new_search(*types)
  search.build do
    adjust_solr_params { |params| params[:shards] = shard_urls.join(',') }
    # I feel a little dirty doing this.
  end
  search
end

#remove(*objects) ⇒ Object

See Sunspot.remove



76
77
78
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 76

def remove(*objects)
  using_sharded_session(objects) { |session, group| session.remove(group) }
end

#remove!(*objects) ⇒ Object

See Sunspot.remove!



83
84
85
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 83

def remove!(*objects)
  using_sharded_session(objects) { |session, group| session.remove!(group) }
end

#remove_all(clazz = nil) ⇒ Object

If no argument is passed, behaves like Sunspot.remove_all

If an argument is passed, will raise NotSupportedError, as the proxy does not know which session(s) to which to delegate this operation.



93
94
95
96
97
98
99
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 93

def remove_all(clazz = nil)
  if clazz
    raise NotSupportedError, "Sharding session proxy does not support remove_all with an argument."
  else
    all_sessions.each { |session| session.remove_all }
  end
end

#remove_all!(clazz = nil) ⇒ Object

If no argument is passed, behaves like Sunspot.remove_all!

If an argument is passed, will raise NotSupportedError, as the proxy does not know which session(s) to which to delegate this operation.



107
108
109
110
111
112
113
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 107

def remove_all!(clazz = nil)
  if clazz
    raise NotSupportedError, "Sharding session proxy does not support remove_all! with an argument."
  else
    all_sessions.each { |session| session.remove_all! }
  end
end

#search(*types, &block) ⇒ Object

Build and execute a new Search. The search will have an extra :shards param injected into the query, which will tell the Solr instance referenced by the search session to search across all shards.

See Sunspot.search



166
167
168
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 166

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

#session_for(object) ⇒ Object

Return the appropriate shard session for the object.

<strong>Concrete subclasses must implement this method.</strong>



46
47
48
# File 'lib/sunspot/session_proxy/sharding_session_proxy.rb', line 46

def session_for(object)
  raise NotImplementedError
end