Class: Searchkick::Results
- Inherits:
-
Object
- Object
- Searchkick::Results
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/searchkick/results.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #aggregations ⇒ Object
- #aggs ⇒ 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
- #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)
- #results ⇒ 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
Constructor Details
#initialize(klass, response, options = {}) ⇒ Results
Returns a new instance of Results.
12 13 14 15 16 |
# File 'lib/searchkick/results.rb', line 12 def initialize(klass, response, = {}) @klass = klass @response = response @options = end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
8 9 10 |
# File 'lib/searchkick/results.rb', line 8 def klass @klass end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/searchkick/results.rb', line 8 def @options end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
8 9 10 |
# File 'lib/searchkick/results.rb', line 8 def response @response end |
Instance Method Details
#aggregations ⇒ Object
93 94 95 |
# File 'lib/searchkick/results.rb', line 93 def aggregations response["aggregations"] end |
#aggs ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/searchkick/results.rb', line 97 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 |
#current_page ⇒ Object
139 140 141 |
# File 'lib/searchkick/results.rb', line 139 def current_page [:page] end |
#entry_name(options = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/searchkick/results.rb', line 124 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
116 117 118 |
# File 'lib/searchkick/results.rb', line 116 def error response["error"] end |
#first_page? ⇒ Boolean
171 172 173 |
# File 'lib/searchkick/results.rb', line 171 def first_page? previous_page.nil? end |
#highlights(multiple: false) ⇒ Object
191 192 193 194 195 |
# File 'lib/searchkick/results.rb', line 191 def highlights(multiple: false) hits.map do |hit| hit_highlights(hit, multiple: multiple) end end |
#hits ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/searchkick/results.rb', line 183 def hits if error raise Searchkick::Error, "Query error - use the error method to view it" else @response["hits"]["hits"] end end |
#last_page? ⇒ Boolean
175 176 177 |
# File 'lib/searchkick/results.rb', line 175 def last_page? next_page.nil? end |
#misspellings? ⇒ Boolean
203 204 205 |
# File 'lib/searchkick/results.rb', line 203 def misspellings? @options[:misspellings] end |
#model_name ⇒ Object
120 121 122 |
# File 'lib/searchkick/results.rb', line 120 def model_name klass.model_name end |
#next_page ⇒ Object
167 168 169 |
# File 'lib/searchkick/results.rb', line 167 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset_value ⇒ Object Also known as: offset
157 158 159 |
# File 'lib/searchkick/results.rb', line 157 def offset_value (current_page - 1) * per_page + padding end |
#out_of_range? ⇒ Boolean
179 180 181 |
# File 'lib/searchkick/results.rb', line 179 def out_of_range? current_page > total_pages end |
#padding ⇒ Object
148 149 150 |
# File 'lib/searchkick/results.rb', line 148 def padding [:padding] end |
#per_page ⇒ Object Also known as: limit_value
143 144 145 |
# File 'lib/searchkick/results.rb', line 143 def per_page [:per_page] end |
#previous_page ⇒ Object Also known as: prev_page
162 163 164 |
# File 'lib/searchkick/results.rb', line 162 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#results ⇒ Object
18 19 20 |
# File 'lib/searchkick/results.rb', line 18 def results @results ||= with_hit.map(&:first) end |
#suggestions ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/searchkick/results.rb', line 83 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 [:term] == "*" [] else raise "Pass `suggest: true` to the search method for suggestions" end end |
#took ⇒ Object
112 113 114 |
# File 'lib/searchkick/results.rb', line 112 def took response["took"] end |
#total_count ⇒ Object Also known as: total_entries
134 135 136 |
# File 'lib/searchkick/results.rb', line 134 def total_count [:total_entries] || response["hits"]["total"] end |
#total_pages ⇒ Object Also known as: num_pages
152 153 154 |
# File 'lib/searchkick/results.rb', line 152 def total_pages (total_count / per_page.to_f).ceil end |
#with_highlights(multiple: false) ⇒ Object
197 198 199 200 201 |
# File 'lib/searchkick/results.rb', line 197 def with_highlights(multiple: false) with_hit.map do |result, hit| [result, hit_highlights(hit, multiple: multiple)] end end |
#with_hit ⇒ Object
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/searchkick/results.rb', line 22 def with_hit @with_hit ||= begin if [:load] # results can have different types results = {} hits.group_by { |hit, _| hit["_type"] }.each do |type, grouped_hits| klass = (![:index_name] && @klass) || type.camelize.constantize results[type] = results_query(klass, grouped_hits).to_a.index_by { |r| r.id.to_s } end missing_ids = [] # sort results = hits.map do |hit| result = results[hit["_type"]][hit["_id"].to_s] if result && !([:load].is_a?(Hash) && [:load][:dumpable]) if (hit["highlight"] || [:highlight]) && !result.respond_to?(:search_highlights) highlights = hit_highlights(hit) result.define_singleton_method(:search_highlights) do highlights end end end [result, hit] end.select do |result, hit| missing_ids << hit["_id"] unless result result end if missing_ids.any? warn "[searchkick] WARNING: Records in search index do not exist in database: #{missing_ids.join(", ")}" end results else hits.map do |hit| result = if hit["_source"] hit.except("_source").merge(hit["_source"]) elsif hit["fields"] hit.except("fields").merge(hit["fields"]) else hit end if hit["highlight"] || [:highlight] highlight = Hash[hit["highlight"].to_a.map { |k, v| [base_field(k), v.first] }] [:highlighted_fields].map { |k| base_field(k) }.each do |k| result["highlighted_#{k}"] ||= (highlight[k] || result[k]) end end result["id"] ||= result["_id"] # needed for legacy reasons [HashWrapper.new(result), hit] end end end end |