Class: SearchFlip::Response
- Inherits:
-
Object
- Object
- SearchFlip::Response
- Extended by:
- Forwardable
- Defined in:
- lib/search_flip/response.rb
Overview
The SearchFlip::Response class wraps a raw SearchFlip response and decorates it with methods for aggregations, hits, records, pagination, etc.
Instance Attribute Summary collapse
-
#criteria ⇒ Object
Returns the value of attribute criteria.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
-
#aggregations(name = nil) ⇒ Hash
Returns a single or all aggregations returned by Elasticsearch, depending on whether or not a name is specified.
-
#current_page ⇒ Fixnum
Returns the current page number, useful for pagination.
-
#first_page? ⇒ Boolean
Returns whether or not the current page is the first page.
-
#hits ⇒ Hash
Returns the hits returned by Elasticsearch.
-
#ids ⇒ Object
Returns the array of ids returned by Elasticsearch for the current result set, ie the ids listed in the hits section of the response.
-
#initialize(criteria, response) ⇒ Response
constructor
private
Initializes a new response object for the provided criteria and raw Elasticsearch response.
-
#last_page? ⇒ Boolean
Returns whether or not the current page is the last page.
-
#next_page ⇒ Fixnum?
Returns the next page number or nil if there is no next page, ie the current page is the last page.
-
#out_of_range? ⇒ Boolean
Returns whether or not the current page is out of range, ie.
-
#previous_page ⇒ Fixnum?
(also: #prev_page)
Returns the previous page number or nil if no previous page exists, ie if the current page is the first page.
-
#raw_response ⇒ Hash
Returns the raw response, ie a hash derived from the Elasticsearch JSON response.
-
#records ⇒ Array
Returns the database records, usually ActiveRecord objects, depending on the ORM you’re using.
-
#results ⇒ Array
Returns the results, ie hits, wrapped in a SearchFlip::Result object which basically is a Hashie::Mash.
-
#scope ⇒ Object
Builds and returns a scope for the array of ids in the current result set returned by Elasticsearch, including the eager load, preload and includes associations, if specified.
-
#scroll_id ⇒ String
Returns the scroll id returned by Elasticsearch, that can be used in the following request to fetch the next batch of records.
-
#suggestions(name = nil) ⇒ Hash, Array
Returns the named sugggetion, if a name is specified or alle suggestions.
-
#took ⇒ Fixnum
Returns the response time in milliseconds of Elasticsearch specified in the took info of the response.
-
#total_count ⇒ Fixnum
(also: #total_entries)
Returns the total number of results.
-
#total_pages ⇒ Fixnum
Returns the number of total pages for the current pagination settings, ie per page/limit settings.
Constructor Details
#initialize(criteria, response) ⇒ Response
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes a new response object for the provided criteria and raw Elasticsearch response.
15 16 17 18 |
# File 'lib/search_flip/response.rb', line 15 def initialize(criteria, response) self.criteria = criteria self.response = response end |
Instance Attribute Details
#criteria ⇒ Object
Returns the value of attribute criteria.
8 9 10 |
# File 'lib/search_flip/response.rb', line 8 def criteria @criteria end |
#response ⇒ Object
Returns the value of attribute response.
8 9 10 |
# File 'lib/search_flip/response.rb', line 8 def response @response end |
Instance Method Details
#aggregations(name = nil) ⇒ Hash
Returns a single or all aggregations returned by Elasticsearch, depending on whether or not a name is specified. If no name is specified, the raw aggregation hash is simply returned. Contrary, if a name is specified, only this aggregation is returned. Moreover, if a name is specified and the aggregation includes a buckets section, a post-processed aggregation hash is returned.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/search_flip/response.rb', line 296 def aggregations(name = nil) return response["aggregations"] || {} unless name @aggregations ||= {} key = name.to_s return @aggregations[key] if @aggregations.key?(key) @aggregations[key] = if response["aggregations"].nil? || response["aggregations"][key].nil? Result.new elsif response["aggregations"][key]["buckets"].is_a?(Array) response["aggregations"][key]["buckets"].each_with_object({}) { |bucket, hash| hash[bucket["key"]] = Result.new(bucket) } elsif response["aggregations"][key]["buckets"].is_a?(Hash) Result.new response["aggregations"][key]["buckets"] else Result.new response["aggregations"][key] end end |
#current_page ⇒ Fixnum
Returns the current page number, useful for pagination.
104 105 106 |
# File 'lib/search_flip/response.rb', line 104 def current_page 1 + (criteria.offset_value_with_default / criteria.limit_value_with_default) end |
#first_page? ⇒ Boolean
Returns whether or not the current page is the first page.
59 60 61 |
# File 'lib/search_flip/response.rb', line 59 def first_page? current_page == 1 end |
#hits ⇒ Hash
Returns the hits returned by Elasticsearch.
200 201 202 |
# File 'lib/search_flip/response.rb', line 200 def hits response["hits"] end |
#ids ⇒ Object
Returns the array of ids returned by Elasticsearch for the current result set, ie the ids listed in the hits section of the response.
261 262 263 |
# File 'lib/search_flip/response.rb', line 261 def ids @ids ||= hits["hits"].map { |hit| hit["_id"] } end |
#last_page? ⇒ Boolean
Returns whether or not the current page is the last page.
75 76 77 |
# File 'lib/search_flip/response.rb', line 75 def last_page? current_page == total_pages end |
#next_page ⇒ Fixnum?
Returns the next page number or nil if there is no next page, ie the current page is the last page.
151 152 153 154 155 156 |
# File 'lib/search_flip/response.rb', line 151 def next_page return nil if current_page >= total_pages return 1 if current_page < 1 current_page + 1 end |
#out_of_range? ⇒ Boolean
Returns whether or not the current page is out of range, ie. smaller than 1 or larger than #total_pages
92 93 94 |
# File 'lib/search_flip/response.rb', line 92 def out_of_range? current_page < 1 || current_page > total_pages end |
#previous_page ⇒ Fixnum? Also known as: prev_page
Returns the previous page number or nil if no previous page exists, ie if the current page is the first page.
133 134 135 136 137 138 |
# File 'lib/search_flip/response.rb', line 133 def previous_page return nil if current_page <= 1 return total_pages if current_page > total_pages current_page - 1 end |
#raw_response ⇒ Hash
Returns the raw response, ie a hash derived from the Elasticsearch JSON response.
29 30 31 |
# File 'lib/search_flip/response.rb', line 29 def raw_response response end |
#records ⇒ Array
Returns the database records, usually ActiveRecord objects, depending on the ORM you’re using. The records are sorted using the order returned by Elasticsearch.
225 226 227 228 229 230 231 |
# File 'lib/search_flip/response.rb', line 225 def records @records ||= begin sort_map = ids.each_with_index.with_object({}) { |(id, index), hash| hash[id.to_s] = index } scope.to_a.sort_by { |record| sort_map[criteria.target.record_id(record).to_s] } end end |
#results ⇒ Array
Returns the results, ie hits, wrapped in a SearchFlip::Result object which basically is a Hashie::Mash. Check out the Hashie docs for further details.
168 169 170 |
# File 'lib/search_flip/response.rb', line 168 def results @results ||= hits["hits"].map { |hit| Result.from_hit(hit) } end |
#scope ⇒ Object
Builds and returns a scope for the array of ids in the current result set returned by Elasticsearch, including the eager load, preload and includes associations, if specified. A scope is eg an ActiveRecord::Relation, depending on the ORM you’re using.
243 244 245 246 247 248 249 250 251 |
# File 'lib/search_flip/response.rb', line 243 def scope res = criteria.target.fetch_records(ids) res = res.includes(*criteria.includes_values) if criteria.includes_values res = res.eager_load(*criteria.eager_load_values) if criteria.eager_load_values res = res.preload(*criteria.preload_values) if criteria.preload_values res end |
#scroll_id ⇒ String
Returns the scroll id returned by Elasticsearch, that can be used in the following request to fetch the next batch of records.
212 213 214 |
# File 'lib/search_flip/response.rb', line 212 def scroll_id response["_scroll_id"] end |
#suggestions(name = nil) ⇒ Hash, Array
Returns the named sugggetion, if a name is specified or alle suggestions.
184 185 186 187 188 189 190 |
# File 'lib/search_flip/response.rb', line 184 def suggestions(name = nil) if name response["suggest"][name.to_s].first["options"] else response["suggest"] end end |
#took ⇒ Fixnum
Returns the response time in milliseconds of Elasticsearch specified in the took info of the response.
275 276 277 |
# File 'lib/search_flip/response.rb', line 275 def took response["took"] end |
#total_count ⇒ Fixnum Also known as: total_entries
Returns the total number of results.
41 42 43 |
# File 'lib/search_flip/response.rb', line 41 def total_count hits["total"].is_a?(Hash) ? hits["total"]["value"] : hits["total"] end |
#total_pages ⇒ Fixnum
Returns the number of total pages for the current pagination settings, ie per page/limit settings.
117 118 119 |
# File 'lib/search_flip/response.rb', line 117 def total_pages [(total_count.to_f / criteria.limit_value_with_default).ceil, 1].max end |