Module: ElasticRecord::AsDocument

Defined in:
lib/elastic_record/as_document.rb

Instance Method Summary collapse

Instance Method Details

#as_partial_update_documentObject



13
14
15
16
17
18
19
20
21
# File 'lib/elastic_record/as_document.rb', line 13

def as_partial_update_document
  mappings = doctype.mapping[:properties]

  changed.each_with_object({}) do |field, result|
    if field_mapping = mappings[field]
      result[field] = elastic_search_value field, field_mapping
    end
  end
end

#as_search_documentObject



3
4
5
6
7
8
9
10
11
# File 'lib/elastic_record/as_document.rb', line 3

def as_search_document
  doctype.mapping[:properties].each_with_object({}) do |(field, mapping), result|
    value = elastic_search_value field, mapping

    unless value.nil?
      result[field] = value
    end
  end
end

#elastic_search_value(field, mapping) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/elastic_record/as_document.rb', line 23

def elastic_search_value(field, mapping)
  value = try field
  return if value.nil?

  value = case mapping[:type]
          when :object
            value.as_search_document
          when :nested
            value.map(&:as_search_document)
          else
            value
          end

  if value.present? || value == false
    value
  end
end