From 1b7790622f667c6c1703ba4223f3ccfcb3d418d4 Mon Sep 17 00:00:00 2001 From: Volodya Sveredyuk Date: Tue, 20 Dec 2016 11:57:24 +0200 Subject: [PATCH 1/4] Configurable strips --- lib/mongoid_search.rb | 8 ++++++++ lib/mongoid_search/util.rb | 6 ++++-- mongoid_search.gemspec | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) 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/" From ac6d7521d57d48675d413c743e6b30310d4361d3 Mon Sep 17 00:00:00 2001 From: Artem Krivonozhko Date: Mon, 30 Oct 2017 14:15:25 +0300 Subject: [PATCH 2/4] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) 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 From e9d8b3045ebed2ca19a9b7ab8070dae001a1e748 Mon Sep 17 00:00:00 2001 From: Volodymyr Sveredyuk Date: Mon, 30 Oct 2017 13:54:07 +0200 Subject: [PATCH 3/4] Sort filtered keys --- lib/mongoid_search/mongoid_search.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoid_search/mongoid_search.rb b/lib/mongoid_search/mongoid_search.rb index a06095a..8bfdbcf 100644 --- a/lib/mongoid_search/mongoid_search.rb +++ b/lib/mongoid_search/mongoid_search.rb @@ -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 From a21f52147160d96e848845475e997d9a1e12ebc3 Mon Sep 17 00:00:00 2001 From: Volodymyr Sveredyuk Date: Mon, 30 Oct 2017 14:37:39 +0200 Subject: [PATCH 4/4] Revert "Sort filtered keys" This reverts commit e9d8b3045ebed2ca19a9b7ab8070dae001a1e748. --- lib/mongoid_search/mongoid_search.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoid_search/mongoid_search.rb b/lib/mongoid_search/mongoid_search.rb index 8bfdbcf..a06095a 100644 --- a/lib/mongoid_search/mongoid_search.rb +++ b/lib/mongoid_search/mongoid_search.rb @@ -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.push(*self.search_fields.map(&:to_s)) + flatten.reject{|k| k.nil? || k.empty?}.uniq.sort end end