Skip to content

Allow nested aggregation to accept an Aggregation instance. #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/elasticsearch/dsl/search/base_aggregation_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ def method_missing(name, *args, &block)
#
def aggregation(*args, &block)
@aggregations ||= AggregationsCollection.new
@aggregations.update args.first => Aggregation.new(*args, &block)

if block
@aggregations.update args.first => Aggregation.new(*args, &block)
else
name = args.shift
@aggregations.update name => args.shift
end
self
end

Expand Down
14 changes: 14 additions & 0 deletions test/unit/search_base_aggregation_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ class ::Elasticsearch::DSL::Search::Aggregations::Dummy

assert_equal 'foo', subject.to_hash[:aggregations][:inner][:dummy][:field]
end

should "add a nested aggregation instance" do
nested = Elasticsearch::DSL::Search::Aggregation.new do
dummy field: 'foo'
end
subject.aggregation :inner, nested

assert !subject.aggregations.empty?, "#{subject.aggregations.inspect} is empty"

assert_instance_of Elasticsearch::DSL::Search::Aggregation, subject.aggregations[:inner]
assert_equal({ dummy: { field: 'foo' } }, subject.aggregations[:inner].to_hash)

assert_equal 'foo', subject.to_hash[:aggregations][:inner][:dummy][:field]
end
end
end
end
Expand Down