Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim characters option added and configurable #110

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/mongoid_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid_search/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ 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.
mb_chars.
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]}.
Expand All @@ -50,5 +52,4 @@ def self.normalize_keywords(text)
text = text.map(&stem_proc) if stem_keywords
text
end

end