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.
225 226 227 228 229 230 231 |
# File 'lib/sunspot/rails/searchable.rb', line 225 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
210 211 212 213 214 215 216 217 |
# File 'lib/sunspot/rails/searchable.rb', line 210 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)
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={}) = { :batch_size => 500, :batch_commit => true, :include => []}.merge(opts) remove_all_from_index unless [:batch_size] Sunspot.index!(all(:include => [:include])) else record_count = count counter = 1 offset = 0 while(offset < record_count) benchmark [:batch_size], counter do Sunspot.index(all(:include => [:include], :offset => offset, :limit => [:batch_size], :order => primary_key)) 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.
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
241 242 243 |
# File 'lib/sunspot/rails/searchable.rb', line 241 def searchable? true end |