Skip to content

Commit 7391f3b

Browse files
[8.0] Rewrite the 'from' parameter from kwargs/query into 'from_'
Co-authored-by: Seth Michael Larson <[email protected]>
1 parent 35cbb56 commit 7391f3b

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

elasticsearch/_async/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,16 @@ def pop_transport_kwargs(kw: MutableMapping[str, Any]) -> MutableMapping[str, An
428428
)
429429
client._client_meta = (("h", "s"),)
430430

431+
# Setting query={"from": ...} would make 'from' be used
432+
# as a keyword argument instead of 'from_'. We handle that here.
433+
def normalize_from_keyword(kw: MutableMapping[str, Any]) -> None:
434+
if "from" in kw:
435+
kw["from_"] = kw.pop("from")
436+
437+
normalize_from_keyword(kwargs)
431438
try:
432439
search_kwargs = query.copy() if query else {}
440+
normalize_from_keyword(search_kwargs)
433441
search_kwargs.update(kwargs)
434442
search_kwargs["scroll"] = scroll
435443
search_kwargs["size"] = size

elasticsearch/helpers/actions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,16 @@ def pop_transport_kwargs(kw: MutableMapping[str, Any]) -> Dict[str, Any]:
690690
)
691691
client._client_meta = (("h", "s"),)
692692

693+
# Setting query={"from": ...} would make 'from' be used
694+
# as a keyword argument instead of 'from_'. We handle that here.
695+
def normalize_from_keyword(kw: MutableMapping[str, Any]) -> None:
696+
if "from" in kw:
697+
kw["from_"] = kw.pop("from")
698+
699+
normalize_from_keyword(kwargs)
693700
try:
694701
search_kwargs = query.copy() if query else {}
702+
normalize_from_keyword(search_kwargs)
695703
search_kwargs.update(kwargs)
696704
search_kwargs["scroll"] = scroll
697705
search_kwargs["size"] = size

test_elasticsearch/test_async/test_server/test_helpers.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,44 @@ async def test_scan_auth_kwargs_favor_scroll_kwargs_option(
831831
]
832832

833833

834+
@pytest.mark.parametrize(
835+
"scan_kwargs",
836+
[
837+
{"from": 1},
838+
{"from_": 1},
839+
{"query": {"from": 1}},
840+
{"query": {"from_": 1}},
841+
{"query": {"query": {"match_all": {}}}, "from": 1},
842+
{"query": {"query": {"match_all": {}}}, "from_": 1},
843+
],
844+
)
845+
async def test_scan_from_keyword_is_aliased(async_client, scan_kwargs):
846+
with patch.object(async_client, "options", return_value=async_client), patch.object(
847+
async_client,
848+
"search",
849+
return_value=MockResponse(
850+
ObjectApiResponse(
851+
body={
852+
"_scroll_id": "dummy_id",
853+
"_shards": {"successful": 5, "total": 5},
854+
"hits": {"hits": []},
855+
},
856+
meta=None,
857+
)
858+
),
859+
) as search_mock, patch.object(
860+
async_client, "clear_scroll", return_value=MockResponse(None)
861+
):
862+
[
863+
x
864+
async for x in helpers.async_scan(
865+
async_client, index="test_index", **scan_kwargs
866+
)
867+
]
868+
assert search_mock.call_args[1]["from_"] == 1
869+
assert "from" not in search_mock.call_args[1]
870+
871+
834872
@pytest.fixture(scope="function")
835873
async def reindex_setup(async_client):
836874
bulk = []

test_elasticsearch/test_server/test_helpers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,35 @@ def test_shards_no_skipped_field(sync_client):
799799
assert data == [{"search_data": 1}, {"scroll_data": 42}]
800800

801801

802+
@pytest.mark.parametrize(
803+
"scan_kwargs",
804+
[
805+
{"from": 1},
806+
{"from_": 1},
807+
{"query": {"from": 1}},
808+
{"query": {"from_": 1}},
809+
{"query": {"query": {"match_all": {}}}, "from": 1},
810+
{"query": {"query": {"match_all": {}}}, "from_": 1},
811+
],
812+
)
813+
def test_scan_from_keyword_is_aliased(sync_client, scan_kwargs):
814+
with patch.object(sync_client, "options", return_value=sync_client), patch.object(
815+
sync_client,
816+
"search",
817+
return_value=ObjectApiResponse(
818+
raw={
819+
"_scroll_id": "dummy_id",
820+
"_shards": {"successful": 5, "total": 5},
821+
"hits": {"hits": []},
822+
},
823+
meta=None,
824+
),
825+
) as search_mock, patch.object(sync_client, "clear_scroll"):
826+
list(helpers.scan(sync_client, index="test_index", **scan_kwargs))
827+
assert search_mock.call_args[1]["from_"] == 1
828+
assert "from" not in search_mock.call_args[1]
829+
830+
802831
@pytest.fixture(scope="function")
803832
def reindex_setup(sync_client):
804833
bulk = []

0 commit comments

Comments
 (0)