Class: OpenSearch::DSL::Search::Aggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/opensearch/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.



46
47
48
# File 'lib/opensearch/dsl/search/aggregation.rb', line 46

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



54
55
56
57
58
# File 'lib/opensearch/dsl/search/aggregation.rb', line 54

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

Instance Method Details

#aggregation(*args, &block) ⇒ Object

Defines an aggregation nested in another one



66
67
68
69
# File 'lib/opensearch/dsl/search/aggregation.rb', line 66

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

#aggregationsObject

Returns the aggregations



73
74
75
76
# File 'lib/opensearch/dsl/search/aggregation.rb', line 73

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)


84
85
86
87
88
89
90
# File 'lib/opensearch/dsl/search/aggregation.rb', line 84

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

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

Returns:

  • (Boolean)


60
61
62
# File 'lib/opensearch/dsl/search/aggregation.rb', line 60

def respond_to_missing?(method_name, include_private = false)
  Aggregations.const_defined?(Utils.__camelize(method_name)) || super
end

#to_hash(_options = {}) ⇒ Hash

Converts the object to a Hash

Returns:

  • (Hash)


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/opensearch/dsl/search/aggregation.rb', line 96

def to_hash(_options = {})
  call

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