Skip to content

[STORE] Make default doc type '_doc' in preparation for deprecation of mapping types #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 23, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ def find(*args)
# @return [true, false]
#
def exists?(id, options={})
type = document_type || (klass ? __get_type_from_class(klass) : ALL)
type = document_type || ALL
client.exists( { index: index_name, type: type, id: id }.merge(options) )
end

# @api private
#
def __find_one(id, options={})
type = document_type || (klass ? __get_type_from_class(klass) : ALL)
type = document_type || ALL
document = client.get( { index: index_name, type: type, id: id }.merge(options) )

deserialize(document)
Expand All @@ -75,7 +75,7 @@ def __find_one(id, options={})
# @api private
#
def __find_many(ids, options={})
type = document_type || (klass ? __get_type_from_class(klass) : ALL)
type = document_type || ALL
documents = client.mget( { index: index_name, type: type, body: { ids: ids } }.merge(options) )

documents[DOCS].map { |document| document[FOUND] ? deserialize(document) : nil }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ module Naming
#
IDS = [:id, 'id', :_id, '_id'].freeze

DEFAULT_DOC_TYPE = '_doc'.freeze

# Get or set the class used to initialize domain objects when deserializing them
#
def klass name=nil
@klass = name || @klass
def klass(name=nil)
if name
@klass = name
else
@klass
end
end

# Set the class used to initialize domain objects when deserializing them
Expand Down Expand Up @@ -43,7 +49,7 @@ def index_name=(name)
# Get or set the document type used when storing and retrieving documents
#
def document_type name=nil
@document_type = name || @document_type || (klass ? klass.to_s.underscore : nil)
@document_type = name || @document_type || DEFAULT_DOC_TYPE
end; alias :type :document_type

# Set the document type used when storing and retrieving documents
Expand All @@ -52,38 +58,6 @@ def document_type=(name)
@document_type = name
end; alias :type= :document_type=

# Get the Ruby class from the Elasticsearch `_type`
#
# @example
# repository.__get_klass_from_type 'note'
# => Note
#
# @return [Class] The class corresponding to the passed type
# @raise [NameError] if the class cannot be found
#
# @api private
#
def __get_klass_from_type(type)
klass = type.classify
klass.constantize
rescue NameError => e
raise NameError, "Attempted to get class '#{klass}' from the '#{type}' type, but no such class can be found."
end

# Get the Elasticsearch `_type` from the Ruby class
#
# @example
# repository.__get_type_from_class Note
# => "note"
#
# @return [String] The type corresponding to the passed class
#
# @api private
#
def __get_type_from_class(klass)
klass.to_s.underscore
end

# Get a document ID from the document (assuming Hash or Hash-like object)
#
# @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module Search
# @return [Elasticsearch::Persistence::Repository::Response::Results]
#
def search(query_or_definition, options={})
type = document_type || (klass ? __get_type_from_class(klass) : nil )
type = document_type

case
when query_or_definition.respond_to?(:to_hash)
Expand Down Expand Up @@ -79,7 +79,7 @@ def search(query_or_definition, options={})
#
def count(query_or_definition=nil, options={})
query_or_definition ||= { query: { match_all: {} } }
type = document_type || (klass ? __get_type_from_class(klass) : nil )
type = document_type

case
when query_or_definition.respond_to?(:to_hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ module Repository
#
module Serialize

# Error message raised when documents are attempted to be deserialized and no klass is defined for
# the Repository.
#
# @since 6.0.0
NO_CLASS_ERROR_MESSAGE = "No class is defined for deserializing documents. " +
"Please define a 'klass' for the Repository or define a custom " +
"deserialize method.".freeze

# The key for document fields in an Elasticsearch query response.
#
SOURCE = '_source'.freeze
Expand All @@ -31,8 +39,8 @@ def serialize(document)
# Use the `klass` property, if defined, otherwise try to get the class from the document's `_type`.
#
def deserialize(document)
_klass = klass || __get_klass_from_type(document[TYPE])
_klass.new document[SOURCE]
raise NameError.new(NO_CLASS_ERROR_MESSAGE) unless klass
klass.new document[SOURCE]
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Store
def save(document, options={})
serialized = serialize(document)
id = __get_id_from_document(serialized)
type = document_type || __get_type_from_class(klass || document.class)
type = document_type
client.index( { index: index_name, type: type, id: id, body: serialized }.merge(options) )
end

Expand Down Expand Up @@ -48,8 +48,7 @@ def update(document, options={})

type = options.delete(:type) || \
(defined?(serialized) && serialized && serialized.delete(:type)) || \
document_type || \
__get_type_from_class(klass)
document_type

if defined?(serialized) && serialized
body = if serialized[:script]
Expand Down Expand Up @@ -80,11 +79,11 @@ def update(document, options={})
def delete(document, options={})
if document.is_a?(String) || document.is_a?(Integer)
id = document
type = document_type || __get_type_from_class(klass)
type = document_type
else
serialized = serialize(document)
id = __get_id_from_document(serialized)
type = document_type || __get_type_from_class(klass || document.class)
type = document_type
end
client.delete( { index: index_name, type: type, id: id }.merge(options) )
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ::MyNotesRepository
include Elasticsearch::Persistence::Repository

klass MyNote
document_type 'my_note'

settings number_of_shards: 1 do
mapping do
Expand All @@ -45,6 +46,8 @@ def deserialize(document)
end

@repository = MyNotesRepository.new
@repository.klass = MyNotesRepository.klass
@repository.document_type = MyNotesRepository.document_type

@repository.client.cluster.health wait_for_status: 'yellow'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def to_hash
context "The default repository class" do
setup do
@repository = Elasticsearch::Persistence::Repository.new
@repository.klass = ::Note
@repository.document_type = 'note'
@repository.client.cluster.health wait_for_status: 'yellow'
end

Expand Down Expand Up @@ -105,6 +107,7 @@ def to_hash
end

should "save and find a plain hash" do
@repository.klass = Hash
@repository.save id: 1, title: 'Hash'
result = @repository.find(1)
assert_equal 'Hash', result['_source']['title']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def set_id(id)
@repository = Elasticsearch::Persistence::Repository.new do
index :pages
klass Page
document_type 'page'

def deserialize(document)
page = klass.new document['_source']
Expand Down
Loading