| Module | ActsAsSolr::ActsMethods |
| In: |
lib/acts_methods.rb
|
declares a class as solr-searchable
| fields: | This option can be used to specify only the fields you‘d like to
index. If not given, all the attributes from the class will be indexed. You
can also use this option to include methods that should be indexed as
fields
class Movie < ActiveRecord::Base
acts_as_solr :fields => [:name, :description, :current_time]
def current_time
Time.now.to_s
end
end
Each field passed can also be a hash with the value being a field type
class Electronic < ActiveRecord::Base
acts_as_solr :fields => [{:price => :range_float}]
def current_time
Time.now
end
end
The field types accepted are:
Setting the field type preserves its original type when indexed | ||||||||||||||
| additional_fields: | This option takes fields to be include in the index in addition to those
derived from the database. You can also use this option to include custom
fields derived from methods you define. This option will be ignored if the
:fields option is given. It also accepts the same field types as the option
above
class Movie < ActiveRecord::Base
acts_as_solr :additional_fields => [:current_time]
def current_time
Time.now.to_s
end
end
| ||||||||||||||
| include: | This option can be used for association indexing, which means you can
include any :has_one, :has_many, :belongs_to and :has_and_belongs_to_many
association to be indexed:
class Category < ActiveRecord::Base has_many :books acts_as_solr :include => [:books] end | ||||||||||||||
| facets: | This option can be used to specify the fields you‘d like to index as
facet fields
class Electronic < ActiveRecord::Base acts_as_solr :facets => [:category, :manufacturer] end | ||||||||||||||
| background: | This option can be used to have a delayed indexing by specifying the indexing time. Requires rails_cron |
# File lib/acts_methods.rb, line 75 def acts_as_solr(options={}, solr_options={}) extend ClassMethods include InstanceMethods include CommonMethods after_save :solr_save after_destroy :solr_destroy cattr_accessor :configuration cattr_accessor :solr_configuration self.configuration = { :fields => nil, :additional_fields => nil, :background => false, :include => nil, :facets => nil } self.solr_configuration = { :type_field => "type_t", :primary_key_field => "pk_i" } configuration.update(options) if options.is_a?(Hash) solr_configuration.update(solr_options) if solr_options.is_a?(Hash) configuration[:solr_fields] = [] if configuration[:background] include BackgroundMethods after_save :solr_save_delayed end if configuration[:fields].respond_to?(:each) process_fields(configuration[:fields]) else process_fields(self.new.attributes.keys.map { |k| k.to_sym }) process_fields(configuration[:additional_fields]) end end