module ARIndexer::ARSearch

Public Class Methods

expand_forward_index(forward_index, stopwords) click to toggle source
# File lib/ar_indexer/index_search.rb, line 60
def self.expand_forward_index(forward_index, stopwords)
  # Stem and pluralize
  forward_index.each do |word|
    root = Stemmer::stem_word(word)
    unless forward_index.include? root
      forward_index = forward_index.inject([root], :<<)
    end
    plural = word.pluralize
    unless forward_index.include? plural
      forward_index = forward_index.inject([plural], :<<)
    end
  end
  
  # Remove stopwords and duplicates again
  stopwords = (Stopwords::STOPWORDS + stopwords).uniq
  forward_index = (forward_index - stopwords).uniq
  return forward_index
end
get_object_counts(base_results, search_models, match_type, match_threshold) click to toggle source
# File lib/ar_indexer/index_search.rb, line 79
def self.get_object_counts(base_results, search_models, match_type, match_threshold)
  relevancy_counts = {}
  unsorted_results = []
  search_models.each do |model|
    model_results = base_results.where(:model_constant => model)
    unless model_results.empty?
      relevancy_counts[model] = {}
      model_results.each do |result|
        id_array = result.retrieve_id_array
        id_array.each do |object_id|
          if relevancy_counts[model][object_id].nil?
            relevancy_counts[model][object_id] = 1
          else
            relevancy_counts[model][object_id] = (relevancy_counts[model][object_id] + 1)
          end
        end
      end
      if match_type == :all
        relevancy_counts[model].delete_if do |object_id, count|
          count < match_threshold
        end
      end
    end
    unsorted_results << relevancy_counts[model].to_a.map{|result| result << model}
  end
  return unsorted_results
end
sort_by_field(base_results, search_object, match_threshold) click to toggle source
# File lib/ar_indexer/index_search.rb, line 120
def self.sort_by_field(base_results, search_object, match_threshold)
  unsorted_results = ARSearch.get_object_counts(base_results, search_object.search_models, search_object.options(:match), match_threshold).flatten!(1) || []
  unless unsorted_results.empty?
    unsorted_objects = unsorted_results.collect {|result| result[2].constantize.find(result[0])}
    sort_method = search_object.options(:sort_method)
    case sort_method.class.to_s
    when 'Symbol'
      sorted_results = unsorted_objects.sort_by {|object| object[sort_method]}
    when 'Proc'
      sorted_results = unsorted_objects.sort_by {|object| sort_method.call(object)}
    else
      sorted_results = unsorted_objects
    end
    if search_object.options(:sort_direction) == :desc
      sorted_results = sorted_results.reverse
    end
    return sorted_results 
  else
    return []
  end
end
sort_by_relevance(base_results, search_object, match_threshold) click to toggle source
# File lib/ar_indexer/index_search.rb, line 107
def self.sort_by_relevance(base_results, search_object, match_threshold)
  unsorted_results = ARSearch.get_object_counts(base_results, search_object.search_models, search_object.options(:match), match_threshold).flatten!(1) || []
  unless unsorted_results.empty?
    sorted_results = unsorted_results.sort_by {|x| [x[1], x[0]]}
    if search_object.options(:sort_direction) == :desc
      sorted_results = sorted_results.reverse
    end
    return sorted_results.collect {|result| result[2].constantize.find(result[0])}
  else
    return []
  end
end