Module: Sunspot::Rails::Searchable::ClassMethods

Defined in:
lib/sunspot/rails/searchable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sunspot/rails/searchable.rb', line 95

def self.extended(base) #:nodoc:
  class <<base
    alias_method :search, :solr_search unless method_defined? :search
    alias_method :search_ids, :solr_search_ids unless method_defined? :search_ids
    alias_method :remove_all_from_index, :solr_remove_all_from_index unless method_defined? :remove_all_from_index
    alias_method :remove_all_from_index!, :solr_remove_all_from_index! unless method_defined? :remove_all_from_index!
    alias_method :reindex, :solr_reindex unless method_defined? :reindex
    alias_method :index, :solr_index unless method_defined? :index
    alias_method :index_orphans, :solr_index_orphans unless method_defined? :index_orphans
    alias_method :clean_index_orphans, :solr_clean_index_orphans unless method_defined? :clean_index_orphans
  end
end

Instance Method Details

#searchable?Boolean

Classes that have been defined as searchable return true for this method.

Returns

true

Returns:

  • (Boolean)


237
238
239
# File 'lib/sunspot/rails/searchable.rb', line 237

def searchable?
  true
end

#solr_clean_index_orphansObject

Find IDs of records of this class that are indexed in Solr but do not exist in the database, and remove them from Solr. Under normal circumstances, this should not be necessary; this method is provided in case something goes wrong.



221
222
223
224
225
226
227
# File 'lib/sunspot/rails/searchable.rb', line 221

def solr_clean_index_orphans
  solr_index_orphans.each do |id|
    new do |fake_instance|
      fake_instance.id = id
    end.solr_remove_from_index
  end
end

#solr_execute_search(options = {}) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/sunspot/rails/searchable.rb', line 241

def solr_execute_search(options = {})
  options.assert_valid_keys(:select)
  search = yield
  unless options.empty?
    search.build do |query|
      if options[:select]
        query.data_accessor_for(self).select = options[:select]
      end
    end
  end
  search.execute
end

#solr_execute_search_ids(options = {}) ⇒ Object



254
255
256
257
# File 'lib/sunspot/rails/searchable.rb', line 254

def solr_execute_search_ids(options = {})
  search = yield
  search.raw_results.map { |raw_result| BSON::ObjectId.from_string(raw_result.primary_key) }
end

#solr_index(opts = {}) ⇒ Object

Add/update all existing records in the Solr index. Mongoid does not support batch updates or include options.

Options (passed as a hash)

Options values will be ignored, leaving the options
argument for compatibility

Examples

# index and commit all
# Mongoid does not support batches
Post.index


192
193
194
# File 'lib/sunspot/rails/searchable.rb', line 192

def solr_index(opts={})
  Sunspot.index!(all)
end

#solr_index_orphansObject

Return the IDs of records of this class that are indexed in Solr but do not exist in the database. Under normal circumstances, this should never happen, but this method is provided in case something goes wrong. Usually you will want to rectify the situation by calling #clean_index_orphans or #reindex

Returns

Array

Collection of IDs that exist in Solr but not in the database



206
207
208
209
210
211
212
213
# File 'lib/sunspot/rails/searchable.rb', line 206

def solr_index_orphans
  count = self.count
  indexed_ids = solr_search_ids { paginate(:page => 1, :per_page => count) }.to_set
  only(:id).each do |object|
    indexed_ids.delete(object.id)
  end
  indexed_ids.to_a
end

#solr_reindex(options = {}) ⇒ Object

Completely rebuild the index for this class. First removes all instances from the index, then loads records and indexes them.

See #index for information on options, etc.



173
174
175
176
# File 'lib/sunspot/rails/searchable.rb', line 173

def solr_reindex(options = {})
  solr_remove_all_from_index
  solr_index(options)
end

#solr_remove_all_from_indexObject

Remove instances of this class from the Solr index.



154
155
156
# File 'lib/sunspot/rails/searchable.rb', line 154

def solr_remove_all_from_index
  Sunspot.remove_all(self)
end

#solr_remove_all_from_index!Object

Remove all instances of this class from the Solr index and immediately commit.



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

def solr_remove_all_from_index!
  Sunspot.remove_all!(self)
end

#solr_search(options = {}, &block) ⇒ Object

Search for instances of this class in Solr. The block is delegated to the Sunspot.search method - see the Sunspot documentation for the full API.

Example

Post.search() do
  keywords 'best pizza'
  with :blog_id, 1
  order :updated_at, :desc
  facet :category_ids
end

Options

:select

Specify columns to select from database when loading results

Returns

Sunspot::Search

Object containing results, totals, facets, etc.



129
130
131
132
133
# File 'lib/sunspot/rails/searchable.rb', line 129

def solr_search(options = {}, &block)
  solr_execute_search(options) do
    Sunspot.new_search(self, &block)
  end
end

#solr_search_ids(&block) ⇒ Object

Get IDs of matching results without loading the result objects from the database. This method may be useful if search is used as an intermediate step in a larger find operation. The block is the same as the block provided to the #search method.

Returns

Array

Array of IDs, in the order returned by the search



145
146
147
148
149
# File 'lib/sunspot/rails/searchable.rb', line 145

def solr_search_ids(&block)
  solr_execute_search_ids do
    solr_search(&block)
  end
end