Class: Searchkick::Results

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/searchkick/results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @klass = klass
  @response = response
  @options = options
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/searchkick/results.rb', line 6

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/searchkick/results.rb', line 6

def options
  @options
end

#responseObject (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_pageObject



68
69
70
# File 'lib/searchkick/results.rb', line 68

def current_page
  options[:page]
end

#each_with_hit(&block) ⇒ Object



41
42
43
# File 'lib/searchkick/results.rb', line 41

def each_with_hit(&block)
  results.zip(hits).each(&block)
end

#facetsObject



55
56
57
# File 'lib/searchkick/results.rb', line 55

def facets
  response["facets"]
end

#model_nameObject



59
60
61
# File 'lib/searchkick/results.rb', line 59

def model_name
  klass.model_name
end

#next_pageObject



89
90
91
# File 'lib/searchkick/results.rb', line 89

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#offset_valueObject



81
82
83
# File 'lib/searchkick/results.rb', line 81

def offset_value
  current_page * per_page
end

#per_pageObject Also known as: limit_value



72
73
74
# File 'lib/searchkick/results.rb', line 72

def per_page
  options[:per_page]
end

#previous_pageObject



85
86
87
# File 'lib/searchkick/results.rb', line 85

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#resultsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/searchkick/results.rb', line 16

def results
  @results ||= begin
    if options[:load]
      hit_ids = hits.map{|hit| hit["_id"] }
      records = klass
      if options[:includes]
        records = records.includes(options[:includes])
      end
      records = records.find(hit_ids)
      hit_ids = hit_ids.map(&:to_s)
      records.sort_by{|r| hit_ids.index(r.id.to_s)  }
    else
      hits.map{|hit| Hashie::Mash.new(hit["_source"]) }
    end
  end
end

#suggestionsObject



33
34
35
36
37
38
39
# File 'lib/searchkick/results.rb', line 33

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_countObject Also known as: total_entries



63
64
65
# File 'lib/searchkick/results.rb', line 63

def total_count
  response["hits"]["total"]
end

#total_pagesObject



77
78
79
# File 'lib/searchkick/results.rb', line 77

def total_pages
  (total_count / per_page.to_f).ceil
end

#with_detailsObject



45
46
47
48
49
50
51
52
53
# File 'lib/searchkick/results.rb', line 45

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