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:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/elasticsearch/model/response/records.rb', line 21

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
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object

Delegate methods to ‘@records`



60
61
62
# File 'lib/elasticsearch/model/response/records.rb', line 60

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.



15
16
17
# File 'lib/elasticsearch/model/response/records.rb', line 15

def options
  @options
end

Instance Method Details

#each_with_hit(&block) ⇒ Object

Yields [record, hit] pairs to the block



48
49
50
# File 'lib/elasticsearch/model/response/records.rb', line 48

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

#idsObject

Returns the hit IDs



36
37
38
# File 'lib/elasticsearch/model/response/records.rb', line 36

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

#map_with_hit(&block) ⇒ Object

Yields [record, hit] pairs and returns the result



54
55
56
# File 'lib/elasticsearch/model/response/records.rb', line 54

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)


66
67
68
# File 'lib/elasticsearch/model/response/records.rb', line 66

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

#resultsObject



42
43
44
# File 'lib/elasticsearch/model/response/records.rb', line 42

def results
  response.results
end