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:



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sunspot/rails/searchable.rb', line 111

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)


341
342
343
# File 'lib/sunspot/rails/searchable.rb', line 341

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.



325
326
327
328
329
330
331
# File 'lib/sunspot/rails/searchable.rb', line 325

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



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/sunspot/rails/searchable.rb', line 345

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

#solr_execute_search_ids(options = {}) ⇒ Object



361
362
363
364
# File 'lib/sunspot/rails/searchable.rb', line 361

def solr_execute_search_ids(options = {})
  search = yield
  search.raw_results.map { |raw_result| raw_result.primary_key.to_i }
end

#solr_index(opts = {}) ⇒ Object

Add/update all existing records in the Solr index. 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

first_id

The lowest possible ID for this class. Defaults to 0, which is fine for integer IDs; string primary keys will need to specify something reasonable here.

Examples

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

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

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

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


233
234
235
236
237
238
239
# File 'lib/sunspot/rails/searchable.rb', line 233

def solr_index(opts={})
  if self.sunspot_options[:paginate]
    solr_index_paged(opts)
  else
    solr_index_batched(opts)
  end
end

#solr_index_batched(opts = {}) ⇒ Object

The default method of indexing records into the Solr index. Performs batching using the ID of the last retrieved records and a limit. Ideal for use with database/activerecord etc.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/sunspot/rails/searchable.rb', line 246

def solr_index_batched(opts={})
  options = { :batch_size => 500, :batch_commit => true, :include => self.sunspot_options[:include], :first_id => 0}.merge(opts)
  unless options[:batch_size]
    Sunspot.index!(all(:include => options[:include]))
  else
    offset = 0
    counter = 1
    record_count = count
    last_id = options[:first_id]
    while(offset < record_count)
      solr_benchmark options[:batch_size], counter do
        records = find(:all, :include => options[:include], :conditions => ["#{table_name}.#{primary_key} > ?", last_id], :limit => options[:batch_size], :order => primary_key)
        Sunspot.index(records)
        last_id = records.last.id
      end
      Sunspot.commit if options[:batch_commit]
      offset += options[:batch_size]
      counter += 1
    end
    Sunspot.commit unless options[:batch_commit]
  end
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



310
311
312
313
314
315
316
317
# File 'lib/sunspot/rails/searchable.rb', line 310

def solr_index_orphans
  count = self.count
  indexed_ids = solr_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

#solr_index_paged(opts = {}) ⇒ Object

Index records into Solr using pagintated pattern. Ideal for when you won’t know the total number of records until after the first batch is retreived. Use with activeresource/pulling records from a webservice etc.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/sunspot/rails/searchable.rb', line 275

def solr_index_paged(opts={})
  options = { :batch_size => 500, :batch_commit => true, :include => self.sunspot_options[:include], :first_id => 0}.merge(opts)
  options[:batch_size] = self.sunspot_options[:max_batch_size] if options[:batch_size] > self.sunspot_options[:max_batch_size]

  unless options[:batch_size]
    Sunspot.index!(all(:include => options[:include]))
  else
    page = 0
    per_page = options[:batch_size]
    total_entries = options[:batch_size]
    while ((page * per_page) < total_entries)
      records = find(:all,
                     :include => options[:include],
                     :params => { :page => page + 1,
                                  :per_page => per_page })
      Sunspot.index(records)
      Sunspot.commit if options[:batch_commit]

      total_entries = records.total_entries
      page += 1
    end
    Sunspot.commit unless options[:batch_commit]
  end
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.



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

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.



171
172
173
# File 'lib/sunspot/rails/searchable.rb', line 171

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.



180
181
182
# File 'lib/sunspot/rails/searchable.rb', line 180

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(:include => [:blog]) do
  keywords 'best pizza'
  with :blog_id, 1
  order :updated_at, :desc
  facet :category_ids
end

Options

:include

Specify associations to eager load

:select

Specify columns to select from database when loading results

Returns

Sunspot::Search

Object containing results, totals, facets, etc.



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

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



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

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