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

Configurable strips #108

Merged
merged 4 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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 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
2 changes: 1 addition & 1 deletion lib/mongoid_search/mongoid_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ def index_keywords!

def set_keywords
self._keywords = Mongoid::Search::Util.keywords(self, self.search_fields).
flatten.reject{|k| k.nil? || k.empty?}.uniq.sort
flatten.reject{|k| k.nil? || k.empty?}.uniq.sort.push(*self.search_fields.map(&:to_s))
end
end
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