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



12
13
14
15
16
# File 'lib/searchkick/results.rb', line 12

def initialize(klass, response, options = {})
  @klass = klass
  @response = response
  @options = options
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/searchkick/results.rb', line 8

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/searchkick/results.rb', line 8

def options
  @options
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/searchkick/results.rb', line 8

def response
  @response
end

Instance Method Details

#current_pageObject



100
101
102
# File 'lib/searchkick/results.rb', line 100

def current_page
  options[:page]
end

#each_with_hit(&block) ⇒ Object



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

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

#entry_nameObject



91
92
93
# File 'lib/searchkick/results.rb', line 91

def entry_name
  model_name.human.downcase
end

#facetsObject



83
84
85
# File 'lib/searchkick/results.rb', line 83

def facets
  response["facets"]
end

#first_page?Boolean



132
133
134
# File 'lib/searchkick/results.rb', line 132

def first_page?
  previous_page.nil?
end

#last_page?Boolean



136
137
138
# File 'lib/searchkick/results.rb', line 136

def last_page?
  next_page.nil?
end

#model_nameObject



87
88
89
# File 'lib/searchkick/results.rb', line 87

def model_name
  klass.model_name
end

#next_pageObject



128
129
130
# File 'lib/searchkick/results.rb', line 128

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

#offset_valueObject Also known as: offset



118
119
120
# File 'lib/searchkick/results.rb', line 118

def offset_value
  (current_page - 1) * per_page + padding
end

#paddingObject



109
110
111
# File 'lib/searchkick/results.rb', line 109

def padding
  options[:padding]
end

#per_pageObject Also known as: limit_value



104
105
106
# File 'lib/searchkick/results.rb', line 104

def per_page
  options[:per_page]
end

#previous_pageObject Also known as: prev_page



123
124
125
# File 'lib/searchkick/results.rb', line 123

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

#resultsObject



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
53
54
55
56
57
58
59
# File 'lib/searchkick/results.rb', line 18

def results
  @results ||= begin
    if options[:load]
      # results can have different types
      results = {}

      hits.group_by{|hit, i| hit["_type"] }.each do |type, grouped_hits|
        records = type.camelize.constantize
        if options[:includes]
          records = records.includes(options[:includes])
        end
        results[type] =
          if records.respond_to?(:primary_key) and records.primary_key
            # ActiveRecord
            records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a
          elsif records.respond_to?(:all) and records.all.respond_to?(:for_ids)
            # Mongoid 2
            records.all.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
          else
            # Mongoid 3+
            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

#suggestionsObject



61
62
63
64
65
66
67
# File 'lib/searchkick/results.rb', line 61

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



95
96
97
# File 'lib/searchkick/results.rb', line 95

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

#total_pagesObject Also known as: num_pages



113
114
115
# File 'lib/searchkick/results.rb', line 113

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

#with_detailsObject



73
74
75
76
77
78
79
80
81
# File 'lib/searchkick/results.rb', line 73

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(/\.analyzed\z/, "")).to_sym, v.first] } ]
    end
    [model, details]
  end
end