| Module | ActsAsFerret::InstanceMethods |
| In: |
lib/instance_methods.rb
|
# File lib/instance_methods.rb, line 152
152: def content_for_field_name(field, via = field, dynamic_boost = nil)
153: field_data = self.send(via) || self.instance_variable_get("@#{via}")
154: if (dynamic_boost && boost_value = self.send(dynamic_boost))
155: field_data = Ferret::Field.new(field_data)
156: field_data.boost = boost_value.to_i
157: end
158: field_data
159: end
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.
# File lib/instance_methods.rb, line 82
82: def disable_ferret(option = :once)
83: if block_given?
84: @ferret_disabled = :always
85: result = yield
86: ferret_enable
87: ferret_update if option == :index_when_finished || (option == :index_when_true && result)
88: result
89: elsif [:once, :always].include?(option)
90: @ferret_disabled = option
91: else
92: raise ArgumentError.new("Invalid Argument #{option}")
93: end
94: end
# File lib/instance_methods.rb, line 144
144: def document_number
145: self.class.aaf_index.document_number(id, self.class.name)
146: end
re-eneable ferret indexing for this instance after a call to disable_ferret
# File lib/instance_methods.rb, line 35
35: def enable_ferret
36: @ferret_disabled = nil
37: end
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.
# File lib/instance_methods.rb, line 59
59: def ferret_analyzer
60: nil
61: end
add to index
# File lib/instance_methods.rb, line 97
97: def ferret_create
98: if ferret_enabled?
99: logger.debug "ferret_create/update: #{self.class.name} : #{self.id}"
100: self.class.aaf_index << self
101: else
102: ferret_enable if @ferret_disabled == :once
103: end
104: true # signal success to AR
105: end
remove from index
# File lib/instance_methods.rb, line 110
110: def ferret_destroy
111: logger.debug "ferret_destroy: #{self.class.name} : #{self.id}"
112: begin
113: self.class.aaf_index.remove self.id, self.class.name
114: rescue
115: logger.warn("Could not find indexed value for this object: #{$!}\n#{$!.backtrace}")
116: end
117: true # signal success to AR
118: end
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).
# File lib/instance_methods.rb, line 49
49: def ferret_enabled?(is_bulk_index = false)
50: @ferret_disabled.nil? && (is_bulk_index || self.class.ferret_enabled?) && (aaf_configuration[:if].nil? || aaf_configuration[:if].call(self))
51: end
Returns an array of strings with the matches highlighted. The query can either be a String or a Ferret::Search::Query object.
| 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. |
# File lib/instance_methods.rb, line 30
30: def highlight(query, options = {})
31: self.class.aaf_index.highlight(self.send(self.class.primary_key), self.class.name, query, options)
32: end
# File lib/instance_methods.rb, line 148
148: def query_for_record
149: self.class.aaf_index.query_for_record(id, self.class.name)
150: end
turn this instance into a ferret document (which basically is a hash of fieldname => value pairs)
# File lib/instance_methods.rb, line 122
122: def to_doc
123: logger.debug "creating doc for class: #{self.class.name}, id: #{self.id}"
124: returning Ferret::Document.new do |doc|
125: # store the id and class name of each item
126: doc[:id] = self.id
127: doc[:class_name] = self.class.name
128:
129: # iterate through the fields and add them to the document
130: aaf_configuration[:defined_fields].each_pair do |field, config|
131: doc[field] = self.send("#{field}_to_ferret") unless config[:ignore]
132: end
133: if aaf_configuration[:boost]
134: if self.respond_to?(aaf_configuration[:boost])
135: boost = self.send aaf_configuration[:boost]
136: doc.boost = boost.to_i if boost
137: else
138: logger.error "boost option should point to an instance method: #{aaf_configuration[:boost]}"
139: end
140: end
141: end
142: end