@@ -43,18 +43,18 @@ module Search
43
43
# @return [Elasticsearch::Persistence::Repository::Response::Results]
44
44
#
45
45
def search ( query_or_definition , options = { } )
46
- type = document_type
47
-
48
- case
49
- when query_or_definition . respond_to? ( :to_hash )
50
- response = client . search ( { index : index_name , type : type , body : query_or_definition . to_hash } . merge ( options ) )
51
- when query_or_definition . is_a? ( String )
52
- response = client . search ( { index : index_name , type : type , q : query_or_definition } . merge ( options ) )
46
+ request = { index : index_name ,
47
+ type : document_type }
48
+ if query_or_definition . respond_to? ( :to_hash )
49
+ request [ :body ] = query_or_definition . to_hash
50
+ elsif query_or_definition . is_a? ( String )
51
+ request [ :q ] = query_or_definition
53
52
else
54
53
raise ArgumentError , "[!] Pass the search definition as a Hash-like object or pass the query as a String" +
55
- " -- #{ query_or_definition . class } given."
54
+ " -- #{ query_or_definition . class } given."
56
55
end
57
- Response ::Results . new ( self , response )
56
+
57
+ Response ::Results . new ( self , client . search ( request . merge ( options ) ) )
58
58
end
59
59
60
60
# Return the number of domain object in the index
@@ -81,18 +81,19 @@ def search(query_or_definition, options={})
81
81
#
82
82
def count ( query_or_definition = nil , options = { } )
83
83
query_or_definition ||= { query : { match_all : { } } }
84
- type = document_type
84
+ request = { index : index_name ,
85
+ type : document_type }
85
86
86
- case
87
- when query_or_definition . respond_to? ( :to_hash )
88
- response = client . count ( { index : index_name , type : type , body : query_or_definition . to_hash } . merge ( options ) )
89
- when query_or_definition . is_a? ( String )
90
- response = client . count ( { index : index_name , type : type , q : query_or_definition } . merge ( options ) )
87
+ if query_or_definition . respond_to? ( :to_hash )
88
+ request [ :body ] = query_or_definition . to_hash
89
+ elsif query_or_definition . is_a? ( String )
90
+ request [ :q ] = query_or_definition
91
91
else
92
- raise ArgumentError , "[!] Pass the search definition as a Hash-like object or pass the query as a String, not as [#{ query_or_definition . class } ]"
92
+ raise ArgumentError , "[!] Pass the search definition as a Hash-like object or pass the query as a String" +
93
+ " -- #{ query_or_definition . class } given."
93
94
end
94
95
95
- response [ COUNT ]
96
+ client . count ( request . merge ( options ) ) [ COUNT ]
96
97
end
97
98
98
99
private
0 commit comments