Skip to content

Add swap indexes feature for v0.30.0 #603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from meilisearch.config import Config
from meilisearch.errors import MeiliSearchError
from meilisearch.index import Index
from meilisearch.models.task import TaskInfo
from meilisearch.task import get_task, get_tasks, wait_for_task


Expand Down Expand Up @@ -399,6 +400,27 @@ def create_dump(self) -> dict[str, str]:
"""
return self.http.post(self.config.paths.dumps)

def swap_indexes(self, parameters: list[dict[str, list[str]]]) -> TaskInfo:
"""Swap two indexes.

Parameters
----------
indexes:
List of indexes to swap (ex: [{"indexes": ["indexA", "indexB"]}).

Returns
-------
task_info:
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task

Raises
------
MeiliSearchApiError
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
"""
return TaskInfo(**self.http.post(self.config.paths.swap, parameters))

def get_tasks(
self, parameters: dict[str, Any] | None = None
) -> dict[str, list[dict[str, Any]]]:
Expand Down
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Paths:
dumps = "dumps"
pagination = "pagination"
faceting = "faceting"
swap = "swap-indexes"

def __init__(self, url: str, api_key: str | None = None, timeout: int | None = None) -> None:
"""
Expand Down
59 changes: 59 additions & 0 deletions tests/client/test_client_swap_meilisearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# pylint: disable=invalid-name

import pytest

from meilisearch.errors import MeiliSearchApiError


def test_swap_indexes(client, empty_index):
"""Tests swap two indexes."""
indexA = empty_index("index_A")
indexB = empty_index("index_B")
taskA = indexA.add_documents([{"id": 1, "title": "index_A"}])
taskB = indexB.add_documents([{"id": 1, "title": "index_B"}])
client.wait_for_task(taskA.task_uid)
client.wait_for_task(taskB.task_uid)
swapTask = client.swap_indexes(
[
{
"indexes": [indexA.uid, indexB.uid],
},
]
)
task = client.wait_for_task(swapTask.task_uid)
docA = client.index(indexA.uid).get_document(1)
docB = client.index(indexB.uid).get_document(1)

assert docA.title == indexB.uid
assert docB.title == indexA.uid
assert task["type"] == "indexSwap"
assert "swaps" in task["details"]


def test_swap_indexes_with_one_that_does_not_exist(client, empty_index):
"""Tests swap indexes with one that does not exist."""
index = empty_index("index_A")
swapTask = client.swap_indexes(
[
{
"indexes": [index.uid, "does_not_exist"],
},
]
)
task = client.wait_for_task(swapTask.task_uid)

assert swapTask.type == "indexSwap"
assert task["error"]["code"] == "index_not_found"


def test_swap_indexes_with_itself(client, empty_index):
"""Tests swap indexes with itself."""
index = empty_index()
with pytest.raises(MeiliSearchApiError):
client.swap_indexes(
[
{
"indexes": [index.uid, index.uid],
},
]
)