Class: ElasticsearchDslBuilder::DSL::Search::Aggregations::Aggregation

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

Overview

Base class for aggregation types

Instance Method Summary collapse

Instance Method Details

#aggregation(name, aggregation) ⇒ Object

Defines an aggregation nested in another one

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
# File 'lib/elasticsearch_dsl_builder/dsl/search/aggregation.rb', line 15

def aggregation(name, aggregation)
  name_valid = name.instance_of?(String) || name.instance_of?(Symbol)
  raise ArgumentError, 'name must be a String or Symbol' unless name_valid
  raise ArgumentError, 'aggregation must extend Aggregation' unless aggregation.is_a?(Aggregation)

  @aggregations ||= AggregationsCollection.new
  @aggregations[name.to_sym] = aggregation
  self
end

#to_hashObject



25
26
27
28
29
30
# File 'lib/elasticsearch_dsl_builder/dsl/search/aggregation.rb', line 25

def to_hash
  hash = {}
  hash.update(@type => @aggregation.to_hash) if @aggregation
  hash.update(aggregations: @aggregations.to_hash) if @aggregations
  hash
end