@@ -7,6 +7,20 @@ class DocumentNotFound < StandardError; end
7
7
#
8
8
module Find
9
9
10
+ # The default type of document.
11
+ #
12
+ ALL = '_all' . freeze
13
+
14
+ # The key for accessing the document found and returned from an
15
+ # Elasticsearch _mget query.
16
+ #
17
+ DOCS = 'docs' . freeze
18
+
19
+ # The key for the boolean value indicating whether a particular id
20
+ # has been successfully found in an Elasticsearch _mget query.
21
+ #
22
+ FOUND = 'found' . freeze
23
+
10
24
# Retrieve a single object or multiple objects from Elasticsearch by ID or IDs
11
25
#
12
26
# @example Retrieve a single object by ID
@@ -43,14 +57,14 @@ def find(*args)
43
57
# @return [true, false]
44
58
#
45
59
def exists? ( id , options = { } )
46
- type = document_type || ( klass ? __get_type_from_class ( klass ) : '_all' )
60
+ type = document_type || ( klass ? __get_type_from_class ( klass ) : ALL )
47
61
client . exists ( { index : index_name , type : type , id : id } . merge ( options ) )
48
62
end
49
63
50
64
# @api private
51
65
#
52
66
def __find_one ( id , options = { } )
53
- type = document_type || ( klass ? __get_type_from_class ( klass ) : '_all' )
67
+ type = document_type || ( klass ? __get_type_from_class ( klass ) : ALL )
54
68
document = client . get ( { index : index_name , type : type , id : id } . merge ( options ) )
55
69
56
70
deserialize ( document )
@@ -61,10 +75,10 @@ def __find_one(id, options={})
61
75
# @api private
62
76
#
63
77
def __find_many ( ids , options = { } )
64
- type = document_type || ( klass ? __get_type_from_class ( klass ) : '_all' )
78
+ type = document_type || ( klass ? __get_type_from_class ( klass ) : ALL )
65
79
documents = client . mget ( { index : index_name , type : type , body : { ids : ids } } . merge ( options ) )
66
80
67
- documents [ 'docs' ] . map { |document | document [ 'found' ] ? deserialize ( document ) : nil }
81
+ documents [ DOCS ] . map { |document | document [ FOUND ] ? deserialize ( document ) : nil }
68
82
end
69
83
end
70
84
0 commit comments