Skip to content

Commit

Permalink
Fixed use of dictionaries as values in Terms query (#1921)
Browse files Browse the repository at this point in the history
Fixes #1920
  • Loading branch information
miguelgrinberg authored Oct 7, 2024
1 parent ec8da55 commit fb18f38
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion elasticsearch_dsl/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2649,7 +2649,7 @@ def __init__(

def _setattr(self, name: str, value: Any) -> None:
# here we convert any iterables that are not strings to lists
if hasattr(value, "__iter__") and not isinstance(value, (str, list)):
if hasattr(value, "__iter__") and not isinstance(value, (str, list, dict)):
value = list(value)
super()._setattr(name, value)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def test_terms_to_dict() -> None:
assert {"terms": {"_type": ["article", "section"], "boost": 1.1}} == query.Terms(
_type=("article", "section"), boost=1.1
).to_dict()
assert {"terms": {"_type": "article", "boost": 1.1}} == query.Terms(
_type="article", boost=1.1
).to_dict()
assert {
"terms": {"_id": {"index": "my-other-index", "id": "my-id"}, "boost": 1.1}
} == query.Terms(
_id={"index": "my-other-index", "id": "my-id"}, boost=1.1
).to_dict()


def test_bool_to_dict() -> None:
Expand Down
2 changes: 1 addition & 1 deletion utils/templates/query.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ EMPTY_QUERY = MatchAll()
{% elif k.name == "Terms" %}
def _setattr(self, name: str, value: Any) -> None:
# here we convert any iterables that are not strings to lists
if hasattr(value, "__iter__") and not isinstance(value, (str, list)):
if hasattr(value, "__iter__") and not isinstance(value, (str, list, dict)):
value = list(value)
super()._setattr(name, value)

Expand Down

0 comments on commit fb18f38

Please sign in to comment.