Skip to content

Commit a95513e

Browse files
committed
[STORE] Minor refactor in Repository::Search
1 parent 391ddd1 commit a95513e

File tree

1 file changed

+18
-17
lines changed
  • elasticsearch-persistence/lib/elasticsearch/persistence/repository

1 file changed

+18
-17
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ module Search
4343
# @return [Elasticsearch::Persistence::Repository::Response::Results]
4444
#
4545
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
5352
else
5453
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."
5655
end
57-
Response::Results.new(self, response)
56+
57+
Response::Results.new(self, client.search(request.merge(options)))
5858
end
5959

6060
# Return the number of domain object in the index
@@ -81,18 +81,19 @@ def search(query_or_definition, options={})
8181
#
8282
def count(query_or_definition=nil, options={})
8383
query_or_definition ||= { query: { match_all: {} } }
84-
type = document_type
84+
request = { index: index_name,
85+
type: document_type }
8586

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
9191
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."
9394
end
9495

95-
response[COUNT]
96+
client.count(request.merge(options))[COUNT]
9697
end
9798

9899
private

0 commit comments

Comments
 (0)