Module: Sunspot::Rails::Searchable::ClassMethods
- Defined in:
- lib/sunspot/rails/searchable.rb
Instance Method Summary collapse
-
#clean_index_orphans ⇒ Object
Find IDs of records of this class that are indexed in Solr but do not exist in the database, and remove them from Solr.
-
#index_orphans ⇒ Object
Return the IDs of records of this class that are indexed in Solr but do not exist in the database.
-
#reindex(opts = {}) ⇒ Object
Completely rebuild the index for this class.
-
#remove_all_from_index ⇒ Object
Remove instances of this class from the Solr index.
-
#remove_all_from_index! ⇒ Object
Remove all instances of this class from the Solr index and immediately commit.
-
#search(&block) ⇒ Object
Search for instances of this class in Solr.
-
#search_ids(&block) ⇒ Object
Get IDs of matching results without loading the result objects from the database.
-
#searchable? ⇒ Boolean
Classes that have been defined as searchable return
true
for this method.
Instance Method Details
#clean_index_orphans ⇒ Object
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.
233 234 235 236 237 238 239 |
# File 'lib/sunspot/rails/searchable.rb', line 233 def clean_index_orphans index_orphans.each do |id| new do |fake_instance| fake_instance.id = id end.remove_from_index end end |
#index_orphans ⇒ Object
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
218 219 220 221 222 223 224 225 |
# File 'lib/sunspot/rails/searchable.rb', line 218 def index_orphans count = self.count indexed_ids = search_ids { paginate(:page => 1, :per_page => count) }.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)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/sunspot/rails/searchable.rb', line 184 def reindex(opts={}) = { :batch_size => 500, :batch_commit => true, :include => []}.merge(opts) remove_all_from_index unless [:batch_size] Sunspot.index!(all(:include => [:include])) else offset = 0 counter = 1 record_count = count last_id = 0 while(offset < record_count) benchmark [:batch_size], counter do records = all(:include => [:include], :conditions => ["#{table_name}.#{primary_key} > ?", last_id], :limit => [:batch_size], :order => primary_key) Sunspot.index(records) last_id = records.last.id end Sunspot.commit if [:batch_commit] offset += [:batch_size] counter += 1 end Sunspot.commit unless [:batch_commit] end end |
#remove_all_from_index ⇒ Object
Remove instances of this class from the Solr index.
132 133 134 |
# File 'lib/sunspot/rails/searchable.rb', line 132 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!()
143 144 145 146 |
# File 'lib/sunspot/rails/searchable.rb', line 143 def remove_all_from_index! remove_all_from_index 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.
111 112 113 |
# File 'lib/sunspot/rails/searchable.rb', line 111 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
125 126 127 |
# File 'lib/sunspot/rails/searchable.rb', line 125 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
249 250 251 |
# File 'lib/sunspot/rails/searchable.rb', line 249 def searchable? true end |