Skip to content

Commit 1c43a53

Browse files
delwatermankarmi
authored andcommitted
[MODEL] Added a convenience accessor for the aggregations part of response
Closes elastic#376
1 parent 8508577 commit 1c43a53

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

elasticsearch-model/lib/elasticsearch/model/response.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ def timed_out
6565
def shards
6666
Hashie::Mash.new(response['_shards'])
6767
end
68+
69+
# Returns a Hashie::Mash of the aggregations
70+
#
71+
def aggregations
72+
response['aggregations'] ? Hashie::Mash.new(response['aggregations']) : nil
73+
end
6874
end
6975
end
7076
end

elasticsearch-model/test/unit/response_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ def self.index_name; 'foo'; end
77
def self.document_type; 'bar'; end
88
end
99

10-
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, 'hits' => { 'hits' => [] } }
10+
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, 'hits' => { 'hits' => [] },
11+
'aggregations' => {'foo' => {'bar' => 10}}}
1112

1213
setup do
1314
@search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
@@ -63,5 +64,14 @@ def self.document_type; 'bar'; end
6364

6465
Elasticsearch::Model::Response::Response.new OriginClass, @search
6566
end
67+
68+
should "access the aggregations" do
69+
@search.expects(:execute!).returns(RESPONSE)
70+
71+
response = Elasticsearch::Model::Response::Response.new OriginClass, @search
72+
assert_respond_to response, :aggregations
73+
assert_kind_of Hashie::Mash, response.aggregations.foo
74+
assert_equal 10, response.aggregations.foo.bar
75+
end
6676
end
6777
end

0 commit comments

Comments
 (0)