Module: Elasticsearch::Persistence::Repository::Serialize

Included in:
Class
Defined in:
lib/elasticsearch/persistence/repository/serialize.rb

Overview

Provide serialization and deserialization between Ruby objects and Elasticsearch documents

Override these methods in your repository class to customize the logic.

Instance Method Summary collapse

Instance Method Details

#deserialize(document) ⇒ Object

Deserialize the document retrieved from Elasticsearch into a Ruby object

Use the ‘klass` property, if defined, otherwise try to get the class from the document’s ‘_type`.



23
24
25
26
# File 'lib/elasticsearch/persistence/repository/serialize.rb', line 23

def deserialize(document)
  _klass = klass || __get_klass_from_type(document['_type'])
  _klass.new document['_source'] || document['fields']
end

#serialize(document) ⇒ Object

Serialize the object for storing it in Elasticsearch

In the default implementation, call the ‘to_hash` method on the passed object.



15
16
17
# File 'lib/elasticsearch/persistence/repository/serialize.rb', line 15

def serialize(document)
  document.to_hash
end