Class: Chewy::Search::Response
- Inherits:
-
Object
- Object
- Chewy::Search::Response
- Defined in:
- lib/chewy/search/response.rb
Overview
This class is a ES response hash wrapper.
Instance Method Summary collapse
-
#aggs ⇒ Hash
(also: #aggregations)
The
aggregations
response part. -
#hits ⇒ Array<Hash>
Raw response
hits
collection. -
#initialize(body, loader, paginator = nil) ⇒ Response
constructor
A new instance of Response.
-
#max_score ⇒ Float
Response
max_score
field. -
#object_hash ⇒ {Chewy::Index => Object}
(also: #record_hash, #document_hash)
This method is used in cases when you need to iterate through both of the collections simultaneously.
-
#objects ⇒ Array<Object>
(also: #records, #documents)
ORM/ODM objects that had been a source for Chewy import and now loaded from the DB using hits ids.
-
#suggest ⇒ Hash
The
suggest
response part. -
#terminated_early? ⇒ true, false
Has the request been terminated early?.
-
#timed_out? ⇒ true, false
Has the request been timed out?.
-
#took ⇒ Integer
Duration of the request handling in ms according to ES.
-
#total ⇒ Integer
Response
total
field. -
#wrappers ⇒ Array<Chewy::Index>
Index wrappers collection instantiated on top of hits.
Constructor Details
#initialize(body, loader, paginator = nil) ⇒ Response
Returns a new instance of Response.
9 10 11 12 13 |
# File 'lib/chewy/search/response.rb', line 9 def initialize(body, loader, paginator = nil) @body = body @loader = loader @paginator = paginator end |
Instance Method Details
#aggs ⇒ Hash Also known as: aggregations
The aggregations
response part. Returns empty hash if aggregations
were not requested.
69 70 71 |
# File 'lib/chewy/search/response.rb', line 69 def aggs @aggs ||= @body['aggregations'] || {} end |
#hits ⇒ Array<Hash>
Raw response hits
collection. Returns empty array is something went wrong.
18 19 20 |
# File 'lib/chewy/search/response.rb', line 18 def hits @hits ||= hits_root['hits'] || [] end |
#max_score ⇒ Float
Response max_score
field.
32 33 34 |
# File 'lib/chewy/search/response.rb', line 32 def max_score @max_score ||= hits_root['max_score'] end |
#object_hash ⇒ {Chewy::Index => Object} Also known as: record_hash, document_hash
This method is used in cases when you need to iterate through both of the collections simultaneously.
113 114 115 |
# File 'lib/chewy/search/response.rb', line 113 def object_hash @object_hash ||= wrappers.zip(objects).to_h end |
#objects ⇒ Array<Object> Also known as: records, documents
ORM/ODM objects that had been a source for Chewy import and now loaded from the DB using hits ids. Uses Chewy::Search::Request#load passed options for loading.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/chewy/search/response.rb', line 90 def objects @objects ||= begin objects = @loader.load(hits) if @paginator @paginator.call(objects) else objects end end end |
#suggest ⇒ Hash
The suggest
response part. Returns empty hash if suggests
were not requested.
61 62 63 |
# File 'lib/chewy/search/response.rb', line 61 def suggest @suggest ||= @body['suggest'] || {} end |
#terminated_early? ⇒ true, false
Has the request been terminated early?
53 54 55 |
# File 'lib/chewy/search/response.rb', line 53 def terminated_early? @terminated_early ||= @body['terminated_early'] end |
#timed_out? ⇒ true, false
Has the request been timed out?
46 47 48 |
# File 'lib/chewy/search/response.rb', line 46 def timed_out? @timed_out ||= @body['timed_out'] end |
#took ⇒ Integer
Duration of the request handling in ms according to ES.
39 40 41 |
# File 'lib/chewy/search/response.rb', line 39 def took @took ||= @body['took'] end |
#total ⇒ Integer
Response total
field. Returns 0
if something went wrong.
25 26 27 |
# File 'lib/chewy/search/response.rb', line 25 def total @total ||= hits_root.fetch('total', {}).fetch('value', 0) end |
#wrappers ⇒ Array<Chewy::Index>
Index wrappers collection instantiated on top of hits.
77 78 79 80 81 |
# File 'lib/chewy/search/response.rb', line 77 def wrappers @wrappers ||= hits.map do |hit| @loader.derive_index(hit['_index']).build(hit) end end |