class Groonga::Client::Searcher

Public Class Methods

schema() click to toggle source
# File lib/groonga/client/searcher.rb, line 28
def schema
  @schema ||= Schema.new(default_table_name)
end
source(model_class) click to toggle source
# File lib/groonga/client/searcher.rb, line 32
def source(model_class)
  sources[model_class] ||= create_source(model_class)
end
sync() click to toggle source
# File lib/groonga/client/searcher.rb, line 36
def sync
  sync_schema
  sync_records
end
sync_records() click to toggle source
# File lib/groonga/client/searcher.rb, line 49
def sync_records
  ensure_model_classes_loaded
  sources.each do |model_class, definition|
    all_records = model_class.all
    if all_records.respond_to?(:find_each)
      enumerator = all_records.find_each
    else
      enumerator = all_records.each
    end
    searcher = new
    enumerator.each do |model|
      searcher.upsert(model)
    end
  end
end
sync_schema() click to toggle source
# File lib/groonga/client/searcher.rb, line 41
def sync_schema
  current_schema = Client.open do |client|
    client.schema
  end
  syncher = SchemaSynchronizer.new(schema, current_schema)
  syncher.sync
end

Private Class Methods

create_source(model_class) click to toggle source
# File lib/groonga/client/searcher.rb, line 70
def create_source(model_class)
  source = Source.new(schema, model_class)

  searcher_class = self
  model_class.after_create do |model|
    searcher = searcher_class.new
    searcher.create(model)
  end

  model_class.after_update do |model|
    searcher = searcher_class.new
    searcher.update(model)
  end

  model_class.after_destroy do |model|
    searcher = searcher_class.new
    searcher.destroy(model)
  end

  source
end
default_table_name() click to toggle source
# File lib/groonga/client/searcher.rb, line 92
def default_table_name
  name.gsub(/Searcher\z/, "").tableize
end
ensure_model_classes_loaded() click to toggle source
# File lib/groonga/client/searcher.rb, line 96
def ensure_model_classes_loaded
  ::Rails.application.eager_load!
end
sources() click to toggle source
# File lib/groonga/client/searcher.rb, line 66
def sources
  @sources ||= {}
end

Public Instance Methods

create(model) click to toggle source
# File lib/groonga/client/searcher.rb, line 127
def create(model)
  upsert(model)
end
destroy(model) click to toggle source
# File lib/groonga/client/searcher.rb, line 135
def destroy(model)
  Client.open do |client|
    client.delete(table: self.class.schema.table,
                  key: model_key(model))
  end
end
inititalize() click to toggle source
# File lib/groonga/client/searcher.rb, line 101
def inititalize
end
select() click to toggle source
# File lib/groonga/client/searcher.rb, line 146
def select
  schema = self.class.schema
  full_text_searchable_column_names = []
  schema.columns.each do |name, column|
    if column.have_full_text_search_index?
      full_text_searchable_column_names << name
    elsif column.have_index? and column.vector?
      full_text_searchable_column_names << name
    end
  end
  extensions = [SelectRequest]
  Request::Select.new(schema.table, extensions).
    match_columns(full_text_searchable_column_names)
end
update(model) click to toggle source
# File lib/groonga/client/searcher.rb, line 131
def update(model)
  upsert(model)
end
upsert(model) click to toggle source
# File lib/groonga/client/searcher.rb, line 104
def upsert(model)
  source = self.class.source(model.class)
  record = {}
  source.columns.each do |name, reader|
    case reader
    when Symbol
      value = model.__send__(reader)
    when TrueClass
      value = model.__send__(name)
    when NilClass
      next
    else
      value = reader.call(model)
    end
    record[name] = value
  end
  record["_key"] = model_key(model)
  Client.open do |client|
    client.load(:table => self.class.schema.table,
                :values => [record])
  end
end

Private Instance Methods

model_key(model) click to toggle source
# File lib/groonga/client/searcher.rb, line 162
def model_key(model)
  "#{model.class.name}-#{model.id}"
end