Skip to content

Commit

Permalink
[7.17] Rewrite the 'from' parameter from kwargs/query into 'from_'
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson authored Feb 3, 2022
1 parent 1e01f63 commit bcd6efb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions elasticsearch/_async/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,16 @@ async def async_scan(

# initial search
search_kwargs = kwargs.copy()

# Setting query={"from": ...} would make 'from' be used
# as a keyword argument instead of 'from_'. We handle that here.
if "from" in search_kwargs:
search_kwargs["from_"] = search_kwargs.pop("from")
if query:
search_kwargs.update(query)
if "from" in search_kwargs:
search_kwargs["from_"] = search_kwargs.pop("from")

search_kwargs["scroll"] = scroll
search_kwargs["size"] = size
search_kwargs["request_timeout"] = request_timeout
Expand Down
8 changes: 8 additions & 0 deletions elasticsearch/helpers/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,16 @@ def scan(

# initial search
search_kwargs = kwargs.copy()

# Setting query={"from": ...} would make 'from' be used
# as a keyword argument instead of 'from_'. We handle that here.
if "from" in search_kwargs:
search_kwargs["from_"] = search_kwargs.pop("from")
if query:
search_kwargs.update(query)
if "from" in search_kwargs:
search_kwargs["from_"] = search_kwargs.pop("from")

search_kwargs["scroll"] = scroll
search_kwargs["size"] = size
search_kwargs["request_timeout"] = request_timeout
Expand Down
42 changes: 42 additions & 0 deletions test_elasticsearch/test_async/test_server/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,48 @@ async def test_scan_duplicate_parameters(self, async_client):
params={"__elastic_client_meta": (("h", "s"),)},
)

@pytest.mark.parametrize(
"scan_kwargs",
[
{"from": 1},
{"from_": 1},
{"query": {"from": 1}},
{"query": {"from_": 1}},
{"query": {"query": {"match_all": {}}}, "from": 1},
{"query": {"query": {"match_all": {}}}, "from_": 1},
],
)
async def test_scan_from_keyword_is_aliased(self, async_client, scan_kwargs):
with patch.object(async_client, "search") as search_mock, patch.object(
async_client, "scroll"
) as scroll_mock, patch.object(
async_client, "clear_scroll"
) as clear_scroll_mock:
search_mock.return_value = MockResponse(
{
"_scroll_id": "scroll_id",
"_shards": {"successful": 5, "total": 5, "skipped": 0},
"hits": {"hits": [{"field": "value"}]},
}
)
scroll_mock.return_value = MockResponse(
{
"_scroll_id": "scroll_id",
"_shards": {"successful": 5, "total": 5, "skipped": 0},
"hits": {"hits": []},
}
)
clear_scroll_mock.return_value = MockResponse({"acknowledged": True})

[
x
async for x in helpers.async_scan(
async_client, index="test_index", **scan_kwargs
)
]
assert search_mock.call_args[1]["from_"] == 1
assert "from" not in search_mock.call_args[1]


@pytest.fixture(scope="function")
async def reindex_setup(async_client):
Expand Down
26 changes: 26 additions & 0 deletions test_elasticsearch/test_server/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,29 @@ def test_reindex_index_datastream_op_type_index(self, sync_client):
query={"query": {"bool": {"filter": {"term": {"type": "answers"}}}}},
op_type="_index",
)


@pytest.mark.parametrize(
"scan_kwargs",
[
{"from": 1},
{"from_": 1},
{"query": {"from": 1}},
{"query": {"from_": 1}},
{"query": {"query": {"match_all": {}}}, "from": 1},
{"query": {"query": {"match_all": {}}}, "from_": 1},
],
)
def test_scan_from_keyword_is_aliased(sync_client, scan_kwargs):
with patch.object(
sync_client,
"search",
return_value={
"_scroll_id": "dummy_id",
"_shards": {"successful": 5, "total": 5},
"hits": {"hits": []},
},
) as search_mock, patch.object(sync_client, "clear_scroll"):
list(helpers.scan(sync_client, index="test_index", **scan_kwargs))
assert search_mock.call_args[1]["from_"] == 1
assert "from" not in search_mock.call_args[1]

0 comments on commit bcd6efb

Please sign in to comment.