module Elasticsearch::Persistence::Repository::Serialize

Provide serialization and deserialization between Ruby objects and Elasticsearch documents.

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

Constants

IDS
SOURCE

The key for document fields in an Elasticsearch query response.

TYPE

The key for the document type in an Elasticsearch query response.

Note that it will be removed eventually, as multiple types in a single
index are deprecated as of Elasticsearch 6.0.

Public Instance Methods

deserialize(document) click to toggle source

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

@param [ Hash ] document The raw document.

@return [ Object ] The deserialized object.

# File lib/elasticsearch/persistence/repository/serialize.rb, line 51
def deserialize(document)
  klass ? klass.new(document[SOURCE]) : document[SOURCE]
end
serialize(document) click to toggle source

Serialize the object for storing it in Elasticsearch.

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

@param [ Object ] document The Ruby object to serialize.

@return [ Hash ] The serialized document.

# File lib/elasticsearch/persistence/repository/serialize.rb, line 36
def serialize(document)
  document.to_hash
end

Private Instance Methods

__extract_id_from_document(document) click to toggle source

Extract a document ID from the document (assuming Hash or Hash-like object)

@note Calling this method will remove the ‘id` or `_id` key from the passed object.

@example

options = { title: 'Test', id: 'abc123' }
repository.__extract_id_from_document options
# => "abc123"
options
# => { title: 'Test' }

@api private

# File lib/elasticsearch/persistence/repository/serialize.rb, line 94
def __extract_id_from_document(document)
  IDS.inject(nil) do |deleted, id|
    if document[id]
      document.delete(id)
    else
      deleted
    end
  end
end
__get_id_from_document(document) click to toggle source

Get a document ID from the document (assuming Hash or Hash-like object)

@example

repository.__get_id_from_document title: 'Test', id: 'abc123'
=> "abc123"

@api private

# File lib/elasticsearch/persistence/repository/serialize.rb, line 77
def __get_id_from_document(document)
  document[IDS.find { |id| document[id] }]
end