Changeset 244
- Timestamp:
- 09/17/07 09:15:10 (1 year ago)
- Files:
-
- trunk/demo/app/models/content_base.rb (modified) (1 diff)
- trunk/demo/test/unit/content_test.rb (modified) (3 diffs)
- trunk/plugin/acts_as_ferret/lib/act_methods.rb (modified) (2 diffs)
- trunk/plugin/acts_as_ferret/lib/acts_as_ferret.rb (modified) (1 diff)
- trunk/plugin/acts_as_ferret/lib/instance_methods.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/demo/app/models/content_base.rb
r142 r244 12 12 # descriptions 13 13 acts_as_ferret( :fields => { :comment_count => {}, 14 :title => { :boost => 2},14 :title => { :boost => :title_boost }, 15 15 :description => { :boost => 1, :store => :yes }, 16 16 :special => {} }, 17 17 :store_class_name => true, 18 :boost => :record_boost, 18 19 :remote => ENV['AAF_REMOTE'] ) 19 20 20 21 def comment_count; 0 end 21 22 23 attr_accessor :record_boost 24 attr_writer :title_boost 25 def title_boost 26 @title_boost || 2 27 end 22 28 end 23 29 trunk/demo/test/unit/content_test.rb
r238 r244 556 556 557 557 # give description field higher boost: 558 total_hits, contents_from_ferret = Content.find_id_by_contents('title:title OR description:title^ 10')558 total_hits, contents_from_ferret = Content.find_id_by_contents('title:title OR description:title^200') 559 559 assert_equal 2, contents_from_ferret.size 560 560 assert_equal 2, total_hits … … 569 569 def test_find_by_contents_boost 570 570 # give description field higher boost: 571 contents_from_ferret = Content.find_by_contents('title:title OR description:title^ 10')571 contents_from_ferret = Content.find_by_contents('title:title OR description:title^200') 572 572 assert_equal 2, contents_from_ferret.size 573 573 assert_equal @another_content.id, contents_from_ferret.first.id … … 736 736 end 737 737 738 def test_per_field_boost 739 Content.destroy_all 740 Content.create! :title => 'the title' 741 boosted = Content.new :title => 'the title' 742 boosted.title_boost = 100 743 boosted.save! 744 Content.create! :title => 'the title' 745 results = Content.find_with_ferret 'title:title' 746 assert_equal 3, results.size 747 assert_equal boosted.id, results.first.id 748 end 749 750 def test_per_document_boost 751 Content.destroy_all 752 Content.create! :title => 'the title' 753 boosted = Content.new :title => 'the title' 754 boosted.record_boost = 10 755 boosted.save! 756 Content.create! :title => 'the title' 757 results = Content.find_with_ferret 'title' 758 assert_equal 3, results.size 759 assert_equal boosted.id, results.first.id 760 end 761 762 738 763 protected 739 764 def more_contents trunk/plugin/acts_as_ferret/lib/act_methods.rb
r234 r244 212 212 # document instance 213 213 def define_to_field_method(field, options = {}) 214 if options[:boost].is_a?(Symbol) 215 dynamic_boost = options[:boost] 216 options.delete :boost 217 end 214 218 options.reverse_merge!( :store => :no, 215 219 :highlight => :yes, … … 219 223 options[:term_vector] = :no if options[:index] == :no 220 224 aaf_configuration[:ferret_fields][field] = options 225 221 226 define_method("#{field}_to_ferret".to_sym) do 222 227 begin 223 val = content_for_field_name(field )228 val = content_for_field_name(field, dynamic_boost) 224 229 rescue 225 230 logger.warn("Error retrieving value for field #{field}: #{$!}") trunk/plugin/acts_as_ferret/lib/acts_as_ferret.rb
r234 r244 123 123 end 124 124 fields.each_pair do |field, options| 125 options = options.dup 126 options.delete(:boost) if options[:boost].is_a?(Symbol) 125 127 fi.add_field(field, { :store => :no, 126 128 :index => :yes }.update(options)) trunk/plugin/acts_as_ferret/lib/instance_methods.rb
r234 r244 108 108 def to_doc 109 109 logger.debug "creating doc for class: #{self.class.name}, id: #{self.id}" 110 returning doc = Ferret::Document.new do110 returning Ferret::Document.new do |doc| 111 111 # store the id of each item 112 112 doc[:id] = self.id … … 118 118 aaf_configuration[:ferret_fields].each_pair do |field, config| 119 119 doc[field] = self.send("#{field}_to_ferret") unless config[:ignore] 120 end 121 if aaf_configuration[:boost] 122 if self.respond_to?(aaf_configuration[:boost]) 123 boost = self.send aaf_configuration[:boost] 124 doc.boost = boost.to_i if boost 125 else 126 logger.error "boost option should point to an instance method: #{aaf_configuration[:boost]}" 127 end 120 128 end 121 129 end … … 130 138 end 131 139 132 def content_for_field_name(field) 133 self[field] || self.instance_variable_get("@#{field.to_s}".to_sym) || self.send(field.to_sym) 140 def content_for_field_name(field, dynamic_boost = nil) 141 field_data = self[field] || self.instance_variable_get("@#{field.to_s}".to_sym) || self.send(field.to_sym) 142 if (dynamic_boost && boost_value = self.send(dynamic_boost)) 143 field_data = Ferret::Field.new(field_data) 144 field_data.boost = boost_value.to_i 145 logger.debug "####### #{field_data}" 146 end 147 field_data 134 148 end 135 149
