To edit pages or tickets please login with username/password: aaf/aaf

Changeset 244

Show
Ignore:
Timestamp:
09/17/07 09:15:10 (1 year ago)
Author:
jk
Message:

#166

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/demo/app/models/content_base.rb

    r142 r244  
    1212  # descriptions 
    1313  acts_as_ferret( :fields => { :comment_count => {}, 
    14                                :title         => { :boost => 2 },  
     14                               :title         => { :boost => :title_boost },  
    1515                               :description   => { :boost => 1, :store => :yes }, 
    1616                               :special       => {} }, 
    1717                  :store_class_name => true, 
     18                  :boost => :record_boost, 
    1819                  :remote           => ENV['AAF_REMOTE'] ) 
    1920 
    2021  def comment_count; 0 end 
    2122 
     23  attr_accessor :record_boost 
     24  attr_writer :title_boost 
     25  def title_boost 
     26    @title_boost || 2 
     27  end 
    2228end 
    2329 
  • trunk/demo/test/unit/content_test.rb

    r238 r244  
    556556      
    557557    # 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') 
    559559    assert_equal 2, contents_from_ferret.size 
    560560    assert_equal 2, total_hits 
     
    569569  def test_find_by_contents_boost 
    570570    # 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') 
    572572    assert_equal 2, contents_from_ferret.size 
    573573    assert_equal @another_content.id, contents_from_ferret.first.id 
     
    736736  end 
    737737 
     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 
    738763  protected 
    739764  def more_contents 
  • trunk/plugin/acts_as_ferret/lib/act_methods.rb

    r234 r244  
    212212    # document instance 
    213213    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 
    214218      options.reverse_merge!( :store       => :no,  
    215219                              :highlight   => :yes,  
     
    219223      options[:term_vector] = :no if options[:index] == :no 
    220224      aaf_configuration[:ferret_fields][field] = options 
     225 
    221226      define_method("#{field}_to_ferret".to_sym) do 
    222227        begin 
    223           val = content_for_field_name(field
     228          val = content_for_field_name(field, dynamic_boost
    224229        rescue 
    225230          logger.warn("Error retrieving value for field #{field}: #{$!}") 
  • trunk/plugin/acts_as_ferret/lib/acts_as_ferret.rb

    r234 r244  
    123123    end 
    124124    fields.each_pair do |field, options| 
     125      options = options.dup 
     126      options.delete(:boost) if options[:boost].is_a?(Symbol) 
    125127      fi.add_field(field, { :store => :no,  
    126128                            :index => :yes }.update(options))  
  • trunk/plugin/acts_as_ferret/lib/instance_methods.rb

    r234 r244  
    108108    def to_doc 
    109109      logger.debug "creating doc for class: #{self.class.name}, id: #{self.id}" 
    110       returning doc = Ferret::Document.new do 
     110      returning Ferret::Document.new do |doc| 
    111111        # store the id of each item 
    112112        doc[:id] = self.id 
     
    118118        aaf_configuration[:ferret_fields].each_pair do |field, config| 
    119119          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 
    120128        end 
    121129      end 
     
    130138    end 
    131139 
    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 
    134148    end 
    135149 

To edit pages or tickets please login with username/password: aaf/aaf