class RediSearch::Document

Attributes

attributes[R]
index[R]
score[R]

Public Class Methods

for_object(index, record, serializer: nil, only: []) click to toggle source
# File lib/redi_search/document.rb, line 11
def for_object(index, record, serializer: nil, only: [])
  object_to_serialize = serializer&.new(record) || record

  field_values = index.schema.fields.map(&:name).map do |field|
    next unless only.empty? || only.include?(field)

    [field.to_s, object_to_serialize.public_send(field)]
  end.compact.to_h

  new(index, object_to_serialize.id, field_values)
end
get(index, document_id) click to toggle source
# File lib/redi_search/document.rb, line 23
def get(index, document_id)
  Finder.new(index, document_id).find
end
new(index, document_id, fields, score = nil) click to toggle source
# File lib/redi_search/document.rb, line 30
def initialize(index, document_id, fields, score = nil)
  @index = index
  @document_id = document_id
  @attributes = fields
  @score = score

  load_attributes
end

Public Instance Methods

del() click to toggle source
# File lib/redi_search/document.rb, line 39
def del
  RediSearch.client.call!("DEL", document_id, skip_ft: true).ok?
end
document_id() click to toggle source
# File lib/redi_search/document.rb, line 55
def document_id
  if @document_id.to_s.start_with? index.name
    @document_id
  else
    "#{index.name}#{@document_id}"
  end
end
document_id_without_index() click to toggle source
# File lib/redi_search/document.rb, line 63
def document_id_without_index
  if @document_id.to_s.start_with? index.name
    @document_id.gsub(index.name, "")
  else
    @document_id
  end
end
redis_attributes() click to toggle source
# File lib/redi_search/document.rb, line 49
def redis_attributes
  attributes.flat_map do |field, value|
    [field, index.schema[field.to_sym].serialize(value)]
  end
end
schema_fields() click to toggle source
# File lib/redi_search/document.rb, line 43
def schema_fields
  @schema_fields ||= index.schema.fields.map do |field|
    field.name.to_s
  end
end

Private Instance Methods

load_attributes() click to toggle source
# File lib/redi_search/document.rb, line 75
def load_attributes
  attributes.each do |field, value|
    next unless schema_fields.include? field.to_s

    instance_variable_set(:"@#{field}", value)
    define_singleton_method(field) { value }
  end
end