Module: ActsAsFerret::InstanceMethods

Includes:
ResultAttributes
Defined in:
lib/acts_as_ferret/instance_methods.rb

Instance Attribute Summary

Attributes included from ResultAttributes

#ferret_rank, #ferret_score

Instance Method Summary collapse

Instance Method Details

#content_for_field_name(field, via = field, dynamic_boost = nil) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/acts_as_ferret/instance_methods.rb', line 158

def content_for_field_name(field, via = field, dynamic_boost = nil)
  field_data = (respond_to?(via) ? send(via) : instance_variable_get("@#{via}"))
  field_data = (field_data.is_a?(Array) ? field_data.map{|d| d.to_s} : field_data.to_s)
  # field_data = self.send(via) || self.instance_variable_get("@#{via}")
  if (dynamic_boost && boost_value = self.send(dynamic_boost))
    field_data = Ferret::Field.new(field_data)
    field_data.boost = boost_value.to_i
  end
  field_data
end

#disable_ferret(option = :once) ⇒ Object

Disable Ferret for this record for a specified amount of time. ::once will disable Ferret for the next call to #save (this is the default), ::always will do so for all subsequent calls.

Note that this will turn off only the create and update hooks, but not the destroy hook. I think that’s reasonable, if you think the opposite, please tell me.

To manually trigger reindexing of a record after you’re finished modifying it, you can call #ferret_update directly instead of #save (remember to enable ferret again before).

When given a block, this will be executed without any ferret indexing of this object taking place. The optional argument in this case can be used to indicate if the object should be indexed after executing the block (::index_when_finished). Automatic Ferret indexing of this object will be turned on after the block has been executed. If passed ::index_when_true, the index will only be updated if the block evaluated not to false or nil.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/acts_as_ferret/instance_methods.rb', line 82

def disable_ferret(option = :once)
  if block_given?
    @ferret_disabled = :always
    result = yield
    ferret_enable
    ferret_update if option == :index_when_finished || (option == :index_when_true && result)
    result
  elsif [:once, :always].include?(option)
    @ferret_disabled = option
  else
    raise ArgumentError.new("Invalid Argument #{option}")
  end
end

#document_numberObject



150
151
152
# File 'lib/acts_as_ferret/instance_methods.rb', line 150

def document_number
  self.class.aaf_index.document_number(self.ferret_key)
end

#enable_ferretObject Also known as: ferret_enable

re-eneable ferret indexing for this instance after a call to #disable_ferret



35
36
37
# File 'lib/acts_as_ferret/instance_methods.rb', line 35

def enable_ferret  
  @ferret_disabled = nil 
end

#ferret_analyzerObject

Returns the analyzer to use when adding this record to the index.

Override to return a specific analyzer for any record that is to be indexed, i.e. specify a different analyzer based on language. Returns nil by default so the global analyzer (specified with the acts_as_ferret call) is used.



59
60
61
# File 'lib/acts_as_ferret/instance_methods.rb', line 59

def ferret_analyzer
  nil
end

#ferret_createObject Also known as: ferret_update

add to index



97
98
99
100
101
102
103
104
105
# File 'lib/acts_as_ferret/instance_methods.rb', line 97

def ferret_create
  if ferret_enabled?
    logger.debug "ferret_create/update: #{self.ferret_key}"
    self.class.aaf_index << self
  else
    ferret_enable if @ferret_disabled == :once
  end
  true # signal success to AR
end

#ferret_destroyObject

remove from index



110
111
112
113
114
115
116
117
118
# File 'lib/acts_as_ferret/instance_methods.rb', line 110

def ferret_destroy
  logger.debug "ferret_destroy: #{self.ferret_key}"
  begin
    self.class.aaf_index.remove self.ferret_key
  rescue
    logger.warn("Could not find indexed value for this object: #{$!}\n#{$!.backtrace}")
  end
  true # signal success to AR
end

#ferret_enabled?(is_bulk_index = false) ⇒ Boolean

returns true if ferret indexing is enabled for this record.

The optional is_bulk_index parameter will be true if the method is called by rebuild_index or bulk_index, and false otherwise.

If is_bulk_index is true, the class level ferret_enabled state will be ignored by this method (per-instance ferret_enabled checks however will take place, so if you override this method to forbid indexing of certain records you’re still safe).

Returns:

  • (Boolean)


49
50
51
# File 'lib/acts_as_ferret/instance_methods.rb', line 49

def ferret_enabled?(is_bulk_index = false)
  @ferret_disabled.nil? && (is_bulk_index || self.class.ferret_enabled?) && (aaf_configuration[:if].nil? || aaf_configuration[:if].call(self))
end

#ferret_keyObject



120
121
122
# File 'lib/acts_as_ferret/instance_methods.rb', line 120

def ferret_key
  "#{self.class.name}-#{self.send self.class.primary_key}" unless new_record?
end

#highlight(query, options = {}) ⇒ Object

Returns an array of strings with the matches highlighted. The query can either be a String or a Ferret::Search::Query object.

Options

field

field to take the content from. This field has to have it’s content stored in the index (:store => :yes in your call to aaf). If not given, all stored fields are searched, and the highlighted content found in all of them is returned. set :highlight => :no in the field options to avoid highlighting of contents from a :stored field.

excerpt_length

Default: 150. Length of excerpt to show. Highlighted terms will be in the centre of the excerpt.

num_excerpts

Default: 2. Number of excerpts to return.

pre_tag

Default: “<em>”. Tag to place to the left of the match.

post_tag

Default: “</em>”. This tag should close the :pre_tag.

ellipsis

Default: “…”. This is the string that is appended at the beginning and end of excerpts (unless the excerpt hits the start or end of the field. You’ll probably want to change this to a Unicode elipsis character.



30
31
32
# File 'lib/acts_as_ferret/instance_methods.rb', line 30

def highlight(query, options = {})
  self.class.aaf_index.highlight(self.ferret_key, query, options)
end

#query_for_recordObject



154
155
156
# File 'lib/acts_as_ferret/instance_methods.rb', line 154

def query_for_record
  self.class.aaf_index.query_for_record(self.ferret_key)
end

#to_docObject

turn this instance into a ferret document (which basically is a hash of fieldname => value pairs)



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/acts_as_ferret/instance_methods.rb', line 126

def to_doc
  logger.debug "creating doc for class: #{self.ferret_key}"
  Ferret::Document.new.tap do |doc|
    # store the id and class name of each item, and the unique key used for identifying the record
    # even in multi-class indexes.
    doc[:key] = self.ferret_key
    doc[:id] = self.id.to_s
    doc[:class_name] = self.class.name
  
    # iterate through the fields and add them to the document
    aaf_configuration[:defined_fields].each_pair do |field, config|
      doc[field] = self.send("#{field}_to_ferret") unless config[:ignore]
    end
    if aaf_configuration[:boost]
      if self.respond_to?(aaf_configuration[:boost])
        boost = self.send aaf_configuration[:boost]
        doc.boost = boost.to_i if boost
      else
        logger.error "boost option should point to an instance method: #{aaf_configuration[:boost]}"
      end
    end
  end
end