Class: Elasticsearch::DSL::Search::Aggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/dsl/search/aggregation.rb

Overview

Wraps the ‘aggregations` part of a search definition

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Aggregation

Returns a new instance of Aggregation.



14
15
16
# File 'lib/elasticsearch/dsl/search/aggregation.rb', line 14

def initialize(*args, &block)
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Looks up the corresponding class for a method being invoked, and initializes it

Raises:

  • (NoMethodError)

    When the corresponding class cannot be found



22
23
24
25
26
27
28
29
# File 'lib/elasticsearch/dsl/search/aggregation.rb', line 22

def method_missing(name, *args, &block)
  klass = Utils.__camelize(name)
  if Aggregations.const_defined? klass
    @value = Aggregations.const_get(klass).new *args, &block
  else
    raise NoMethodError, "undefined method '#{name}' for #{self}"
  end
end

Instance Method Details

#aggregation(*args, &block) ⇒ Object

Defines an aggregation nested in another one



33
34
35
36
# File 'lib/elasticsearch/dsl/search/aggregation.rb', line 33

def aggregation(*args, &block)
  call
  @value.__send__ :aggregation, *args, &block
end

#aggregationsObject

Returns the aggregations



40
41
42
43
# File 'lib/elasticsearch/dsl/search/aggregation.rb', line 40

def aggregations
  call
  @value.__send__ :aggregations
end

#callself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluates the block passed to initializer, ensuring it is called just once

Returns:

  • (self)


51
52
53
54
55
# File 'lib/elasticsearch/dsl/search/aggregation.rb', line 51

def call
  @block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block && ! @_block_called
  @_block_called = true
  self
end

#to_hash(options = {}) ⇒ Hash

Converts the object to a Hash

Returns:

  • (Hash)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/elasticsearch/dsl/search/aggregation.rb', line 61

def to_hash(options={})
  call

  if @value
    case
      when @value.respond_to?(:to_hash)
        @value.to_hash
      else
        @value
    end
  else
    {}
  end
end