Class: Elasticsearch::Model::Response::Records

Inherits:
Object
  • Object
show all
Includes:
Base, Enumerable
Defined in:
lib/elasticsearch/model/response/records.rb

Overview

Encapsulates the collection of records returned from the database

Implements Enumerable and forwards its methods to the Base#records object, which is provided by an Adapter::Adapter implementation.

Instance Attribute Summary collapse

Attributes included from Base

#klass, #raw_response, #response

Instance Method Summary collapse

Methods included from Base

#max_score, #records, #total

Constructor Details

#initialize(klass, response, options = {}) ⇒ Records

Returns a new instance of Records.

See Also:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/elasticsearch/model/response/records.rb', line 38

def initialize(klass, response, options={})
  super

  # Include module provided by the adapter in the singleton class ("metaclass")
  #
  adapter = Adapter.from_class(klass)
  metaclass = class << self; self; end
  metaclass.__send__ :include, adapter.records_mixin

  self.options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object

Delegate methods to ‘@records`



76
77
78
# File 'lib/elasticsearch/model/response/records.rb', line 76

def method_missing(method_name, *arguments)
  records.respond_to?(method_name) ? records.__send__(method_name, *arguments) : super
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



32
33
34
# File 'lib/elasticsearch/model/response/records.rb', line 32

def options
  @options
end

Instance Method Details

#each_with_hit(&block) ⇒ Object

Yields [record, hit] pairs to the block



64
65
66
# File 'lib/elasticsearch/model/response/records.rb', line 64

def each_with_hit(&block)
  records.to_a.zip(results).each(&block)
end

#idsObject

Returns the hit IDs



52
53
54
# File 'lib/elasticsearch/model/response/records.rb', line 52

def ids
  response.response['hits']['hits'].map { |hit| hit['_id'] }
end

#map_with_hit(&block) ⇒ Object

Yields [record, hit] pairs and returns the result



70
71
72
# File 'lib/elasticsearch/model/response/records.rb', line 70

def map_with_hit(&block)
  records.to_a.zip(results).map(&block)
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Respond to methods from ‘@records`

Returns:

  • (Boolean)


82
83
84
# File 'lib/elasticsearch/model/response/records.rb', line 82

def respond_to?(method_name, include_private = false)
  records.respond_to?(method_name) || super
end

#resultsObject



58
59
60
# File 'lib/elasticsearch/model/response/records.rb', line 58

def results
  response.results
end