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

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

Instance Method Summary collapse

Instance Method Details

#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.



224
225
226
227
228
229
230
# File 'lib/sunspot/rails/searchable.rb', line 224

def clean_index_orphans
  index_orphans.each do |id|
    new do |fake_instance|
      fake_instance.id = id
    end.remove_from_index
  end
end

#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



210
211
212
213
214
215
216
# File 'lib/sunspot/rails/searchable.rb', line 210

def index_orphans
  indexed_ids = search_ids.to_set
  all(:select => 'id').each do |object|
    indexed_ids.delete(object.id)
  end
  indexed_ids.to_a
end

#reindex(opts = {}) ⇒ Object

Completely rebuild the index for this class. First removes all instances from the index, then loads records and save them. The batch_size argument specifies how many records to load out of the database at a time. The default batch size is 500; if nil is passed, records will not be indexed in batches. By default, a commit is issued after each batch; passing false for batch_commit will disable this, and only issue a commit at the end of the process. If associated objects need to indexed also, you can specify include in format accepted by ActiveRecord to improve your sql select performance

Options (passed as a hash)

batch_size<Integer>

Batch size with which to load records. Passing ‘nil’ will skip batches. Default is 500.

batch_commit<Boolean>

Flag signalling if a commit should be done after after each batch is indexed, default is ‘true’

include<Mixed>

include option to be passed to the ActiveRecord find, used for including associated objects that need to be indexed with the parent object, accepts all formats ActiveRecord::Base.find does

Examples

# reindex in batches of 500, commit after each
Post.reindex 

# index all rows at once, then commit
Post.reindex(:batch_size => nil) 

# reindex in batches of 500, commit when all batches complete
Post.reindex(:batch_commit => false) 

# include the associated +author+ object when loading to index
Post.reindex(:include => :author)


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/sunspot/rails/searchable.rb', line 179

def reindex(opts={})
  options = { :batch_size => 500, :batch_commit => true, :include => []}.merge(opts)
  remove_all_from_index
  unless options[:batch_size]
    Sunspot.index!(all(:include => options[:include]))
  else
    record_count = count
    counter = 1
    offset = 0
    while(offset < record_count)
      benchmark options[:batch_size], counter do
        Sunspot.index(all(:include => options[:include], :offset => offset, :limit => options[:batch_size], :order => primary_key))
      end
      Sunspot.commit if options[:batch_commit]
      offset += options[:batch_size]
      counter += 1
    end
    Sunspot.commit unless options[:batch_commit]
  end
end

#remove_all_from_indexObject

Remove instances of this class from the Solr index.



127
128
129
# File 'lib/sunspot/rails/searchable.rb', line 127

def remove_all_from_index
  Sunspot.remove_all(self)
end

#remove_all_from_index!Object

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


XXX Sunspot should implement remove_all!()



138
139
140
141
# File 'lib/sunspot/rails/searchable.rb', line 138

def remove_all_from_index!
  Sunspot.remove_all(self)
  Sunspot.commit
end

#search(&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

Returns

Sunspot::Search

Object containing results, totals, facets, etc.



106
107
108
# File 'lib/sunspot/rails/searchable.rb', line 106

def search(&block)
  Sunspot.search(self, &block)
end

#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



120
121
122
# File 'lib/sunspot/rails/searchable.rb', line 120

def search_ids(&block)
  search(&block).raw_results.map { |raw_result| raw_result.primary_key.to_i }
end

#searchable?Boolean

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

Returns

true

Returns:

  • (Boolean)


240
241
242
# File 'lib/sunspot/rails/searchable.rb', line 240

def searchable?
  true
end