Class: Elasticsearch::Model::Response::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/model/response/result.rb

Overview

Encapsulates the “hit” returned from the Elasticsearch client

Wraps the raw Hash with in a ‘Hashie::Mash` instance, providing access to the Hash properties by calling Ruby methods.

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Result

Returns a new instance of Result.

Parameters:

  • attributes (Hash) (defaults to: {})

    A Hash with document properties



33
34
35
# File 'lib/elasticsearch/model/response/result.rb', line 33

def initialize(attributes={})
  @result = HashWrapper.new(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments) ⇒ Object

Delegate methods to ‘@result` or `@result._source`



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/elasticsearch/model/response/result.rb', line 45

def method_missing(name, *arguments)
  case
  when name.to_s.end_with?('?')
    @result.__send__(name, *arguments) || ( @result._source && @result._source.__send__(name, *arguments) )
  when @result.respond_to?(name)
    @result.__send__ name, *arguments
  when @result._source && @result._source.respond_to?(name)
    @result._source.__send__ name, *arguments
  else
    super
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



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

def as_json(options={})
  @result.as_json(options)
end

#idObject

Return document ‘_id` as `id`



39
40
41
# File 'lib/elasticsearch/model/response/result.rb', line 39

def id
  @result['_id']
end

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

Respond to methods from ‘@result` or `@result._source`

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/elasticsearch/model/response/result.rb', line 60

def respond_to_missing?(method_name, include_private = false)
  @result.respond_to?(method_name.to_sym) || \
  @result._source && @result._source.respond_to?(method_name.to_sym) || \
  super
end