Skip to content

Commit f227734

Browse files
Fixed use of dictionaries as values in Terms query
Fixes elastic#1920
1 parent bacfc74 commit f227734

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

elasticsearch_dsl/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2647,7 +2647,7 @@ def __init__(
26472647

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

tests/test_query.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ def test_terms_to_dict() -> None:
8484
assert {"terms": {"_type": ["article", "section"], "boost": 1.1}} == query.Terms(
8585
_type=("article", "section"), boost=1.1
8686
).to_dict()
87+
assert {"terms": {"_type": "article", "boost": 1.1}} == query.Terms(
88+
_type="article", boost=1.1
89+
).to_dict()
90+
assert {
91+
"terms": {"_id": {"index": "my-other-index", "id": "my-id"}, "boost": 1.1}
92+
} == query.Terms(
93+
_id={"index": "my-other-index", "id": "my-id"}, boost=1.1
94+
).to_dict()
8795

8896

8997
def test_bool_to_dict() -> None:

utils/templates/query.py.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ EMPTY_QUERY = MatchAll()
365365
{% elif k.name == "Terms" %}
366366
def _setattr(self, name: str, value: Any) -> None:
367367
# here we convert any iterables that are not strings to lists
368-
if hasattr(value, "__iter__") and not isinstance(value, (str, list)):
368+
if hasattr(value, "__iter__") and not isinstance(value, (str, list, dict)):
369369
value = list(value)
370370
super()._setattr(name, value)
371371

0 commit comments

Comments
 (0)