Skip to content

Commit bcd6efb

Browse files
authored
[7.17] Rewrite the 'from' parameter from kwargs/query into 'from_'
1 parent 1e01f63 commit bcd6efb

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

elasticsearch/_async/helpers.py

+8
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,16 @@ async def async_scan(
357357

358358
# initial search
359359
search_kwargs = kwargs.copy()
360+
361+
# Setting query={"from": ...} would make 'from' be used
362+
# as a keyword argument instead of 'from_'. We handle that here.
363+
if "from" in search_kwargs:
364+
search_kwargs["from_"] = search_kwargs.pop("from")
360365
if query:
361366
search_kwargs.update(query)
367+
if "from" in search_kwargs:
368+
search_kwargs["from_"] = search_kwargs.pop("from")
369+
362370
search_kwargs["scroll"] = scroll
363371
search_kwargs["size"] = size
364372
search_kwargs["request_timeout"] = request_timeout

elasticsearch/helpers/actions.py

+8
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,16 @@ def scan(
567567

568568
# initial search
569569
search_kwargs = kwargs.copy()
570+
571+
# Setting query={"from": ...} would make 'from' be used
572+
# as a keyword argument instead of 'from_'. We handle that here.
573+
if "from" in search_kwargs:
574+
search_kwargs["from_"] = search_kwargs.pop("from")
570575
if query:
571576
search_kwargs.update(query)
577+
if "from" in search_kwargs:
578+
search_kwargs["from_"] = search_kwargs.pop("from")
579+
572580
search_kwargs["scroll"] = scroll
573581
search_kwargs["size"] = size
574582
search_kwargs["request_timeout"] = request_timeout

test_elasticsearch/test_async/test_server/test_helpers.py

+42
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,48 @@ async def test_scan_duplicate_parameters(self, async_client):
809809
params={"__elastic_client_meta": (("h", "s"),)},
810810
)
811811

812+
@pytest.mark.parametrize(
813+
"scan_kwargs",
814+
[
815+
{"from": 1},
816+
{"from_": 1},
817+
{"query": {"from": 1}},
818+
{"query": {"from_": 1}},
819+
{"query": {"query": {"match_all": {}}}, "from": 1},
820+
{"query": {"query": {"match_all": {}}}, "from_": 1},
821+
],
822+
)
823+
async def test_scan_from_keyword_is_aliased(self, async_client, scan_kwargs):
824+
with patch.object(async_client, "search") as search_mock, patch.object(
825+
async_client, "scroll"
826+
) as scroll_mock, patch.object(
827+
async_client, "clear_scroll"
828+
) as clear_scroll_mock:
829+
search_mock.return_value = MockResponse(
830+
{
831+
"_scroll_id": "scroll_id",
832+
"_shards": {"successful": 5, "total": 5, "skipped": 0},
833+
"hits": {"hits": [{"field": "value"}]},
834+
}
835+
)
836+
scroll_mock.return_value = MockResponse(
837+
{
838+
"_scroll_id": "scroll_id",
839+
"_shards": {"successful": 5, "total": 5, "skipped": 0},
840+
"hits": {"hits": []},
841+
}
842+
)
843+
clear_scroll_mock.return_value = MockResponse({"acknowledged": True})
844+
845+
[
846+
x
847+
async for x in helpers.async_scan(
848+
async_client, index="test_index", **scan_kwargs
849+
)
850+
]
851+
assert search_mock.call_args[1]["from_"] == 1
852+
assert "from" not in search_mock.call_args[1]
853+
812854

813855
@pytest.fixture(scope="function")
814856
async def reindex_setup(async_client):

test_elasticsearch/test_server/test_helpers.py

+26
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,29 @@ def test_reindex_index_datastream_op_type_index(self, sync_client):
899899
query={"query": {"bool": {"filter": {"term": {"type": "answers"}}}}},
900900
op_type="_index",
901901
)
902+
903+
904+
@pytest.mark.parametrize(
905+
"scan_kwargs",
906+
[
907+
{"from": 1},
908+
{"from_": 1},
909+
{"query": {"from": 1}},
910+
{"query": {"from_": 1}},
911+
{"query": {"query": {"match_all": {}}}, "from": 1},
912+
{"query": {"query": {"match_all": {}}}, "from_": 1},
913+
],
914+
)
915+
def test_scan_from_keyword_is_aliased(sync_client, scan_kwargs):
916+
with patch.object(
917+
sync_client,
918+
"search",
919+
return_value={
920+
"_scroll_id": "dummy_id",
921+
"_shards": {"successful": 5, "total": 5},
922+
"hits": {"hits": []},
923+
},
924+
) as search_mock, patch.object(sync_client, "clear_scroll"):
925+
list(helpers.scan(sync_client, index="test_index", **scan_kwargs))
926+
assert search_mock.call_args[1]["from_"] == 1
927+
assert "from" not in search_mock.call_args[1]

0 commit comments

Comments
 (0)