class ARIndexer::ARSearch::IndexSearch
Public Class Methods
new(models, opts = {})
click to toggle source
# File lib/ar_indexer/index_search.rb, line 4 def initialize(models, opts = {}) @models = {} models.each do |model| @models[model.to_s.split('::').last] = model end @options = { :fields => [], :match => :any, :sort => :relevance, :sort_method => nil, :sort_direction => :desc, :stopwords => [], :no_results_message => 'No results were returned for the given search term.' } @options.merge!(opts) end
Public Instance Methods
no_results_message()
click to toggle source
# File lib/ar_indexer/index_search.rb, line 55 def no_results_message return @options[:no_results_message] end
options(key)
click to toggle source
# File lib/ar_indexer/index_search.rb, line 26 def options(key) return @options[key] end
search(value)
click to toggle source
# File lib/ar_indexer/index_search.rb, line 30 def search(value) # Build array of words for query `reverse_indices.word IN ('word1', 'word2')` if @options[:match] == :any search_terms = ARSearch.expand_forward_index(Indexer.break_string(value), @options[:stopwords]) enforce_threshold = false else stopwords = (Stopwords::STOPWORDS + @options[:stopwords]).uniq search_terms = (Indexer.break_string(value) - stopwords) enforce_threshold = true end # Execute AR query based on @options[:fields] if @options[:fields].empty? base_results = ReverseIndex.where(:model_constant => self.search_models, :word => search_terms) else base_results = ReverseIndex.where(:model_constant => self.search_models, :field_name => @options[:fields], :word => search_terms) end unless base_results.empty? return ARSearch.method("sort_by_#{@options[:sort]}".to_sym).call(base_results, self, search_terms.count) else return [] end end
search_models()
click to toggle source
# File lib/ar_indexer/index_search.rb, line 22 def search_models return @models.keys end