Module ActsAsFerret::ActMethods
In: lib/act_methods.rb

This module defines the acts_as_ferret method and is included into ActiveRecord::Base

Methods

Included Modules

InstanceMethods MoreLikeThis::InstanceMethods ActsAsFerret::RdigAdapter

Public Instance methods

declares a class as ferret-searchable.

options:

fields:names all fields to include in the index. If not given, all attributes of the class will be indexed. You may also give symbols pointing to instance methods of your model here, i.e. to retrieve and index data from a related model.
additional_fields:names fields to include in the index, in addition to those derived from the db scheme. use if you want to add custom fields derived from methods to the db fields (which will be picked by aaf). This option will be ignored when the fields option is given, in that case additional fields get specified there.
if:Can be set to a block that will be called with the record in question to determine if it should be indexed or not.
index_dir:declares the directory where to put the index for this class. The default is RAILS_ROOT/index/RAILS_ENV/CLASSNAME. The index directory will be created if it doesn‘t exist.
reindex_batch_size:reindexing is done in batches of this size, default is 1000
mysql_fast_batches:set this to false to disable the faster mysql batching algorithm if this model uses a non-integer primary key named ‘id’ on MySQL.
ferret:Hash of Options that directly influence the way the Ferret engine works. You can use most of the options the Ferret::I class accepts here, too. Among the more useful are:
    or_default:: whether query terms are required by
                 default (the default, false), or not (true)

    analyzer:: the analyzer to use for query parsing (default: nil,
               which means the ferret StandardAnalyzer gets used)

    default_field:: use to set one or more fields that are searched for query terms
                    that don't have an explicit field list. This list should *not*
                    contain any untokenized fields. If it does, you're asking
                    for trouble (i.e. not getting results for queries having
                    stop words in them). Aaf by default initializes the default field
                    list to contain all tokenized fields. If you use :single_index => true,
                    you really should set this option specifying your default field
                    list (which should be equal in all your classes sharing the index).
                    Otherwise you might get incorrect search results and you won't get
                    any lazy loading of stored field data.

For downwards compatibility reasons you can also specify the Ferret options in the last Hash argument.

[Source]

     # File lib/act_methods.rb, line 60
 60:     def acts_as_ferret(options={})
 61: 
 62:       extend ClassMethods
 63: 
 64:       include InstanceMethods
 65:       include MoreLikeThis::InstanceMethods
 66: 
 67:       if options[:rdig]
 68:         cattr_accessor :rdig_configuration
 69:         self.rdig_configuration = options[:rdig]
 70:         require 'rdig_adapter'
 71:         include ActsAsFerret::RdigAdapter
 72:       end
 73: 
 74:       unless included_modules.include?(ActsAsFerret::WithoutAR)
 75:         # set up AR hooks
 76:         after_create  :ferret_create
 77:         after_update  :ferret_update
 78:         after_destroy :ferret_destroy      
 79:       end
 80: 
 81:       cattr_accessor :aaf_configuration
 82: 
 83:       # apply default config for rdig based models
 84:       if options[:rdig]
 85:         options[:fields] ||= { :title   => { :boost => 3, :store => :yes },
 86:                                :content => { :store => :yes } }
 87:       end
 88: 
 89:       # name of this index
 90:       index_name = options.delete(:index) || self.name.underscore
 91: 
 92:       index = ActsAsFerret::register_class_with_index(self, index_name, options)
 93:       self.aaf_configuration = index.index_definition.dup
 94:       # logger.debug "configured index for class #{self.name}:\n#{aaf_configuration.inspect}"
 95: 
 96:       # update our copy of the global index config with options local to this class
 97:       aaf_configuration[:class_name] ||= self.name
 98:       aaf_configuration[:if] ||= options[:if]
 99: 
100:       # add methods for retrieving field values
101:       add_fields options[:fields]
102:       add_fields options[:additional_fields]
103:       add_fields aaf_configuration[:fields]
104:       add_fields aaf_configuration[:additional_fields]
105: 
106:     end

[Source]

   # File lib/act_methods.rb, line 8
8:     def reloadable?; false end

Protected Instance methods

[Source]

     # File lib/act_methods.rb, line 133
133:     def add_fields(field_config)
134:       if field_config.is_a? Hash
135:         field_config.each_pair do |field, options|
136:           define_to_field_method field, options
137:         end
138:       elsif field_config.respond_to?(:each)
139:         field_config.each do |field| 
140:           define_to_field_method field
141:         end                
142:       end
143:     end

helper to defines a method which adds the given field to a ferret document instance

[Source]

     # File lib/act_methods.rb, line 114
114:     def define_to_field_method(field, options = {})
115:       method_name = "#{field}_to_ferret"
116:       return if instance_methods.include?(method_name) # already defined
117:       aaf_configuration[:defined_fields] ||= {}
118:       aaf_configuration[:defined_fields][field] = options
119:       dynamic_boost = options[:boost] if options[:boost].is_a?(Symbol)
120:       via = options[:via] || field
121:       define_method(method_name.to_sym) do
122:         val = begin
123:           content_for_field_name(field, via, dynamic_boost)
124:         rescue
125:           logger.warn("Error retrieving value for field #{field}: #{$!}")
126:           ''
127:         end
128:         logger.debug("Adding field #{field} with value '#{val}' to index")
129:         val
130:       end
131:     end

[Validate]