Module: MongoMapper::Search::ClassMethods

Defined in:
lib/mongomapper_search/search.rb

Instance Method Summary collapse

Instance Method Details

#idf(field, term, total) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/mongomapper_search/search.rb', line 86

def idf(field, term, total)
  df = where(field => /#{term}/ ).count
  if df != 0
    Math.log(total/df)
  else
    0
  end
end

#index_keywords!Object

Goes through all documents in the class that includes Mongoid::Search and indexes the keywords.



136
137
138
# File 'lib/mongomapper_search/search.rb', line 136

def index_keywords!
  all.each { |d| d.index_keywords! }
end

#search(query, options = {}, criteria = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mongomapper_search/search.rb', line 95

def search(query, options={}, criteria = {})
  return all(criteria) if query.blank? && (options[:allow_empty_search] || allow_empty_search)
  
  keywords = Util.normalize_keywords(query, stem_keywords)

  unique_keywords = keywords.uniq
  
  regexed_keywords = []
                  
  unique_keywords.each do |keyword|
    regexed_keywords.concat([/#{keyword}/])
  end
   
  search_match = options[:match]||self.match

  total = all.count

  results = []
  self.search_fields.each do |key, value|
    if key.class == Hash
      key.each do |sub_key, sub_value|
        field = "_#{sub_key}_#{sub_value}".to_sym
        search_a_field search_match, field, regexed_keywords, results, value, unique_keywords, total, criteria
      end
    else
      field = "_#{key}".to_sym
      search_a_field search_match, field, regexed_keywords, results, value, unique_keywords, total, criteria
    end
  end

  results.sort { |a,b| b.rank <=> a.rank }
  documents = []
  results.each do |result|
    documents << result.document
  end

  documents
end

#search_a_field(search_match, field, regexed_keywords, results, boost, terms, total, criteria) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mongomapper_search/search.rb', line 55

def search_a_field(search_match, field, regexed_keywords, results, boost, terms, total, criteria)
  if search_match == :all
    field_results = where(criteria).where(field => { "$all" => regexed_keywords })
  elsif search_match == :any
    field_results = where(criteria).where(field => regexed_keywords )
  end
  field_results.each do |field_result|
    result = RankedDocument.new(field_result)
    rank = 0
    terms.each do |term|
      rank += boost * tf_idf(field, term, result.document[field], total)
    end

    if !results.include?(result)
      result.rank = rank
      results << result
    else
      i = results.index result
      results[i].rank += rank
    end
  end
end

#search_in(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mongomapper_search/search.rb', line 20

def search_in(*args)
  options = args.last.is_a?(Hash) && [:allow_empty_search, :stem_keywords].include?(args.last.keys.first) ? args.pop : {}
  self.match              = [:any, :all].include?(options[:match]) ? options[:match] : :any
  self.allow_empty_search = [true, false].include?(options[:allow_empty_search]) ? options[:allow_empty_search] : false
  self.stem_keywords      = [true, false].include?(options[:stem_keywords]) ? options[:allow_empty_search] : false
  self.search_fields      = self.search_fields || {}
  
  args.each do |arg|
    if arg.class == Hash
      arg.each do |key, value|
        if key.class == Hash
          key.each do |sub_key, sub_value|
            field = "_#{sub_key}_#{sub_value}".to_sym
            key field, Array
            ensure_index field, :background => true
            self.search_fields[key] = value
          end
        else
          field = "_#{key}".to_sym
          key field, Array
          ensure_index field, :background => true
          self.search_fields[key] = value
        end
      end
    else
        field = "_#{arg}".to_sym
        key field, Array
        ensure_index field, :background => true
        self.search_fields[arg] = 1
    end
  end
        
  before_save :set_keywords
end

#tf(term, terms) ⇒ Object



82
83
84
# File 'lib/mongomapper_search/search.rb', line 82

def tf(term, terms)
  terms.count(term)
end

#tf_idf(field, term, terms, total) ⇒ Object



78
79
80
# File 'lib/mongomapper_search/search.rb', line 78

def tf_idf(field, term, terms, total)
  tf(term, terms) * idf(field, term, total)
end