diff --git a/lib/mongoid_search.rb b/lib/mongoid_search.rb index 09d9d0c..4c6b970 100644 --- a/lib/mongoid_search.rb +++ b/lib/mongoid_search.rb @@ -27,6 +27,12 @@ module Mongoid::Search mattr_accessor :stem_proc @@stem_proc = Proc.new { |word| word.stem } + # before the text is altered we can trim some characters + # it allows us to avoid replacing punctuations such as '-' to ' ' + # which would not be beneficial in cases like date matching (`2017-10-08`) + mattr_accessor :trim_characters + @@trim_characters = [] + ## Words to ignore mattr_accessor :ignore_list @@ignore_list = [] diff --git a/lib/mongoid_search/util.rb b/lib/mongoid_search/util.rb index 44df1dd..f08edeb 100644 --- a/lib/mongoid_search/util.rb +++ b/lib/mongoid_search/util.rb @@ -34,6 +34,7 @@ def self.normalize_keywords(text) ignore_list = Mongoid::Search.ignore_list stem_keywords = Mongoid::Search.stem_keywords stem_proc = Mongoid::Search.stem_proc + trim_characters = Mongoid::Search.trim_characters return [] if text.blank? text = text.to_s. @@ -41,6 +42,7 @@ def self.normalize_keywords(text) normalize(:kd). downcase. to_s. + gsub(/[#{trim_characters.join("")}]/, ''). gsub(/[._:;'"`,?|+={}()!@#%^&*<>~\$\-\\\/\[\]]/, ' '). # strip punctuation gsub(/[^\s\p{Alnum}]/,''). # strip accents gsub(/[#{ligatures.keys.join("")}]/) {|c| ligatures[c]}. @@ -50,5 +52,4 @@ def self.normalize_keywords(text) text = text.map(&stem_proc) if stem_keywords text end - end