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
- #current_page ⇒ Object
- #each_with_hit(&block) ⇒ Object
- #facets ⇒ Object
- #first_page? ⇒ Boolean
-
#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)
- #padding ⇒ Object
- #per_page ⇒ Object (also: #limit_value)
- #previous_page ⇒ Object
- #results ⇒ Object
- #suggestions ⇒ 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.
10 11 12 13 14 |
# File 'lib/searchkick/results.rb', line 10 def initialize(klass, response, = {}) @klass = klass @response = response = end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
6 7 8 |
# File 'lib/searchkick/results.rb', line 6 def klass @klass end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/searchkick/results.rb', line 6 def end |
#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
#current_page ⇒ Object
89 90 91 |
# File 'lib/searchkick/results.rb', line 89 def current_page [:page] end |
#each_with_hit(&block) ⇒ Object
62 63 64 |
# File 'lib/searchkick/results.rb', line 62 def each_with_hit(&block) results.zip(hits).each(&block) end |
#facets ⇒ Object
76 77 78 |
# File 'lib/searchkick/results.rb', line 76 def facets response["facets"] end |
#first_page? ⇒ Boolean
120 121 122 |
# File 'lib/searchkick/results.rb', line 120 def first_page? previous_page.nil? end |
#last_page? ⇒ Boolean
124 125 126 |
# File 'lib/searchkick/results.rb', line 124 def last_page? next_page.nil? end |
#model_name ⇒ Object
80 81 82 |
# File 'lib/searchkick/results.rb', line 80 def model_name klass.model_name end |
#next_page ⇒ Object
116 117 118 |
# File 'lib/searchkick/results.rb', line 116 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset_value ⇒ Object Also known as: offset
107 108 109 |
# File 'lib/searchkick/results.rb', line 107 def offset_value (current_page - 1) * per_page + padding end |
#padding ⇒ Object
98 99 100 |
# File 'lib/searchkick/results.rb', line 98 def padding [:padding] end |
#per_page ⇒ Object Also known as: limit_value
93 94 95 |
# File 'lib/searchkick/results.rb', line 93 def per_page [:per_page] end |
#previous_page ⇒ Object
112 113 114 |
# File 'lib/searchkick/results.rb', line 112 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#results ⇒ Object
16 17 18 19 20 21 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 |
# File 'lib/searchkick/results.rb', line 16 def results @results ||= begin if [:load] # results can have different types results = {} hits.group_by{|hit, i| hit["_type"] }.each do |type, grouped_hits| records = type.camelize.constantize if [:includes] records = records.includes([:includes]) end results[type] = if records.respond_to?(:primary_key) records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a else records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a end end # sort hits.map do |hit| results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s } end.compact else hits.map do |hit| result = if hit["_source"] hit.except("_source").merge(hit["_source"]) else hit.except("fields").merge(hit["fields"]) end result["id"] ||= result["_id"] # needed for legacy reasons Hashie::Mash.new(result) end end end end |
#suggestions ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/searchkick/results.rb', line 54 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 |
#total_count ⇒ Object Also known as: total_entries
84 85 86 |
# File 'lib/searchkick/results.rb', line 84 def total_count response["hits"]["total"] end |
#total_pages ⇒ Object Also known as: num_pages
102 103 104 |
# File 'lib/searchkick/results.rb', line 102 def total_pages (total_count / per_page.to_f).ceil end |
#with_details ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/searchkick/results.rb', line 66 def with_details each_with_hit.map do |model, hit| details = {} if hit["highlight"] details[:highlight] = Hash[ hit["highlight"].map{|k, v| [k.sub(/\.analyzed\z/, "").to_sym, v.first] } ] end [model, details] end end |