Class: RediSearch::Document

Inherits:
Object
  • Object
show all
Includes:
Display
Defined in:
lib/redi_search/document.rb,
lib/redi_search/document/finder.rb,
lib/redi_search/document/display.rb

Defined Under Namespace

Modules: Display Classes: Finder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Display

#inspect, #pretty_print, #pretty_print_attributes

Constructor Details

#initialize(index, document_id, fields, score = nil) ⇒ Document



25
26
27
28
29
30
31
32
# File 'lib/redi_search/document.rb', line 25

def initialize(index, document_id, fields, score = nil)
  @index = index
  @document_id = document_id
  @attributes = fields
  @score = score

  load_attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



23
24
25
# File 'lib/redi_search/document.rb', line 23

def attributes
  @attributes
end

#scoreObject (readonly)

Returns the value of attribute score.



23
24
25
# File 'lib/redi_search/document.rb', line 23

def score
  @score
end

Class Method Details

.for_object(index, record, only: []) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/redi_search/document.rb', line 8

def for_object(index, record, only: [])
  field_values = index.schema.fields.filter_map do |field|
    next unless only.empty? || only.include?(field.name)

    [field.name.to_s, field.serialize(record)]
  end.to_h

  new(index, record.id, field_values)
end

.get(index, document_id) ⇒ Object



18
19
20
# File 'lib/redi_search/document.rb', line 18

def get(index, document_id)
  Finder.new(index, document_id).find
end

Instance Method Details

#delObject



34
35
36
# File 'lib/redi_search/document.rb', line 34

def del
  RediSearch.client.call!("DEL", document_id, skip_ft: true).ok?
end

#document_idObject



50
51
52
53
54
55
56
# File 'lib/redi_search/document.rb', line 50

def document_id
  if @document_id.to_s.start_with? index.name
    @document_id
  else
    "#{index.name}#{@document_id}"
  end
end

#document_id_without_indexObject



58
59
60
61
62
63
64
# File 'lib/redi_search/document.rb', line 58

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_attributesObject



44
45
46
47
48
# File 'lib/redi_search/document.rb', line 44

def redis_attributes
  attributes.flat_map do |field, value|
    [field, index.schema[field.to_sym].coerce(value)]
  end
end

#schema_fieldsObject



38
39
40
41
42
# File 'lib/redi_search/document.rb', line 38

def schema_fields
  @schema_fields ||= index.schema.fields.map do |field|
    field.name.to_s
  end
end