Skip to content

Commit 13ea20a

Browse files
authored
Merge pull request #1442 from sanders41/rename-index
Add ability to rename indexes
2 parents 51a7586 + 13df61e commit 13ea20a

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

meilisearch_python_sdk/_client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,11 +812,13 @@ async def health(self) -> Health:
812812

813813
return Health(**response.json())
814814

815-
async def swap_indexes(self, indexes: list[tuple[str, str]]) -> TaskInfo:
815+
async def swap_indexes(self, indexes: list[tuple[str, str]], rename: bool = False) -> TaskInfo:
816816
"""Swap two indexes.
817817
818818
Args:
819819
indexes: A list of tuples, each tuple should contain the indexes to swap.
820+
rename: Use rename false if you are swapping two existing indexes. Use rename true if
821+
the second index in your array does not exist. Default = False
820822
821823
Returns:
822824
The details of the task.
@@ -830,7 +832,10 @@ async def swap_indexes(self, indexes: list[tuple[str, str]]) -> TaskInfo:
830832
>>> async with AsyncClient("http://localhost.com", "masterKey") as client:
831833
>>> index = await client.swap_indexes([("index_a", "index_b")])
832834
"""
833-
processed_indexes = [{"indexes": x} for x in indexes]
835+
if rename:
836+
processed_indexes = [{"indexes": x, "rename": True} for x in indexes]
837+
else:
838+
processed_indexes = [{"indexes": x} for x in indexes]
834839
response = await self._http_requests.post("swap-indexes", processed_indexes)
835840

836841
return TaskInfo(**response.json())
@@ -1775,11 +1780,13 @@ def health(self) -> Health:
17751780

17761781
return Health(**response.json())
17771782

1778-
def swap_indexes(self, indexes: list[tuple[str, str]]) -> TaskInfo:
1783+
def swap_indexes(self, indexes: list[tuple[str, str]], rename: bool = False) -> TaskInfo:
17791784
"""Swap two indexes.
17801785
17811786
Args:
17821787
indexes: A list of tuples, each tuple should contain the indexes to swap.
1788+
rename: Use rename false if you are swapping two existing indexes. Use rename true if
1789+
the second index in your array does not exist. Default = False
17831790
17841791
Returns:
17851792
The details of the task.
@@ -1793,7 +1800,10 @@ def swap_indexes(self, indexes: list[tuple[str, str]]) -> TaskInfo:
17931800
>>> client = Client("http://localhost.com", "masterKey")
17941801
>>> index = client.swap_indexes([("index_a", "index_b")])
17951802
"""
1796-
processed_indexes = [{"indexes": x} for x in indexes]
1803+
if rename:
1804+
processed_indexes = [{"indexes": x, "rename": True} for x in indexes]
1805+
else:
1806+
processed_indexes = [{"indexes": x} for x in indexes]
17971807
response = self._http_requests.post("swap-indexes", processed_indexes)
17981808

17991809
return TaskInfo(**response.json())

tests/test_async_client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,21 @@ async def test_swap_indexes(async_client, async_empty_index):
550550
assert task.task_type == "indexSwap"
551551

552552

553+
@pytest.mark.no_parallel
554+
async def test_swap_indexes_rename(async_client, async_empty_index):
555+
new_name = str(uuid4())
556+
index = await async_empty_index()
557+
task = await index.add_documents([{"id": 1, "title": index.uid}])
558+
await async_client.wait_for_task(task.task_uid)
559+
swapTask = await async_client.swap_indexes([(index.uid, new_name)], rename=True)
560+
task = async_client.wait_for_task(swapTask.task_uid)
561+
562+
indexes = await async_client.get_indexes()
563+
uids = [index.uid for index in indexes]
564+
assert index.uid not in uids
565+
assert new_name in uids
566+
567+
553568
@pytest.mark.usefixtures("create_tasks")
554569
@pytest.mark.no_parallel
555570
async def test_cancel_task_statuses(async_client):

tests/test_client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,21 @@ def test_swap_indexes(client, empty_index):
547547
assert task.task_type == "indexSwap"
548548

549549

550+
@pytest.mark.no_parallel
551+
def test_swap_indexes_rename(client, empty_index):
552+
new_name = str(uuid4())
553+
index = empty_index()
554+
task = index.add_documents([{"id": 1, "title": index.uid}])
555+
client.wait_for_task(task.task_uid)
556+
swapTask = client.swap_indexes([(index.uid, new_name)], rename=True)
557+
task = client.wait_for_task(swapTask.task_uid)
558+
559+
indexes = client.get_indexes()
560+
uids = [index.uid for index in indexes]
561+
assert index.uid not in uids
562+
assert new_name in uids
563+
564+
550565
@pytest.mark.usefixtures("create_tasks")
551566
@pytest.mark.no_parallel
552567
def test_cancel_statuses(client):

0 commit comments

Comments
 (0)