diff --git a/README.md b/README.md index 5906759..31a5b30 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,12 @@ Alternatively, you can create an initializer to setup those options: # http://en.wikipedia.org/wiki/Typographic_ligature config.ligatures = { "œ"=>"oe", "æ"=>"ae" } + # Strip symbols regex to be replaced. These symbols will be replaced by space + config.strip_symbols = /[._:;'\"`,?|+={}()!@#%^&*<>~\$\-\\\/\[\]]/ + + # Strip accents regex to be replaced. These sybols will be removed after strip_symbols replacing + config.strip_accents = /[^\s\p{Alnum}]/ + # Minimum word size. Words smaller than it won't be indexed config.minimum_word_size = 2 end diff --git a/lib/mongoid_search.rb b/lib/mongoid_search.rb index 09d9d0c..f54ee79 100644 --- a/lib/mongoid_search.rb +++ b/lib/mongoid_search.rb @@ -60,6 +60,14 @@ module Mongoid::Search mattr_accessor :minimum_word_size @@minimum_word_size = 2 + # Strip special symbols + mattr_accessor :strip_symbols + @@strip_symbols = /[._:;'\"`,?|+={}()!@#%^&*<>~\$\-\\\/\[\]]/ + + # Strip accents + mattr_accessor :strip_accents + @@strip_accents = /[^\s\p{Alnum}]/ + def self.setup yield self end diff --git a/lib/mongoid_search/util.rb b/lib/mongoid_search/util.rb index 44df1dd..ca20d1a 100644 --- a/lib/mongoid_search/util.rb +++ b/lib/mongoid_search/util.rb @@ -34,6 +34,8 @@ def self.normalize_keywords(text) ignore_list = Mongoid::Search.ignore_list stem_keywords = Mongoid::Search.stem_keywords stem_proc = Mongoid::Search.stem_proc + strip_symbols = Mongoid::Search.strip_symbols + strip_accents = Mongoid::Search.strip_accents return [] if text.blank? text = text.to_s. @@ -41,8 +43,8 @@ def self.normalize_keywords(text) normalize(:kd). downcase. to_s. - gsub(/[._:;'"`,?|+={}()!@#%^&*<>~\$\-\\\/\[\]]/, ' '). # strip punctuation - gsub(/[^\s\p{Alnum}]/,''). # strip accents + gsub(strip_symbols, ' '). # strip symbols + gsub(strip_accents, ''). # strip accents gsub(/[#{ligatures.keys.join("")}]/) {|c| ligatures[c]}. split(' '). reject { |word| word.size < Mongoid::Search.minimum_word_size } diff --git a/mongoid_search.gemspec b/mongoid_search.gemspec index 0a73aa5..992baf2 100644 --- a/mongoid_search.gemspec +++ b/mongoid_search.gemspec @@ -4,7 +4,7 @@ $:.unshift lib unless $:.include?(lib) Gem::Specification.new do |s| s.name = "mongoid_search" - s.version = "0.3.3" + s.version = "0.3.4" s.authors = ["Mauricio Zaffari"] s.email =["mauricio@papodenerd.net"] s.homepage = "http://www.papodenerd.net/mongoid-search-full-text-search-for-your-mongoid-models/"