Skip to content

Commit c3f3601

Browse files
committed
[STORE] Updated the count method in the "repository" module
Since the `search_type: 'count'` parameter has been deprecated in Elasticsearch 2 and removed in Elasticsearch 5, the `Elasticsearch::Persistence::Model::Find::ClassMethods#count` method has been updated to use the "Count" API, instead of the "Search" API with the deprecated parameter. See: https://www.elastic.co/guide/en/elasticsearch/reference/2.1/breaking_21_search_changes.html#_literal_search_type_count_literal_deprecated
1 parent e81fc5b commit c3f3601

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,18 @@ def search(query_or_definition, options={})
7575
#
7676
def count(query_or_definition=nil, options={})
7777
query_or_definition ||= { query: { match_all: {} } }
78-
response = search query_or_definition, options.update(search_type: 'count')
79-
response.response.hits.total
78+
type = document_type || (klass ? __get_type_from_class(klass) : nil )
79+
80+
case
81+
when query_or_definition.respond_to?(:to_hash)
82+
response = client.count( { index: index_name, type: type, body: query_or_definition.to_hash }.merge(options) )
83+
when query_or_definition.is_a?(String)
84+
response = client.count( { index: index_name, type: type, q: query_or_definition }.merge(options) )
85+
else
86+
raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String, not as [#{query_or_definition.class}]"
87+
end
88+
89+
response['count']
8090
end
8191
end
8292

elasticsearch-persistence/test/unit/repository_search_test.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,21 @@ class MyDocument; end
9696
end
9797

9898
should "return the number of domain objects" do
99-
subject.expects(:search)
100-
.returns(Elasticsearch::Persistence::Repository::Response::Results.new( subject, {'hits' => { 'total' => 1 }}))
99+
subject.client.expects(:count).returns({'count' => 1})
101100
assert_equal 1, subject.count
102101
end
103102

104-
should "pass arguments from count to search" do
105-
subject.expects(:search)
106-
.with do |query_or_definition, options|
107-
assert_equal 'bar', query_or_definition[:query][:match][:foo]
108-
assert_equal true, options[:ignore_unavailable]
103+
should "pass arguments to count" do
104+
subject.client.expects(:count)
105+
.with do |arguments|
106+
assert_equal 'test', arguments[:index]
107+
assert_equal 'bar', arguments[:body][:query][:match][:foo]
108+
assert_equal true, arguments[:ignore_unavailable]
109109
true
110110
end
111-
.returns(Elasticsearch::Persistence::Repository::Response::Results.new( subject, {'hits' => { 'total' => 1 }}))
111+
.returns({'count' => 1})
112112

113-
subject.count( { query: { match: { foo: 'bar' } } }, { ignore_unavailable: true } )
113+
assert_equal 1, subject.count( { query: { match: { foo: 'bar' } } }, { ignore_unavailable: true } )
114114
end
115115
end
116116

0 commit comments

Comments
 (0)