Skip to content

Commit cae7ba7

Browse files
authored
[STORE] Make default doc type '_doc' in preparation for deprecation of mapping types (#816)
* [STORE] Require document_type or method options to set type in find requests * [STORE] Require document_type for a Repository to have a document type set, otherwise use _doc * [STORE] Use document_type or type in options for search requests * [STORE] Require #klass to be defined on a repository for deserialization * [STORE] Only rely on document_type setting in storage requests * [STORE] Fix up tests to work with default document_type and klass on Repository object
1 parent 30aa787 commit cae7ba7

File tree

13 files changed

+62
-301
lines changed

13 files changed

+62
-301
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/repository/find.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ def find(*args)
5757
# @return [true, false]
5858
#
5959
def exists?(id, options={})
60-
type = document_type || (klass ? __get_type_from_class(klass) : ALL)
60+
type = document_type || ALL
6161
client.exists( { index: index_name, type: type, id: id }.merge(options) )
6262
end
6363

6464
# @api private
6565
#
6666
def __find_one(id, options={})
67-
type = document_type || (klass ? __get_type_from_class(klass) : ALL)
67+
type = document_type || ALL
6868
document = client.get( { index: index_name, type: type, id: id }.merge(options) )
6969

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

8181
documents[DOCS].map { |document| document[FOUND] ? deserialize(document) : nil }

elasticsearch-persistence/lib/elasticsearch/persistence/repository/naming.rb

+9-35
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ module Naming
1010
#
1111
IDS = [:id, 'id', :_id, '_id'].freeze
1212

13+
DEFAULT_DOC_TYPE = '_doc'.freeze
14+
1315
# Get or set the class used to initialize domain objects when deserializing them
1416
#
15-
def klass name=nil
16-
@klass = name || @klass
17+
def klass(name=nil)
18+
if name
19+
@klass = name
20+
else
21+
@klass
22+
end
1723
end
1824

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

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

55-
# Get the Ruby class from the Elasticsearch `_type`
56-
#
57-
# @example
58-
# repository.__get_klass_from_type 'note'
59-
# => Note
60-
#
61-
# @return [Class] The class corresponding to the passed type
62-
# @raise [NameError] if the class cannot be found
63-
#
64-
# @api private
65-
#
66-
def __get_klass_from_type(type)
67-
klass = type.classify
68-
klass.constantize
69-
rescue NameError => e
70-
raise NameError, "Attempted to get class '#{klass}' from the '#{type}' type, but no such class can be found."
71-
end
72-
73-
# Get the Elasticsearch `_type` from the Ruby class
74-
#
75-
# @example
76-
# repository.__get_type_from_class Note
77-
# => "note"
78-
#
79-
# @return [String] The type corresponding to the passed class
80-
#
81-
# @api private
82-
#
83-
def __get_type_from_class(klass)
84-
klass.to_s.underscore
85-
end
86-
8761
# Get a document ID from the document (assuming Hash or Hash-like object)
8862
#
8963
# @example

elasticsearch-persistence/lib/elasticsearch/persistence/repository/search.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module Search
4444
# @return [Elasticsearch::Persistence::Repository::Response::Results]
4545
#
4646
def search(query_or_definition, options={})
47-
type = document_type || (klass ? __get_type_from_class(klass) : nil )
47+
type = document_type
4848

4949
case
5050
when query_or_definition.respond_to?(:to_hash)
@@ -79,7 +79,7 @@ def search(query_or_definition, options={})
7979
#
8080
def count(query_or_definition=nil, options={})
8181
query_or_definition ||= { query: { match_all: {} } }
82-
type = document_type || (klass ? __get_type_from_class(klass) : nil )
82+
type = document_type
8383

8484
case
8585
when query_or_definition.respond_to?(:to_hash)

elasticsearch-persistence/lib/elasticsearch/persistence/repository/serialize.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ module Repository
88
#
99
module Serialize
1010

11+
# Error message raised when documents are attempted to be deserialized and no klass is defined for
12+
# the Repository.
13+
#
14+
# @since 6.0.0
15+
NO_CLASS_ERROR_MESSAGE = "No class is defined for deserializing documents. " +
16+
"Please define a 'klass' for the Repository or define a custom " +
17+
"deserialize method.".freeze
18+
1119
# The key for document fields in an Elasticsearch query response.
1220
#
1321
SOURCE = '_source'.freeze
@@ -31,8 +39,8 @@ def serialize(document)
3139
# Use the `klass` property, if defined, otherwise try to get the class from the document's `_type`.
3240
#
3341
def deserialize(document)
34-
_klass = klass || __get_klass_from_type(document[TYPE])
35-
_klass.new document[SOURCE]
42+
raise NameError.new(NO_CLASS_ERROR_MESSAGE) unless klass
43+
klass.new document[SOURCE]
3644
end
3745
end
3846
end

elasticsearch-persistence/lib/elasticsearch/persistence/repository/store.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Store
1717
def save(document, options={})
1818
serialized = serialize(document)
1919
id = __get_id_from_document(serialized)
20-
type = document_type || __get_type_from_class(klass || document.class)
20+
type = document_type
2121
client.index( { index: index_name, type: type, id: id, body: serialized }.merge(options) )
2222
end
2323

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

4949
type = options.delete(:type) || \
5050
(defined?(serialized) && serialized && serialized.delete(:type)) || \
51-
document_type || \
52-
__get_type_from_class(klass)
51+
document_type
5352

5453
if defined?(serialized) && serialized
5554
body = if serialized[:script]
@@ -80,11 +79,11 @@ def update(document, options={})
8079
def delete(document, options={})
8180
if document.is_a?(String) || document.is_a?(Integer)
8281
id = document
83-
type = document_type || __get_type_from_class(klass)
82+
type = document_type
8483
else
8584
serialized = serialize(document)
8685
id = __get_id_from_document(serialized)
87-
type = document_type || __get_type_from_class(klass || document.class)
86+
type = document_type
8887
end
8988
client.delete( { index: index_name, type: type, id: id }.merge(options) )
9089
end

elasticsearch-persistence/test/integration/repository/custom_class_test.rb

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ::MyNotesRepository
3030
include Elasticsearch::Persistence::Repository
3131

3232
klass MyNote
33+
document_type 'my_note'
3334

3435
settings number_of_shards: 1 do
3536
mapping do
@@ -45,6 +46,8 @@ def deserialize(document)
4546
end
4647

4748
@repository = MyNotesRepository.new
49+
@repository.klass = MyNotesRepository.klass
50+
@repository.document_type = MyNotesRepository.document_type
4851

4952
@repository.client.cluster.health wait_for_status: 'yellow'
5053
end

elasticsearch-persistence/test/integration/repository/default_class_test.rb

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def to_hash
1919
context "The default repository class" do
2020
setup do
2121
@repository = Elasticsearch::Persistence::Repository.new
22+
@repository.klass = ::Note
23+
@repository.document_type = 'note'
2224
@repository.client.cluster.health wait_for_status: 'yellow'
2325
end
2426

@@ -105,6 +107,7 @@ def to_hash
105107
end
106108

107109
should "save and find a plain hash" do
110+
@repository.klass = Hash
108111
@repository.save id: 1, title: 'Hash'
109112
result = @repository.find(1)
110113
assert_equal 'Hash', result['_source']['title']

elasticsearch-persistence/test/integration/repository/virtus_model_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def set_id(id)
2525
@repository = Elasticsearch::Persistence::Repository.new do
2626
index :pages
2727
klass Page
28+
document_type 'page'
2829

2930
def deserialize(document)
3031
page = klass.new document['_source']

0 commit comments

Comments
 (0)