Class: Elasticity::Search::Results

Inherits:
ActiveSupport::ProxyObject
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elasticity/search.rb

Constant Summary collapse

DEFAULT_SIZE =
10

Instance Method Summary collapse

Constructor Details

#initialize(response, body, mapper = nil) ⇒ Results



291
292
293
294
295
296
297
298
299
# File 'lib/elasticity/search.rb', line 291

def initialize(response, body, mapper = nil)
  @response = response
  @body = body
  @documents = if mapper.nil?
    @response["hits"]["hits"]
  else
    @response["hits"]["hits"].map { |hit| mapper.(hit) }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



301
302
303
# File 'lib/elasticity/search.rb', line 301

def method_missing(name, *args, &block)
  @documents.public_send(name, *args, &block)
end

Instance Method Details

#aggregationsObject



309
310
311
# File 'lib/elasticity/search.rb', line 309

def aggregations
  @response["aggregations"] ||= {}
end

#current_pageObject

for pagination



329
330
331
332
# File 'lib/elasticity/search.rb', line 329

def current_page
  return 1 if @body[:from].nil?
  @body[:from] / per_page + 1
end

#each(&block) ⇒ Object



305
306
307
# File 'lib/elasticity/search.rb', line 305

def each(&block)
  @documents.each(&block)
end

#next_pageObject



334
335
336
# File 'lib/elasticity/search.rb', line 334

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

#per_pageObject

for pagination



324
325
326
# File 'lib/elasticity/search.rb', line 324

def per_page
  @body[:size] || DEFAULT_SIZE
end

#previous_pageObject



338
339
340
# File 'lib/elasticity/search.rb', line 338

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

#totalObject Also known as: total_entries



313
314
315
# File 'lib/elasticity/search.rb', line 313

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

#total_pagesObject

for pagination



319
320
321
# File 'lib/elasticity/search.rb', line 319

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