Module: ActsAsFerret::ActMethods

Defined in:
lib/acts_as_ferret/act_methods.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#acts_as_ferret(options = {}) ⇒ Object

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.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/acts_as_ferret/act_methods.rb', line 60

def acts_as_ferret(options={})

  extend ClassMethods

  include InstanceMethods
  include MoreLikeThis::InstanceMethods

  if options[:rdig]
    cattr_accessor :rdig_configuration
    self.rdig_configuration = options[:rdig]
    require 'rdig_adapter'
    include ActsAsFerret::RdigAdapter
  end

  unless included_modules.include?(ActsAsFerret::WithoutAR)
    # set up AR hooks
    after_create  :ferret_create
    after_update  :ferret_update
    after_destroy :ferret_destroy
  end

  cattr_accessor :aaf_configuration

  # apply default config for rdig based models
  if options[:rdig]
    options[:fields] ||= { :title   => { :boost => 3, :store => :yes },
                           :content => { :store => :yes } }
  end

  # name of this index
  index_name = options.delete(:index) || self.name.underscore

  index = ActsAsFerret::register_class_with_index(self, index_name, options)
  self.aaf_configuration = index.index_definition.dup
  # logger.debug "configured index for class #{self.name}:\n#{aaf_configuration.inspect}"

  # update our copy of the global index config with options local to this class
  aaf_configuration[:class_name] ||= self.name
  aaf_configuration[:if] ||= options[:if]

  # add methods for retrieving field values
  add_fields options[:fields]
  add_fields options[:additional_fields]
  add_fields aaf_configuration[:fields]
  add_fields aaf_configuration[:additional_fields]

end

#reloadable?Boolean

Returns:

  • (Boolean)


8
# File 'lib/acts_as_ferret/act_methods.rb', line 8

def reloadable?; false end