class Searchkick::Results
Attributes
klass[R]
options[R]
response[R]
Public Class Methods
new(klass, response, options = {})
click to toggle source
# File lib/searchkick/results.rb, line 12 def initialize(klass, response, options = {}) @klass = klass @response = response @options = options end
Public Instance Methods
aggregations()
click to toggle source
# File lib/searchkick/results.rb, line 99 def aggregations response["aggregations"] end
aggs()
click to toggle source
# File lib/searchkick/results.rb, line 103 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()
click to toggle source
# File lib/searchkick/results.rb, line 145 def current_page options[:page] end
each_with_hit(&block)
click to toggle source
# File lib/searchkick/results.rb, line 85 def each_with_hit(&block) results.zip(hits).each(&block) end
entry_name(options = {})
click to toggle source
# File lib/searchkick/results.rb, line 130 def entry_name(options = {}) if options.empty? # backward compatibility model_name.human.downcase else default = options[:count] == 1 ? model_name.human : model_name.human.pluralize model_name.human(options.reverse_merge(default: default)) end end
error()
click to toggle source
# File lib/searchkick/results.rb, line 122 def error response["error"] end
first_page?()
click to toggle source
# File lib/searchkick/results.rb, line 177 def first_page? previous_page.nil? end
hits()
click to toggle source
# File lib/searchkick/results.rb, line 189 def hits @response["hits"]["hits"] end
last_page?()
click to toggle source
# File lib/searchkick/results.rb, line 181 def last_page? next_page.nil? end
misspellings?()
click to toggle source
# File lib/searchkick/results.rb, line 193 def misspellings? @options[:misspellings] end
model_name()
click to toggle source
# File lib/searchkick/results.rb, line 126 def model_name klass.model_name end
next_page()
click to toggle source
# File lib/searchkick/results.rb, line 173 def next_page current_page < total_pages ? (current_page + 1) : nil end
offset_value()
click to toggle source
# File lib/searchkick/results.rb, line 163 def offset_value (current_page - 1) * per_page + padding end
Also aliased as: offset
out_of_range?()
click to toggle source
# File lib/searchkick/results.rb, line 185 def out_of_range? current_page > total_pages end
padding()
click to toggle source
# File lib/searchkick/results.rb, line 154 def padding options[:padding] end
per_page()
click to toggle source
# File lib/searchkick/results.rb, line 149 def per_page options[:per_page] end
Also aliased as: limit_value
previous_page()
click to toggle source
# File lib/searchkick/results.rb, line 168 def previous_page current_page > 1 ? (current_page - 1) : nil end
Also aliased as: prev_page
records()
click to toggle source
experimental: may not make next release
# File lib/searchkick/results.rb, line 19 def records @records ||= results_query(klass, hits) end
results()
click to toggle source
# File lib/searchkick/results.rb, line 23 def results @results ||= begin if options[: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| result = results[hit["_type"]][hit["_id"].to_s] if result && !(options[:load].is_a?(Hash) && options[:load][:dumpable]) unless result.respond_to?(:search_hit) result.define_singleton_method(:search_hit) do hit end end if hit["highlight"] && !result.respond_to?(:search_highlights) highlights = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }] result.define_singleton_method(:search_highlights) do highlights end end end result 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] }] options[: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()
click to toggle source
# File lib/searchkick/results.rb, line 77 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()
click to toggle source
# File lib/searchkick/results.rb, line 118 def took response["took"] end
total_count()
click to toggle source
# File lib/searchkick/results.rb, line 140 def total_count response["hits"]["total"] end
Also aliased as: total_entries
total_pages()
click to toggle source
# File lib/searchkick/results.rb, line 158 def total_pages (total_count / per_page.to_f).ceil end
Also aliased as: num_pages
with_details()
click to toggle source
# File lib/searchkick/results.rb, line 89 def with_details each_with_hit.map do |model, hit| details = {} if hit["highlight"] details[:highlight] = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }] end [model, details] end end
Private Instance Methods
base_field(k)
click to toggle source
# File lib/searchkick/results.rb, line 218 def base_field(k) k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "") end
results_query(records, hits)
click to toggle source
# File lib/searchkick/results.rb, line 199 def results_query(records, hits) ids = hits.map { |hit| hit["_id"] } if options[:includes] records = if defined?(NoBrainer::Document) && records < NoBrainer::Document if Gem.loaded_specs["nobrainer"].version >= Gem::Version.new("0.21") records.eager_load(options[:includes]) else records.preload(options[:includes]) end else records.includes(options[:includes]) end end Searchkick.load_records(records, ids) end