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
- #each_with_hit(&block) ⇒ Object
- #entry_name ⇒ Object
- #error ⇒ Object
- #facets ⇒ Object
- #first_page? ⇒ Boolean
- #hits ⇒ Object
-
#initialize(klass, response, options = {}) ⇒ Results
constructor
A new instance of Results.
- #last_page? ⇒ 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)
-
#records ⇒ Object
experimental: may not make next release.
- #results ⇒ Object
- #suggestions ⇒ Object
- #took ⇒ Object
- #total_count ⇒ Object (also: #total_entries)
- #total_pages ⇒ Object (also: #num_pages)
- #with_details ⇒ 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
88 89 90 |
# File 'lib/searchkick/results.rb', line 88 def aggregations response["aggregations"] end |
#aggs ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/searchkick/results.rb', line 92 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
128 129 130 |
# File 'lib/searchkick/results.rb', line 128 def current_page [:page] end |
#each_with_hit(&block) ⇒ Object
70 71 72 |
# File 'lib/searchkick/results.rb', line 70 def each_with_hit(&block) results.zip(hits).each(&block) end |
#entry_name ⇒ Object
119 120 121 |
# File 'lib/searchkick/results.rb', line 119 def entry_name model_name.human.downcase end |
#error ⇒ Object
111 112 113 |
# File 'lib/searchkick/results.rb', line 111 def error response["error"] end |
#facets ⇒ Object
84 85 86 |
# File 'lib/searchkick/results.rb', line 84 def facets response["facets"] end |
#first_page? ⇒ Boolean
160 161 162 |
# File 'lib/searchkick/results.rb', line 160 def first_page? previous_page.nil? end |
#hits ⇒ Object
172 173 174 |
# File 'lib/searchkick/results.rb', line 172 def hits @response["hits"]["hits"] end |
#last_page? ⇒ Boolean
164 165 166 |
# File 'lib/searchkick/results.rb', line 164 def last_page? next_page.nil? end |
#model_name ⇒ Object
115 116 117 |
# File 'lib/searchkick/results.rb', line 115 def model_name klass.model_name end |
#next_page ⇒ Object
156 157 158 |
# File 'lib/searchkick/results.rb', line 156 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset_value ⇒ Object Also known as: offset
146 147 148 |
# File 'lib/searchkick/results.rb', line 146 def offset_value (current_page - 1) * per_page + padding end |
#out_of_range? ⇒ Boolean
168 169 170 |
# File 'lib/searchkick/results.rb', line 168 def out_of_range? current_page > total_pages end |
#padding ⇒ Object
137 138 139 |
# File 'lib/searchkick/results.rb', line 137 def padding [:padding] end |
#per_page ⇒ Object Also known as: limit_value
132 133 134 |
# File 'lib/searchkick/results.rb', line 132 def per_page [:per_page] end |
#previous_page ⇒ Object Also known as: prev_page
151 152 153 |
# File 'lib/searchkick/results.rb', line 151 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#records ⇒ Object
experimental: may not make next release
19 20 21 |
# File 'lib/searchkick/results.rb', line 19 def records @records ||= results_query(klass, hits) end |
#results ⇒ Object
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 |
# File 'lib/searchkick/results.rb', line 23 def results @results ||= begin if [:load] # results can have different types results = {} hits.group_by { |hit, _| hit["_type"] }.each do |type, grouped_hits| results[type] = results_query(type.camelize.constantize, grouped_hits).to_a.index_by { |r| r.id.to_s } end # sort hits.map do |hit| results[hit["_type"]][hit["_id"].to_s] end.compact 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 = Hash[hit["highlight"].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 Hashie::Mash.new(result) end end end end |
#suggestions ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/searchkick/results.rb', line 62 def suggestions if response["suggest"] response["suggest"].values.flat_map { |v| v.first["options"] }.sort_by { |o| -o["score"] }.map { |o| o["text"] }.uniq else raise "Pass `suggest: true` to the search method for suggestions" end end |
#took ⇒ Object
107 108 109 |
# File 'lib/searchkick/results.rb', line 107 def took response["took"] end |
#total_count ⇒ Object Also known as: total_entries
123 124 125 |
# File 'lib/searchkick/results.rb', line 123 def total_count response["hits"]["total"] end |
#total_pages ⇒ Object Also known as: num_pages
141 142 143 |
# File 'lib/searchkick/results.rb', line 141 def total_pages (total_count / per_page.to_f).ceil end |
#with_details ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/searchkick/results.rb', line 74 def with_details each_with_hit.map do |model, hit| details = {} if hit["highlight"] details[:highlight] = Hash[hit["highlight"].map { |k, v| [([:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }] end [model, details] end end |