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.



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

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

Instance Attribute Details

#klassObject (readonly)

TODO remove klass and options in 6.0



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

def klass
  @klass
end

#optionsObject (readonly)

TODO remove klass and options in 6.0



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

def options
  @options
end

#responseObject (readonly)

TODO remove klass and options in 6.0



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

def response
  @response
end

Instance Method Details

#aggregationsObject



44
45
46
# File 'lib/searchkick/results.rb', line 44

def aggregations
  response["aggregations"]
end

#aggsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/searchkick/results.rb', line 48

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

#clear_scrollObject



207
208
209
210
211
212
213
214
215
216
# File 'lib/searchkick/results.rb', line 207

def clear_scroll
  begin
    # try to clear scroll
    # not required as scroll will expire
    # but there is a cost to open scrolls
    Searchkick.client.clear_scroll(scroll_id: scroll_id)
  rescue => e
    raise e unless Searchkick.transport_error?(e)
  end
end

#current_pageObject



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

def current_page
  options[:page]
end

#entry_name(options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/searchkick/results.rb', line 79

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

#errorObject



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

def error
  response["error"]
end

#first_page?Boolean

Returns:

  • (Boolean)


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

def first_page?
  previous_page.nil?
end

#highlights(multiple: false) ⇒ Object



152
153
154
155
156
# File 'lib/searchkick/results.rb', line 152

def highlights(multiple: false)
  hits.map do |hit|
    hit_highlights(hit, multiple: multiple)
  end
end

#hitsObject



144
145
146
147
148
149
150
# File 'lib/searchkick/results.rb', line 144

def hits
  if error
    raise Error, "Query error - use the error method to view it"
  else
    @response["hits"]["hits"]
  end
end

#last_page?Boolean

Returns:

  • (Boolean)


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

def last_page?
  next_page.nil?
end

#missing_recordsObject



30
31
32
# File 'lib/searchkick/results.rb', line 30

def missing_records
  @missing_records ||= with_hit_and_missing_records[1]
end

#misspellings?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/searchkick/results.rb', line 174

def misspellings?
  @options[:misspellings]
end

#model_nameObject



71
72
73
74
75
76
77
# File 'lib/searchkick/results.rb', line 71

def model_name
  if klass.nil?
    ActiveModel::Name.new(self.class, nil, 'Result')
  else
    klass.model_name
  end
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

#out_of_range?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/searchkick/results.rb', line 140

def out_of_range?
  current_page > total_pages
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

TODO make private in 6.0



18
19
20
# File 'lib/searchkick/results.rb', line 18

def results
  @results ||= with_hit.map(&:first)
end

#scrollObject

Raises:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/searchkick/results.rb', line 182

def scroll
  raise Error, "Pass `scroll` option to the search method for scrolling" unless scroll_id

  if block_given?
    records = self
    while records.any?
      yield records
      records = records.scroll
    end

    records.clear_scroll
  else
    begin
      # TODO Active Support notifications for this scroll call
      Results.new(@klass, Searchkick.client.scroll(scroll: options[:scroll], body: {scroll_id: scroll_id}), @options)
    rescue => e
      if Searchkick.not_found_error?(e) && e.message =~ /search_context_missing_exception/i
        raise Error, "Scroll id has expired"
      else
        raise e
      end
    end
  end
end

#scroll_idObject



178
179
180
# File 'lib/searchkick/results.rb', line 178

def scroll_id
  @response["_scroll_id"]
end

#suggestionsObject



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

def suggestions
  if response["suggest"]
    response["suggest"].values.flat_map { |v| v.first["options"] }.sort_by { |o| -o["score"] }.map { |o| o["text"] }.uniq
  elsif options[:suggest] || options[:term] == "*" # TODO remove 2nd term
    []
  else
    raise "Pass `suggest: true` to the search method for suggestions"
  end
end

#tookObject



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

def took
  response["took"]
end

#total_countObject Also known as: total_entries



89
90
91
92
93
94
95
96
97
# File 'lib/searchkick/results.rb', line 89

def total_count
  if options[:total_entries]
    options[:total_entries]
  elsif response["hits"]["total"].is_a?(Hash)
    response["hits"]["total"]["value"]
  else
    response["hits"]["total"]
  end
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_highlights(multiple: false) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/searchkick/results.rb', line 158

def with_highlights(multiple: false)
  return enum_for(:with_highlights, multiple: multiple) unless block_given?

  with_hit.each do |result, hit|
    yield result, hit_highlights(hit, multiple: multiple)
  end
end

#with_hitObject



22
23
24
25
26
27
28
# File 'lib/searchkick/results.rb', line 22

def with_hit
  return enum_for(:with_hit) unless block_given?

  build_hits.each do |result|
    yield result
  end
end

#with_scoreObject



166
167
168
169
170
171
172
# File 'lib/searchkick/results.rb', line 166

def with_score
  return enum_for(:with_score) unless block_given?

  with_hit.each do |result, hit|
    yield result, hit["_score"]
  end
end