From 0c3a7191755a8d60c7f16d59a80a20f83a6b8d08 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Fri, 22 Dec 2023 16:29:17 +0000 Subject: [PATCH] Fix importing collapse from dict (#1689) (cherry picked from commit f0c504538f19d3d70e2fb7d6402071f5fbaafc90) --- elasticsearch_dsl/search.py | 5 +++-- tests/test_search.py | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/elasticsearch_dsl/search.py b/elasticsearch_dsl/search.py index 1fa5b5906..50df5597c 100644 --- a/elasticsearch_dsl/search.py +++ b/elasticsearch_dsl/search.py @@ -120,7 +120,6 @@ def __init__(self, using="default", index=None, doc_type=None, extra=None): self._doc_type = [] self._doc_type_map = {} - self._collapse = {} if isinstance(doc_type, (tuple, list)): self._doc_type.extend(doc_type) elif isinstance(doc_type, collections.abc.Mapping): @@ -294,7 +293,6 @@ def _clone(self): s = self.__class__( using=self._using, index=self._index, doc_type=self._doc_type ) - s._collapse = self._collapse.copy() s._doc_type_map = self._doc_type_map.copy() s._extra = self._extra.copy() s._params = self._params.copy() @@ -408,6 +406,7 @@ def _clone(self): s = super()._clone() s._response_class = self._response_class + s._collapse = self._collapse.copy() s._sort = self._sort[:] s._source = copy.copy(self._source) if self._source is not None else None s._highlight = self._highlight.copy() @@ -446,6 +445,8 @@ def update_from_dict(self, d): self.aggs._params = { "aggs": {name: A(value) for (name, value) in aggs.items()} } + if "collapse" in d: + self._collapse = d.pop("collapse") if "sort" in d: self._sort = d.pop("sort") if "_source" in d: diff --git a/tests/test_search.py b/tests/test_search.py index ff1eed430..5cc84ff84 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -544,10 +544,12 @@ def test_update_from_dict(): s = search.Search() s.update_from_dict({"indices_boost": [{"important-documents": 2}]}) s.update_from_dict({"_source": ["id", "name"]}) + s.update_from_dict({"collapse": {"field": "user_id"}}) assert { "indices_boost": [{"important-documents": 2}], "_source": ["id", "name"], + "collapse": {"field": "user_id"}, } == s.to_dict()