Module: IndexedSearch::Match

Defined in:
lib/indexed_search/match.rb,
lib/indexed_search/match/base.rb,
lib/indexed_search/match/leet.rb,
lib/indexed_search/match/stem.rb,
lib/indexed_search/match/exact.rb,
lib/indexed_search/match/result.rb,
lib/indexed_search/match/soundex.rb,
lib/indexed_search/match/initials.rb,
lib/indexed_search/match/metaphone.rb,
lib/indexed_search/match/start_with.rb,
lib/indexed_search/match/result_list.rb,
lib/indexed_search/match/american_soundex.rb,
lib/indexed_search/match/double_metaphone.rb

Defined Under Namespace

Classes: AmericanSoundex, Base, DoubleMetaphone, Exact, Initials, Leet, Metaphone, Result, ResultList, Soundex, StartWith, Stem

Class Method Summary collapse

Class Method Details

.match_class(type) ⇒ Object



12
13
14
15
16
17
# File 'lib/indexed_search/match.rb', line 12

def self.match_class(type)
  # todo: detect whether rails class caching is enabled and conditionally cache here too?
  #@@match_class ||= {}
  #@@match_class[type] ||= IndexedSearch::Match.const_get(type.to_s.camelize)
  IndexedSearch::Match.const_get(type.to_s.camelize)
end

.update_index(type) ⇒ Object

Update just the match column for a specific match type Warning: long running with a large index and many updates… but well suited for a maintenance rake task. Won’t run at a snail’s pace by instantiating millions of activerecord objects in memory, and won’t run out of memory by loading all the data into memory at once either!



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/indexed_search/match.rb', line 23

def self.update_index(type)
  klass = match_class(type)
  count = 0
  IndexedSearch::Word.order('word').batches_by_ids do |scope|
    updates = Hash.new { |hash,key| hash[key] = [] }
    scope.order_values = []
    matcher_attrs = klass.matcher_attribute
    matcher_attrs = [matcher_attrs] unless matcher_attrs.kind_of?(Array)
    scope.values_of(*([:id, :word] + matcher_attrs)).each do |id, word, *matches|
      vals = klass.match_against_term?(word) ? klass.make_index_value(word) : [nil] * matcher_attrs.length
      vals = [vals] * matcher_attrs.length unless vals.kind_of?(Array)
      atrs = {}
      (0...matcher_attrs.length).to_a.each do |idx|
        atrs[matcher_attrs[idx]] = vals[idx] if matches[idx] != vals[idx]
      end
      if atrs.present?
        updates[atrs] << id
        count += 1
      end
    end
    updates.each { |atrs,ids| IndexedSearch::Word.where(:id => ids).update_all(atrs) }
  end
  count
end