Module: FuzzySearch::ModelExtensions::InstanceMethods

Defined in:
lib/fuzzy_model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#update_fuzzy_search_trigrams!Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fuzzy_model_extensions.rb', line 83

def update_fuzzy_search_trigrams!
  FuzzySearchTrigram.delete_all(:rec_id => self.id, :rec_type => self.class.name)

  # to avoid double entries
  tokens = []
  self.class.fuzzy_search_properties.each do |prop|
    prop_value = send(prop)
    next if prop_value.nil?
    # split the property into words (which are separated by whitespaces)
    # and generate the trigrams for each word
    prop_value.to_s.split(/[\s\-]+/).each do |p|
      # put a space in front and at the end to emphasize the endings
      word = ' ' + self.class.normalize(p) + ' '
      word_as_chars = word.mb_chars
      (0..word_as_chars.length - 3).each do |idx|
        token = word_as_chars[idx, 3].to_s
        tokens << token unless tokens.member?(token)
      end
    end
  end

  FuzzySearchTrigram.import(
    [:token, :rec_id, :rec_type],
    tokens.map{|t| [t, self.id, self.class.name]},
    :validate => false
  )
end