Class: Searchkick::Results
- Inherits:
-
Object
- Object
- Searchkick::Results
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/searchkick/results.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #aggregations ⇒ Object
- #aggs ⇒ Object
- #clear_scroll ⇒ Object
- #current_page ⇒ Object
- #entry_name(options = {}) ⇒ Object
- #error ⇒ Object
- #first_page? ⇒ Boolean
- #highlights(multiple: false) ⇒ Object
- #hits ⇒ Object
-
#initialize(klass, response, options = {}) ⇒ Results
constructor
A new instance of Results.
- #last_page? ⇒ Boolean
- #missing_records ⇒ Object
- #misspellings? ⇒ Boolean
- #model_name ⇒ Object
- #next_page ⇒ Object
- #offset_value ⇒ Object (also: #offset)
- #out_of_range? ⇒ Boolean
- #padding ⇒ Object
- #per_page ⇒ Object (also: #limit_value)
- #previous_page ⇒ Object (also: #prev_page)
- #scroll ⇒ Object
- #scroll_id ⇒ Object
- #suggestions ⇒ Object
- #took ⇒ Object
- #total_count ⇒ Object (also: #total_entries)
- #total_pages ⇒ Object (also: #num_pages)
- #with_highlights(multiple: false) ⇒ Object
- #with_hit ⇒ Object
- #with_score ⇒ Object
Constructor Details
#initialize(klass, response, options = {}) ⇒ Results
Returns a new instance of Results.
10 11 12 13 14 |
# File 'lib/searchkick/results.rb', line 10 def initialize(klass, response, = {}) @klass = klass @response = response @options = end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
6 7 8 |
# File 'lib/searchkick/results.rb', line 6 def response @response end |
Instance Method Details
#aggregations ⇒ Object
38 39 40 |
# File 'lib/searchkick/results.rb', line 38 def aggregations response["aggregations"] end |
#aggs ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/searchkick/results.rb', line 42 def aggs @aggs ||= begin if aggregations aggregations.dup.each do |field, filtered_agg| buckets = filtered_agg[field] # move the buckets one level above into the field hash if buckets filtered_agg.delete(field) filtered_agg.merge!(buckets) end end end end end |
#clear_scroll ⇒ Object
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/searchkick/results.rb', line 206 def clear_scroll begin # try to clear scroll # not required as scroll will expire # but there is a cost to open scrolls Searchkick.client.clear_scroll(scroll_id: scroll_id) rescue => e raise e unless Searchkick.transport_error?(e) end end |
#current_page ⇒ Object
94 95 96 |
# File 'lib/searchkick/results.rb', line 94 def current_page [:page] end |
#entry_name(options = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/searchkick/results.rb', line 73 def entry_name( = {}) if .empty? # backward compatibility model_name.human.downcase else default = [:count] == 1 ? model_name.human : model_name.human.pluralize model_name.human(.reverse_merge(default: default)) end end |
#error ⇒ Object
61 62 63 |
# File 'lib/searchkick/results.rb', line 61 def error response["error"] end |
#first_page? ⇒ Boolean
126 127 128 |
# File 'lib/searchkick/results.rb', line 126 def first_page? previous_page.nil? end |
#highlights(multiple: false) ⇒ Object
146 147 148 149 150 |
# File 'lib/searchkick/results.rb', line 146 def highlights(multiple: false) hits.map do |hit| hit_highlights(hit, multiple: multiple) end end |
#hits ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/searchkick/results.rb', line 138 def hits if error raise Error, "Query error - use the error method to view it" else @response["hits"]["hits"] end end |
#last_page? ⇒ Boolean
130 131 132 |
# File 'lib/searchkick/results.rb', line 130 def last_page? next_page.nil? end |
#missing_records ⇒ Object
24 25 26 |
# File 'lib/searchkick/results.rb', line 24 def missing_records @missing_records ||= with_hit_and_missing_records[1] end |
#misspellings? ⇒ Boolean
168 169 170 |
# File 'lib/searchkick/results.rb', line 168 def misspellings? @options[:misspellings] end |
#model_name ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/searchkick/results.rb', line 65 def model_name if klass.nil? ActiveModel::Name.new(self.class, nil, 'Result') else klass.model_name end end |
#next_page ⇒ Object
122 123 124 |
# File 'lib/searchkick/results.rb', line 122 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset_value ⇒ Object Also known as: offset
112 113 114 |
# File 'lib/searchkick/results.rb', line 112 def offset_value (current_page - 1) * per_page + padding end |
#out_of_range? ⇒ Boolean
134 135 136 |
# File 'lib/searchkick/results.rb', line 134 def out_of_range? current_page > total_pages end |
#padding ⇒ Object
103 104 105 |
# File 'lib/searchkick/results.rb', line 103 def padding [:padding] end |
#per_page ⇒ Object Also known as: limit_value
98 99 100 |
# File 'lib/searchkick/results.rb', line 98 def per_page [:per_page] end |
#previous_page ⇒ Object Also known as: prev_page
117 118 119 |
# File 'lib/searchkick/results.rb', line 117 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#scroll ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/searchkick/results.rb', line 176 def scroll raise Error, "Pass `scroll` option to the search method for scrolling" unless scroll_id if block_given? records = self while records.any? yield records records = records.scroll end records.clear_scroll else begin # TODO Active Support notifications for this scroll call params = { scroll: [:scroll], body: {scroll_id: scroll_id} } params[:opaque_id] = [:opaque_id] if [:opaque_id] Results.new(@klass, Searchkick.client.scroll(params), @options) rescue => e if Searchkick.not_found_error?(e) && e. =~ /search_context_missing_exception/i raise Error, "Scroll id has expired" else raise e end end end end |
#scroll_id ⇒ Object
172 173 174 |
# File 'lib/searchkick/results.rb', line 172 def scroll_id @response["_scroll_id"] end |
#suggestions ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/searchkick/results.rb', line 28 def suggestions if response["suggest"] response["suggest"].values.flat_map { |v| v.first["options"] }.sort_by { |o| -o["score"] }.map { |o| o["text"] }.uniq elsif [:suggest] [] else raise "Pass `suggest: true` to the search method for suggestions" end end |
#took ⇒ Object
57 58 59 |
# File 'lib/searchkick/results.rb', line 57 def took response["took"] end |
#total_count ⇒ Object Also known as: total_entries
83 84 85 86 87 88 89 90 91 |
# File 'lib/searchkick/results.rb', line 83 def total_count if [:total_entries] [:total_entries] elsif response["hits"]["total"].is_a?(Hash) response["hits"]["total"]["value"] else response["hits"]["total"] end end |
#total_pages ⇒ Object Also known as: num_pages
107 108 109 |
# File 'lib/searchkick/results.rb', line 107 def total_pages (total_count / per_page.to_f).ceil end |
#with_highlights(multiple: false) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/searchkick/results.rb', line 152 def with_highlights(multiple: false) return enum_for(:with_highlights, multiple: multiple) unless block_given? with_hit.each do |result, hit| yield result, hit_highlights(hit, multiple: multiple) end end |
#with_hit ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/searchkick/results.rb', line 16 def with_hit return enum_for(:with_hit) unless block_given? build_hits.each do |result| yield result end end |
#with_score ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/searchkick/results.rb', line 160 def with_score return enum_for(:with_score) unless block_given? with_hit.each do |result, hit| yield result, hit["_score"] end end |