Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciozaffari committed Jun 20, 2011
1 parent 5233e5d commit 7abed5c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
28 changes: 3 additions & 25 deletions lib/mongoid_search/mongoid_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def search(query, options={})

def search_without_relevance(query, options={})
return criteria.all if query.blank? && allow_empty_search
criteria.send("#{(options[:match]||self.match).to_s}_in", :_keywords => Util.keywords(query, stem_keywords, ignore_list).map { |q| /#{q}/ })
criteria.send("#{(options[:match]||self.match).to_s}_in", :_keywords => Util.normalize_keywords(query, stem_keywords, ignore_list).map { |q| /#{q}/ })
end

def search_relevant(query, options={})
return criteria.all if query.blank? && allow_empty_search

keywords = Util.keywords(query, stem_keywords, ignore_list)
keywords = Util.normalize_keywords(query, stem_keywords, ignore_list)

map = <<-EOS
function() {
Expand Down Expand Up @@ -105,31 +105,9 @@ def index_keywords!
end

private

# TODO: This need some refactoring..
def set_keywords
self._keywords = self.search_fields.map do |field|
if field.is_a?(Hash)
field.keys.map do |key|
attribute = self.send(key)
unless attribute.blank?
method = field[key]
if attribute.is_a?(Array)
if method.is_a?(Array)
method.map {|m| attribute.map { |a| Util.keywords a.send(m), stem_keywords, ignore_list } }
else
attribute.map(&method).map { |t| Util.keywords t, stem_keywords, ignore_list }
end
else
Util.keywords(attribute.send(method), stem_keywords, ignore_list)
end
end
end
else
value = self[field]
value = value.join(' ') if value.respond_to?(:join)
Util.keywords(value, stem_keywords, ignore_list) if value
end
Util.keywords(self, field, stem_keywords, ignore_list)
end.flatten.reject{|k| k.nil? || k.empty?}.uniq.sort
end
end
26 changes: 25 additions & 1 deletion lib/mongoid_search/util.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
module Util

def self.keywords(text, stem_keywords, ignore_list)
def self.keywords(klass, field, stem_keywords, ignore_list)
if field.is_a?(Hash)
field.keys.map do |key|
attribute = klass.send(key)
unless attribute.blank?
method = field[key]
if attribute.is_a?(Array)
if method.is_a?(Array)
method.map {|m| attribute.map { |a| Util.normalize_keywords a.send(m), stem_keywords, ignore_list } }
else
attribute.map(&method).map { |t| Util.normalize_keywords t, stem_keywords, ignore_list }
end
else
Util.normalize_keywords(attribute.send(method), stem_keywords, ignore_list)
end
end
end
else
value = klass[field]
value = value.join(' ') if value.respond_to?(:join)
Util.normalize_keywords(value, stem_keywords, ignore_list) if value
end
end

def self.normalize_keywords(text, stem_keywords, ignore_list)
return [] if text.blank?
text = text.to_s.
mb_chars.
Expand Down
20 changes: 10 additions & 10 deletions spec/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@

describe Util do
it "should return an empty array if no text is passed" do
Util.keywords("", false, "").should == []
Util.normalize_keywords("", false, "").should == []
end

it "should return an array of keywords" do
Util.keywords("keyword", false, "").class.should == Array
Util.normalize_keywords("keyword", false, "").class.should == Array
end

it "should return an array of strings" do
Util.keywords("keyword", false, "").first.class.should == String
Util.normalize_keywords("keyword", false, "").first.class.should == String
end

it "should remove accents from the text passed" do
Util.keywords("café", false, "").should == ["cafe"]
Util.normalize_keywords("café", false, "").should == ["cafe"]
end

it "should downcase the text passed" do
Util.keywords("CaFé", false, "").should == ["cafe"]
Util.normalize_keywords("CaFé", false, "").should == ["cafe"]
end

it "should split whitespaces, hifens, dots, underlines, etc.." do
Util.keywords("CaFé-express.com delicious;come visit, and 'win' an \"iPad\"", false, "").should == ["cafe", "express", "com", "delicious", "come", "visit", "and", "win", "an", "ipad"]
Util.normalize_keywords("CaFé-express.com delicious;come visit, and 'win' an \"iPad\"", false, "").should == ["cafe", "express", "com", "delicious", "come", "visit", "and", "win", "an", "ipad"]
end

it "should stem keywords" do
Util.keywords("A runner running and eating", true, "").should == ["runner", "run", "and", "eat"]
Util.normalize_keywords("A runner running and eating", true, "").should == ["runner", "run", "and", "eat"]
end

it "should ignore keywords from ignore list" do
Util.keywords("An amazing awesome runner running and eating", true, YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))["ignorelist"]).should == ["an", "runner", "run", "and", "eat"]
Util.normalize_keywords("An amazing awesome runner running and eating", true, YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))["ignorelist"]).should == ["an", "runner", "run", "and", "eat"]
end

it "should ignore keywords with less than two words" do
Util.keywords("A runner running", false, "").should_not include "a"
Util.normalize_keywords("A runner running", false, "").should_not include "a"
end

it "should not ignore numbers" do
Util.keywords("Ford T 1908", false, "").should include "1908"
Util.normalize_keywords("Ford T 1908", false, "").should include "1908"
end
end
2 changes: 1 addition & 1 deletion tasks/mongoid_search.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace :mongoid_search do
desc 'Goes through all documents with search enabled and indexes the keywords.'
task :index do
task :index => :environment do
Mongoid::Search.classes.each do |klass|
Log.silent = ENV['SILENT']
Log.log "\nIndexing documents for #{klass.name}:\n"
Expand Down

0 comments on commit 7abed5c

Please sign in to comment.