Skip to content

[Backport 8.x] Allow tuples and other iterables in source() method #1898

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

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
12 changes: 8 additions & 4 deletions elasticsearch_dsl/search_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
from typing_extensions import Self, TypeVar

from .aggs import A, Agg, AggBase
from .document_base import InstrumentedField
from .exceptions import IllegalOperation
from .query import Bool, Q, Query
from .response import Hit, Response
from .utils import _R, AnyUsingType, AttrDict, DslBase, recursive_to_dict

if TYPE_CHECKING:
from .document_base import InstrumentedField
from .field import Field, Object


Expand Down Expand Up @@ -714,10 +714,14 @@ def ensure_strings(
Dict[str, List[Union[str, "InstrumentedField"]]],
]
) -> Union[str, List[str], Dict[str, List[str]]]:
if isinstance(fields, list):
return [str(f) for f in fields]
elif isinstance(fields, dict):
if isinstance(fields, dict):
return {k: ensure_strings(v) for k, v in fields.items()}
elif not isinstance(fields, (str, InstrumentedField)):
# we assume that if `fields` is not a any of [dict, str,
# InstrumentedField] then it is an iterable of strings or
# InstrumentedFields, so we convert them to a plain list of
# strings
return [str(f) for f in fields]
else:
return str(fields)

Expand Down
2 changes: 1 addition & 1 deletion tests/_async/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_source() -> None:

assert {
"_source": {"includes": ["foo.bar.*"], "excludes": ["foo.one"]}
} == AsyncSearch().source(includes=["foo.bar.*"], excludes=["foo.one"]).to_dict()
} == AsyncSearch().source(includes=["foo.bar.*"], excludes=("foo.one",)).to_dict()

assert {"_source": False} == AsyncSearch().source(False).to_dict()

Expand Down
2 changes: 1 addition & 1 deletion tests/_sync/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_source() -> None:

assert {
"_source": {"includes": ["foo.bar.*"], "excludes": ["foo.one"]}
} == Search().source(includes=["foo.bar.*"], excludes=["foo.one"]).to_dict()
} == Search().source(includes=["foo.bar.*"], excludes=("foo.one",)).to_dict()

assert {"_source": False} == Search().source(False).to_dict()

Expand Down
Loading