Module: Mongoid::Search::ClassMethods
- Defined in:
- lib/mongoid_search/mongoid_search.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#index_keywords! ⇒ Object
Goes through all documents in the class that includes Mongoid::Search and indexes the keywords.
- #keyword_stemmer(options = {}) ⇒ Object
- #search(query, options = {}) ⇒ Object (also: #csearch)
-
#search_in(*args) ⇒ Object
Set a field or a number of fields as sources for search.
- #search_relevant(query, options = {}) ⇒ Object
- #search_without_relevance(query, options = {}) ⇒ Object
Instance Method Details
#index_keywords! ⇒ Object
Goes through all documents in the class that includes Mongoid::Search and indexes the keywords.
108 109 110 |
# File 'lib/mongoid_search/mongoid_search.rb', line 108 def index_keywords! all.each { |d| d.index_keywords! ? MongoidSearch::Log.green(".") : MongoidSearch::Log.red("F") } end |
#keyword_stemmer(options = {}) ⇒ Object
60 61 62 |
# File 'lib/mongoid_search/mongoid_search.rb', line 60 def keyword_stemmer(={}) stemmer_class.new(:language => [:language]) if stem_keywords end |
#search(query, options = {}) ⇒ Object Also known as: csearch
43 44 45 46 47 48 49 |
# File 'lib/mongoid_search/mongoid_search.rb', line 43 def search(query, ={}) if relevant_search search_relevant(query, ) else search_without_relevance(query, ) end end |
#search_in(*args) ⇒ Object
Set a field or a number of fields as sources for search
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mongoid_search/mongoid_search.rb', line 21 def search_in(*args) = args.last.is_a?(Hash) && [:match, :allow_empty_search, :relevant_search, :stem_keywords, :ignore_list].include?(args.last.keys.first) ? args.pop : {} self.match = [:any, :all].include?([:match]) ? [:match] : :any self.allow_empty_search = [true, false].include?([:allow_empty_search]) ? [:allow_empty_search] : false self.relevant_search = [true, false].include?([:relevant_search]) ? [:relevant_search] : false self.stemmer_class = [:stemmer_class] self.stem_keywords = !!stemmer_class || [:stem_keywords] self.stemmer_class ||= MongoidSearch::Stemmers.available if stem_keywords && stemmer_class.nil? raise "No stemmer found. Please, install either fast-stemmer or ruby-stemmer." end self.ignore_list = YAML.load(File.open([:ignore_list]))["ignorelist"] if [:ignore_list].present? self.search_fields = (self.search_fields || []).concat args field :_keywords, :type => Array index :_keywords, :background => true before_save :set_keywords end |
#search_relevant(query, options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mongoid_search/mongoid_search.rb', line 64 def search_relevant(query, ={}) return criteria.all if query.blank? && allow_empty_search keywords = MongoidSearch::Util.normalize_keywords(query, keyword_stemmer(), ignore_list) map = <<-EOS function() { var entries = 0 for(i in keywords) for(j in this._keywords) { if(this._keywords[j] == keywords[i]) entries++ } if(entries > 0) emit(this._id, entries) } EOS reduce = <<-EOS function(key, values) { return(values[0]) } EOS #raise [self.class, self.inspect].inspect kw_conditions = keywords.map do |kw| {:_keywords => kw} end criteria = (criteria || self).any_of(*kw_conditions) query = criteria.selector .delete(:limit) .delete(:skip) .merge! :scope => {:keywords => keywords}, :query => query # res = collection.map_reduce(map, reduce, options) # res.find.sort(['value', -1]) # Cursor collection.map_reduce(map, reduce, ) end |
#search_without_relevance(query, options = {}) ⇒ Object
55 56 57 58 |
# File 'lib/mongoid_search/mongoid_search.rb', line 55 def search_without_relevance(query, ={}) return criteria.all if query.blank? && allow_empty_search criteria.send("#{([:match]||self.match).to_s}_in", :_keywords => MongoidSearch::Util.normalize_keywords(query, keyword_stemmer(), ignore_list).map { |q| /#{q}/ }) end |