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

Changeset 95

Show
Ignore:
Timestamp:
09/09/06 17:04:21 (2 years ago)
Author:
jk
Message:

r1107@monsoon: jk | 2006-09-09 19:03:52 +0200
highlighting support'

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plugin/acts_as_ferret/lib/class_methods.rb

    r93 r95  
    1212          options = {  
    1313            :store => :no,  
     14            :highlight => :yes,  
    1415            :index => :yes,  
    1516            :term_vector => :with_positions_offsets, 
  • trunk/plugin/acts_as_ferret/lib/instance_methods.rb

    r92 r95  
    55      module InstanceMethods 
    66        include MoreLikeThis 
     7 
     8          # Returns an array of strings with the matches highlighted. The +query+ can 
     9          # either a query String or a Ferret::Search::Query object. 
     10          #  
     11          # === Options 
     12          # 
     13          # field::            field to take the content from. This field has  
     14          #                    to have it's content stored in the index  
     15          #                    (:store => :yes in your call to aaf). If not 
     16          #                    given, all stored fields are searched, and the 
     17          #                    highlighted content found in all of them is returned. 
     18          #                    set :highlight => :no in the field options to 
     19          #                    avoid highlighting of contents from a :stored field. 
     20          # excerpt_length::   Default: 150. Length of excerpt to show. Highlighted 
     21          #                    terms will be in the centre of the excerpt. 
     22          # num_excerpts::     Default: 2. Number of excerpts to return. 
     23          # pre_tag::          Default: "<em>". Tag to place to the left of the 
     24          #                    match.   
     25          # post_tag::         Default: "</em>". This tag should close the 
     26          #                    +:pre_tag+. 
     27          # ellipsis::         Default: "...". This is the string that is appended 
     28          #                    at the beginning and end of excerpts (unless the 
     29          #                    excerpt hits the start or end of the field. You'll 
     30          #                    probably want to change this so a Unicode elipsis 
     31          #                    character. 
     32        def highlight(query, options = {}) 
     33          options = { :num_excerpts => 2, :pre_tag => '<em>', :post_tag => '</em>' }.update(options) 
     34          i = self.class.ferret_index 
     35          highlights = [] 
     36          i.synchronize do 
     37            doc_num = self.document_number 
     38            if options[:field] 
     39              highlights << i.highlight(query, doc_num, options) 
     40            else 
     41              fields_for_ferret.each_pair do |field, config| 
     42                next if config[:store] == :no || config[:highlight] == :no 
     43                options[:field] = field 
     44                highlights << i.highlight(query, doc_num, options) 
     45              end 
     46            end 
     47          end 
     48          return highlights.compact.flatten[0..options[:num_excerpts]-1] 
     49        end 
    750         
    851        # re-eneable ferret indexing after a call to #disable_ferret 
     
    4992        alias :ferret_update :ferret_create 
    5093         
     94 
    5195        # remove from index 
    5296        def ferret_destroy 
    5397          logger.debug "ferret_destroy: #{self.class.name} : #{self.id}" 
    5498          begin 
    55             query = Ferret::Search::TermQuery.new(:id, self.id.to_s) 
    56             if self.class.configuration[:single_index] 
    57               bq = Ferret::Search::BooleanQuery.new 
    58               bq.add_query(query, :must) 
    59               bq.add_query(Ferret::Search::TermQuery.new(:class_name, self.class.name), :must) 
    60               query = bq 
    61             end 
    62             self.class.ferret_index.query_delete(query) 
     99            self.class.ferret_index.query_delete(query_for_self) 
    63100          rescue 
    64101            logger.warn("Could not find indexed value for this object: #{$!}") 
     
    87124          return doc 
    88125        end 
     126 
     127        # returns the ferret document number this record has. 
     128        def document_number 
     129          hits = self.class.ferret_index.search(query_for_self) 
     130          return hits.hits.first.doc if hits.total_hits == 1 
     131          raise "cannot determine document number from primary key: #{self}" 
     132        end 
     133 
     134        protected 
     135 
     136        # build a ferret query matching only this record 
     137        def query_for_self 
     138          query = Ferret::Search::TermQuery.new(:id, self.id.to_s) 
     139          if self.class.configuration[:single_index] 
     140            bq = Ferret::Search::BooleanQuery.new 
     141            bq.add_query(query, :must) 
     142            bq.add_query(Ferret::Search::TermQuery.new(:class_name, self.class.name), :must) 
     143            return bq 
     144          end 
     145          return query 
     146        end 
     147 
    89148      end 
    90149 
  • trunk/plugin/acts_as_ferret/lib/more_like_this.rb

    r93 r95  
    100100 
    101101         
    102         def document_number 
    103           hits = self.class.ferret_index.search(Ferret::Search::TermQuery.new(:id, self.id.to_s)) 
    104           return hits.hits.first.doc if hits.total_hits == 1 
    105           raise "cannot determine document number from primary key: #{self}" 
    106         end 
    107102 
    108103        # creates a term/term_frequency map for terms from the fields 

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