Skip to content

Commit

Permalink
Merge pull request #108 from sveredyuk/configurable-strip
Browse files Browse the repository at this point in the history
Configurable strips
  • Loading branch information
rtrv authored Oct 30, 2017
2 parents 9ef5af7 + a21f521 commit 7870f85
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions lib/mongoid_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/mongoid_search/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ 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.
mb_chars.
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 }
Expand Down
2 changes: 1 addition & 1 deletion mongoid_search.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 =["[email protected]"]
s.homepage = "http://www.papodenerd.net/mongoid-search-full-text-search-for-your-mongoid-models/"
Expand Down

0 comments on commit 7870f85

Please sign in to comment.