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

Included in:
Elasticsearch::Persistence::Repository
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.

Since:

  • 6.0.0

Instance Method Summary collapse

Instance Method Details

#deserialize(document) ⇒ Object

Deserialize the document retrieved from Elasticsearch into a Ruby object. If no klass is set for the Repository then the raw document ‘_source’ field will be returned.

def deserialize(document)

Note.new document[SOURCE]

end

Parameters:

  • document (Hash)

    The raw document.

Returns:

  • (Object)

    The deserialized object.

Since:

  • 6.0.0



51
52
53
# File 'lib/elasticsearch/persistence/repository/serialize.rb', line 51

def deserialize(document)
  klass ? klass.new(document[SOURCE]) : document[SOURCE]
end

#serialize(document) ⇒ Hash

Serialize the object for storing it in Elasticsearch.

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

Parameters:

  • document (Object)

    The Ruby object to serialize.

Returns:

  • (Hash)

    The serialized document.

Since:

  • 6.0.0



36
37
38
# File 'lib/elasticsearch/persistence/repository/serialize.rb', line 36

def serialize(document)
  document.to_hash
end