diff --git a/elasticsearch_dsl/aggs.py b/elasticsearch_dsl/aggs.py index ef032f7a..df677ba2 100644 --- a/elasticsearch_dsl/aggs.py +++ b/elasticsearch_dsl/aggs.py @@ -127,7 +127,7 @@ def __getitem__(self, agg_name: str) -> Agg[_R]: # make sure we're not mutating a shared state - whenever accessing a # bucket, return a shallow copy of it to be safe if isinstance(agg, Bucket): - agg = A(agg.name, filter=None, **agg._params) + agg = A(agg.name, **agg._params) # be sure to store the copy so any modifications to it will affect us self._params["aggs"][agg_name] = agg diff --git a/tests/test_aggs.py b/tests/test_aggs.py index 0fdf3306..30b39c3a 100644 --- a/tests/test_aggs.py +++ b/tests/test_aggs.py @@ -476,3 +476,12 @@ def test_top_metrics_aggregation() -> None: assert { "top_metrics": {"metrics": {"field": "m"}, "sort": {"s": "desc"}} } == a.to_dict() + + +def test_bucket_agg_with_filter() -> None: + b = aggs.Filter(query.Terms(something=[1, 2, 3])) + + a = aggs.Terms(field="some_field", size=100) + a.bucket("b", b) + + assert a.aggs["b"] == a["b"] # a['b'] threw exception before patch #1902