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 88
def aggregations
  response["aggregations"]
end
aggs() click to toggle source
# 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() click to toggle source
# File lib/searchkick/results.rb, line 128
def current_page
  options[:page]
end
each_with_hit(&block) click to toggle source
# File lib/searchkick/results.rb, line 70
def each_with_hit(&block)
  results.zip(hits).each(&block)
end
entry_name() click to toggle source
# File lib/searchkick/results.rb, line 119
def entry_name
  model_name.human.downcase
end
error() click to toggle source
# File lib/searchkick/results.rb, line 111
def error
  response["error"]
end
facets() click to toggle source
# File lib/searchkick/results.rb, line 84
def facets
  response["facets"]
end
first_page?() click to toggle source
# File lib/searchkick/results.rb, line 160
def first_page?
  previous_page.nil?
end
hits() click to toggle source
# File lib/searchkick/results.rb, line 172
def hits
  @response["hits"]["hits"]
end
last_page?() click to toggle source
# File lib/searchkick/results.rb, line 164
def last_page?
  next_page.nil?
end
limit_value()
Alias for: per_page
model_name() click to toggle source
# File lib/searchkick/results.rb, line 115
def model_name
  klass.model_name
end
next_page() click to toggle source
# File lib/searchkick/results.rb, line 156
def next_page
  current_page < total_pages ? (current_page + 1) : nil
end
num_pages()
Alias for: total_pages
offset()
Alias for: offset_value
offset_value() click to toggle source
# File lib/searchkick/results.rb, line 146
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 168
def out_of_range?
  current_page > total_pages
end
padding() click to toggle source
# File lib/searchkick/results.rb, line 137
def padding
  options[:padding]
end
per_page() click to toggle source
# File lib/searchkick/results.rb, line 132
def per_page
  options[:per_page]
end
Also aliased as: limit_value
prev_page()
Alias for: previous_page
previous_page() click to toggle source
# File lib/searchkick/results.rb, line 151
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|
        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] }]
          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 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() click to toggle source
# File lib/searchkick/results.rb, line 107
def took
  response["took"]
end
total_count() click to toggle source
# File lib/searchkick/results.rb, line 123
def total_count
  response["hits"]["total"]
end
Also aliased as: total_entries
total_entries()
Alias for: total_count
total_pages() click to toggle source
# File lib/searchkick/results.rb, line 141
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 74
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 207
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 178
def results_query(records, hits)
  ids = hits.map { |hit| hit["_id"] }

  if options[:includes]
    records =
      if defined?(NoBrainer::Document) && records < NoBrainer::Document
        records.preload(options[:includes])
      else
        records.includes(options[:includes])
      end
  end

  if records.respond_to?(:primary_key) && records.primary_key
    # ActiveRecord
    records.where(records.primary_key => ids)
  elsif records.respond_to?(:all) && records.all.respond_to?(:for_ids)
    # Mongoid 2
    records.all.for_ids(ids)
  elsif records.respond_to?(:queryable)
    # Mongoid 3+
    records.queryable.for_ids(ids)
  elsif records.respond_to?(:unscoped) && records.all.respond_to?(:preload)
    # Nobrainer
    records.unscoped.where(:id.in => ids)
  else
    raise "Not sure how to load records"
  end
end