molten-trunk/molten5_withgrid/vendor/plugins/acts_as_ferret/lib/more_like_this.rb
# returns other instances of this class, which have similar contents
# like this one. Basically works like this: find out n most interesting
# (i.e. characteristic) terms from this document, and then build a
# query from those which is run against the whole index. Which terms
# are interesting is decided on variour criteria which can be
# influenced by the given options. 
#
# The algorithm used here is a quite straight port of the MoreLikeThis class
# from Apache Lucene.
#
# options are:
# :field_names : Array of field names to use for similarity search (mandatory)
# :min_term_freq => 2,  # Ignore terms with less than this frequency in the source doc.
# :min_doc_freq => 5,   # Ignore words which do not occur in at least this many docs
# :min_word_length => nil, # Ignore words shorter than this length (longer words tend to 
#                            be more characteristic for the document they occur in).
# :max_word_length => nil, # Ignore words if greater than this len.
# :max_query_terms => 25,  # maximum number of terms in the query built
# :max_num_tokens => 5000, # maximum number of tokens to examine in a single field
# :boost => false,         # when true, a boost according to the relative score of 
#                            a term is applied to this Term's TermQuery.
# :similarity => 'DefaultAAFSimilarity'   # the similarity implementation to use (the default 
#                                           equals Ferret's internal similarity implementation)
# :analyzer => 'Ferret::Analysis::StandardAnalyzer' # class name of the analyzer to use
# :append_to_query => nil # proc taking a query object as argument, which will be called after generating the query. can be used to further manipulate the query used to find related documents, i.e. to constrain the search to a given class in single table inheritance scenarios
# ferret_options : Ferret options handed over to find_by_contents (i.e. for limits and sorting)
# ar_options : options handed over to find_by_contents for AR scoping
returns other instances of this class, which have similar contents
like this one. Basically works like this: find out n most interesting
terms from this document, and then build a
query from those which is run against the whole index. Which terms
are interesting is decided on variour criteria which can be
influenced by the given options.

The algorithm used here is a quite straight port of the MoreLikeThis class
from Apache Lucene.

options are:
:field_names : Array of field names to use for similarity search (mandatory)
:min_term_freq => 2,  # Ignore terms with less than this frequency in the source doc.
:min_doc_freq => 5,   # Ignore words which do not occur in at least this many docs
:min_word_length => nil, # Ignore words shorter than this length (longer words tend to
be more characteristic for the document they occur in).
:max_word_length => nil, # Ignore words if greater than this len.
:max_query_terms => 25,  # maximum number of terms in the query built
:max_num_tokens => 5000, # maximum number of tokens to examine in a single field
:boost => false,         # when true, a boost according to the relative score of
a term is applied to this Term's TermQuery.
returns other instances of this class which have similar contents like this one . Basically works like this : find out n most interesting terms from this document and then build a query from those which is run against the whole index . Which terms are interesting is decided on variour criteria which can be influenced by the given options . The algorithm used here is a quite straight port of the MoreLikeThis class from Apache Lucene . options are : : field_names : Array of field names to use for similarity search ( mandatory ) : min_term_freq = > 2 # Ignore terms with less than this frequency in the source doc . : min_doc_freq = > 5 # Ignore words which do not occur in at least this many docs : min_word_length = > nil # Ignore words shorter than this length ( longer words tend to be more characteristic for the document they occur in ) . : max_word_length = > nil # Ignore words if greater than this len . : max_query_terms = > 25 # maximum number of terms in the query built : max_num_tokens = > 5000 # maximum number of tokens to examine in a single field : boost = > false # when true a boost according to the relative score of a term is applied to this Term ' s TermQuery .
def more_like_this(options = {}, ferret_options = {}, ar_options = {})
          options = {
            :field_names => nil,  
            :min_term_freq => 2,  
            :min_doc_freq => 5,   
            :min_word_length => 0, 
            :max_word_length => 0, 
            :max_query_terms => 25,  
            :max_num_tokens => 5000, 
            :boost => false,      
            :similarity => 'ActsAsFerret::MoreLikeThis::DefaultAAFSimilarity',  
            :analyzer => 'Ferret::Analysis::StandardAnalyzer', 
            :append_to_query => nil,
            :base_class => self.class 
          }.update(options)
          clazz = options[:base_class]
          options[:base_class] = clazz.name
          query = clazz.aaf_index.build_more_like_this_query(self.id, self.class.name, options)
          options[:append_to_query].call(query) if options[:append_to_query]
          clazz.find_by_contents(query, ferret_options, ar_options)
        end
def more_like_this ( options = { } , ferret_options = { } , ar_options = { } ) options = { :field_names => nil , :min_term_freq => 2 , :min_doc_freq => 5 , :min_word_length => 0 , :max_word_length => 0 , :max_query_terms => 25 , :max_num_tokens => 5000 , :boost => false , :similarity => 'ActsAsFerret::MoreLikeThis::DefaultAAFSimilarity' , :analyzer => 'Ferret::Analysis::StandardAnalyzer' , :append_to_query => nil , :base_class => self . class } . update ( options ) clazz = options [ :base_class ] options [ :base_class ] = clazz . name query = clazz . aaf_index . build_more_like_this_query ( self . id , self . class . name , options ) options [ :append_to_query ] . call ( query ) if options [ :append_to_query ] clazz . find_by_contents ( query , ferret_options , ar_options ) end
returns other instances of this class, which have similar contents
like this one.
returns other instances of this class which have similar contents like this one .
# Default field names # Ignore terms with less than this frequency in the source doc. # Ignore words which do not occur in at least this many docs # Ignore words if less than this len. Default is not to ignore any words. # Ignore words if greater than this len. Default is not to ignore any words. # maximum number of terms in the query built # maximum number of tokens to analyze when analyzing contents # class name of the similarity implementation to use # class name of the analyzer to use # base class to use for querying, useful in STI scenarios where BaseClass.find_by_contents can be used to retrieve results from other classes, too #index.search_each('id:*') do |doc, score| #  puts "#{doc} == #{index[doc][:description]}" #end
options
ferret_options
ar_options