Skip to content

Commit b50209a

Browse files
committed
feat: Adding support for index renaming.
1 parent 2aa6057 commit b50209a

File tree

3 files changed

+73
-17
lines changed

3 files changed

+73
-17
lines changed

meilisearch/client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,30 @@ def create_snapshot(self) -> TaskInfo:
531531

532532
return TaskInfo(**task)
533533

534+
def swap_indexes(self, parameters: List[Mapping[str, List[str]]]) -> TaskInfo:
535+
"""Swap two indexes.
536+
537+
Parameters
538+
----------
539+
indexes:
540+
List of indexes to swap ex:
541+
[{"indexes": ["indexA", "indexB"]}) # default rename to false
542+
{"indexes": ["indexA", "indexB"], "rename": false}
543+
{"indexes": ["indexA", "indexB"], "rename": true}
544+
545+
Returns
546+
-------
547+
task_info:
548+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
549+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
550+
551+
Raises
552+
------
553+
MeilisearchApiError
554+
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
555+
"""
556+
return TaskInfo(**self.http.post(self.config.paths.swap, parameters))
557+
534558
def get_tasks(self, parameters: Optional[MutableMapping[str, Any]] = None) -> TaskResults:
535559
"""Get all tasks.
536560

meilisearch/index.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ def delete(self) -> TaskInfo:
104104

105105
return TaskInfo(**task)
106106

107-
def update(self, primary_key: str) -> TaskInfo:
107+
def update(self, primary_key: Optional[str] = None, new_uid: Optional[str] = None) -> TaskInfo:
108108
"""Update the index primary-key.
109109
110110
Parameters
111111
----------
112112
primary_key:
113113
The primary key to use for the index.
114+
new_uid : str, optional
115+
The new UID to rename the index.
114116
115117
Returns
116118
-------
@@ -123,7 +125,13 @@ def update(self, primary_key: str) -> TaskInfo:
123125
MeilisearchApiError
124126
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
125127
"""
126-
payload = {"primaryKey": primary_key}
128+
payload = {}
129+
if primary_key is not None:
130+
payload["primaryKey"] = primary_key
131+
132+
if new_uid is not None:
133+
payload["uid"] = new_uid # This enables renaming
134+
127135
task = self.http.patch(f"{self.config.paths.index}/{self.uid}", payload)
128136

129137
return TaskInfo(**task)
@@ -2348,18 +2356,3 @@ def compact(self) -> TaskInfo:
23482356
task = self.http.post(path)
23492357
return TaskInfo(**task)
23502358

2351-
def rename_index(self, new_name: str) -> TaskInfo:
2352-
"""
2353-
Rename the current Meilisearch index.
2354-
2355-
:param new_name: The new UID for the index.
2356-
:return: TaskInfo with information about the rename operation.
2357-
"""
2358-
payload = {"uid": new_name}
2359-
2360-
task = self.http.patch(
2361-
f"{self.config.paths.index}/{self.uid}",
2362-
payload,
2363-
)
2364-
2365-
return TaskInfo(**task)

tests/index/test_index.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,42 @@ def test_index_compact(client):
234234

235235
assert stats_before.number_of_documents == stats_after.number_of_documents
236236
assert stats_after.is_indexing is False
237+
238+
239+
@pytest.mark.usefixtures("indexes_sample")
240+
def test_rename_index(client):
241+
"""Test renaming an existing index."""
242+
original_uid = common.INDEX_UID
243+
new_uid = f"{original_uid}_renamed"
244+
index = client.index(original_uid)
245+
246+
# Perform the rename
247+
task_info = index.update(new_uid=new_uid)
248+
client.wait_for_task(task_info.task_uid)
249+
250+
# Verify the index now exists with the new UID
251+
renamed_index = client.index(new_uid)
252+
info = renamed_index.fetch_info()
253+
assert info.uid == new_uid
254+
255+
# # Verify the old UID no longer exists
256+
with pytest.raises(MeilisearchApiError):
257+
client.index(original_uid).fetch_info() # Assert old UID is gone
258+
259+
260+
@pytest.mark.usefixtures("indexes_sample")
261+
def test_index_update_and_rename(client):
262+
"""Test updating primary key and renaming an index together."""
263+
original_uid = common.INDEX_UID
264+
new_uid = f"{original_uid}_renamed"
265+
index = client.index(original_uid)
266+
267+
# 1. Update the primary key
268+
task_info = index.update(primary_key="objectID", new_uid=new_uid)
269+
client.wait_for_task(task_info.task_uid)
270+
271+
# Verify the index now exists with the new UID
272+
renamed_index = client.index(new_uid)
273+
info = renamed_index.fetch_info()
274+
assert info.uid == new_uid
275+
assert renamed_index.get_primary_key() == "objectID"

0 commit comments

Comments
 (0)