forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.rb
47 lines (39 loc) · 1.28 KB
/
response.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Elasticsearch
module Model
# Contains modules and classes for wrapping the response from Elasticsearch
#
module Response
# Encapsulate the response returned from the Elasticsearch client
#
# Implements Enumerable and forwards its methods to the {#results} object.
#
class Response
attr_reader :klass, :search, :response,
:took, :timed_out, :shards
include Enumerable
extend Support::Forwardable
forward :results, :each, :empty?, :size, :slice, :[], :to_ary
def initialize(klass, search, response)
@klass = klass
@search = search
@response = response
@took = response['took']
@timed_out = response['timed_out']
@shards = Hashie::Mash.new(response['_shards'])
end
# Return the collection of "hits" from Elasticsearch
#
def results
@response ||= search.execute!
@results ||= Results.new(klass, response, nil, self)
end
# Return the collection of records from the database
#
def records
@response ||= search.execute!
@records ||= Records.new(klass, response, results, self)
end
end
end
end
end