Class: Makit::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/indexer.rb

Overview

This class provide methods for indexing objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndexer

Returns a new instance of Indexer.



10
11
12
13
# File 'lib/makit/indexer.rb', line 10

def initialize
  @keywords_index = {}
  @protoc_json_serializer = Makit::Serializer.new(Makit::Proto3Formats::JSON)
end

Instance Attribute Details

#keywords_indexObject

Hash of string key to string[] of keyword



8
9
10
# File 'lib/makit/indexer.rb', line 8

def keywords_index
  @keywords_index
end

#protoc_json_serializerObject

Hash of string key to string[] of keyword



8
9
10
# File 'lib/makit/indexer.rb', line 8

def protoc_json_serializer
  @protoc_json_serializer
end

Instance Method Details

#get_match_count(terms, keywords) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/makit/indexer.rb', line 39

def get_match_count(terms, keywords)
  match_count = 0
  terms.each do |term|
    match_count += 1 if keywords.include?(term)
  end
  match_count
end

#index(key, item) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/makit/indexer.rb', line 15

def index(key, item)
  # item must be serializable to json

  keywords = []
  hash = JSON.parse(item.to_json)
  hash.each_value do |value|
    value = value.to_s.downcase
    keywords << value if value.length >= 3 && !keywords.include?(value)
  end
  keywords.each do |keyword|
    @keywords_index[keyword] = [] unless @keywords_index.key?(keyword)
    @keywords_index[keyword] << key unless @keywords_index[keyword].include?(key)
  end
end

#search(query) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/makit/indexer.rb', line 29

def search(query)
  keys = []
  # todo, remove terms less that length of 3

  terms = query.downcase.split.reject { |term| term.length < 3 }
  keywords_index.each do |key, value| # {|kvp|

    keys << key if get_match_count(terms, value) == terms.length
  end
  keys
end