Class: Fuzzzy::Soundex::Searcher

Inherits:
Base show all
Defined in:
lib/fuzzzy/methods/soundex/searcher.rb

Constant Summary

Constants included from Redis

Redis::INDEX_KEY

Instance Attribute Summary

Attributes inherited from MethodBase

#context

Instance Method Summary collapse

Methods inherited from Base

#index_type, #soundex

Methods inherited from MethodBase

#index_name, #prepare_string, #stopwords, #with_context

Methods included from Redis

counter_key, #counter_key, #dictionary_key, #redis, #shared_key

Instance Method Details

#query_index_stringObject



31
32
33
# File 'lib/fuzzzy/methods/soundex/searcher.rb', line 31

def query_index_string
  context[:prepared_query] ||= prepare_string(context[:query])
end

#search(context) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fuzzzy/methods/soundex/searcher.rb', line 4

def search context
  with_context(context) do
    return if query_index_string.empty?

    if ids = redis.smembers(index_key(soundex))
      result = ids.map do |id|
        string = redis.get(dictionary_key(id))
        {
          :id => id,
          :distance => Levenshtein.distance(query_index_string, string),
          :alpha => string
        }
      end

      result.sort_by!{|item|item[sort_by]} if sort_by
      result.reject!{|item|item[:distance] > context[:distance]} if context[:distance]
      result.map{|item|item[:id]}
    else
      []
    end
  end
end

#sort_byObject



27
28
29
# File 'lib/fuzzzy/methods/soundex/searcher.rb', line 27

def sort_by
  context[:sort_by]
end