Skip to content

Support batches API #1062

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 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,7 @@ multi_search_federated_1: |-
[{"indexUid": "movies", "q": "batman"}, {"indexUid": "comics", "q": "batman"}],
{}
)
get_all_batches_1: |-
client.get_batches()
get_batch_1: |-
client.get_batch(BATCH_UID)
42 changes: 41 additions & 1 deletion meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from meilisearch.errors import MeilisearchError
from meilisearch.index import Index
from meilisearch.models.key import Key, KeysResults
from meilisearch.models.task import Task, TaskInfo, TaskResults
from meilisearch.models.task import Batch, BatchResults, Task, TaskInfo, TaskResults
from meilisearch.task import TaskHandler


Expand Down Expand Up @@ -611,6 +611,46 @@ def wait_for_task(
"""
return self.task_handler.wait_for_task(uid, timeout_in_ms, interval_in_ms)

def get_batches(self, parameters: Optional[MutableMapping[str, Any]] = None) -> BatchResults:
"""Get all batches.

Parameters
----------
parameters (optional):
parameters accepted by the get batches route: https://www.meilisearch.com/docs/reference/api/batches#get-batches.

Returns
-------
batch:
BatchResult instance containing limit, from, next and results containing a list of all batches.

Raises
------
MeilisearchApiError
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
"""
return self.task_handler.get_batches(parameters=parameters)

def get_batch(self, uid: int) -> Batch:
"""Get one tasks batch.

Parameters
----------
uid:
Identifier of the batch.

Returns
-------
batch:
Batch instance containing information about the progress of the asynchronous batch.

Raises
------
MeilisearchApiError
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
"""
return self.task_handler.get_batch(uid)

def generate_tenant_token(
self,
api_key_uid: str,
Expand Down
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Paths:
version = "version"
index = "indexes"
task = "tasks"
batch = "batches"
stat = "stats"
search = "search"
facet_search = "facet-search"
Expand Down
43 changes: 43 additions & 0 deletions meilisearch/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,46 @@ def __init__(self, resp: Dict[str, Any]) -> None:
self.total: int = resp["total"]
self.from_: int = resp["from"]
self.next_: int = resp["next"]


class Batch(CamelBase):
uid: int
details: Optional[Dict[str, Any]] = None
stats: Optional[Dict[str, Union[int, Dict[str, Any]]]] = None
duration: Optional[str] = None
started_at: Optional[datetime] = None
finished_at: Optional[datetime] = None
progress: Optional[Dict[str, Union[float, List[Dict[str, Any]]]]] = None

if is_pydantic_2():

@pydantic.field_validator("started_at", mode="before") # type: ignore[attr-defined]
@classmethod
def validate_started_at(cls, v: str) -> Optional[datetime]: # pylint: disable=invalid-name
return iso_to_date_time(v)

@pydantic.field_validator("finished_at", mode="before") # type: ignore[attr-defined]
@classmethod
def validate_finished_at(cls, v: str) -> Optional[datetime]: # pylint: disable=invalid-name
return iso_to_date_time(v)

else: # pragma: no cover

@pydantic.validator("started_at", pre=True)
@classmethod
def validate_started_at(cls, v: str) -> Optional[datetime]: # pylint: disable=invalid-name
return iso_to_date_time(v)

@pydantic.validator("finished_at", pre=True)
@classmethod
def validate_finished_at(cls, v: str) -> Optional[datetime]: # pylint: disable=invalid-name
return iso_to_date_time(v)


class BatchResults(CamelBase):
results: List[Batch]
total: int
limit: int
from_: int
# None means last page
next_: Optional[int]
49 changes: 48 additions & 1 deletion meilisearch/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from meilisearch._httprequests import HttpRequests
from meilisearch.config import Config
from meilisearch.errors import MeilisearchTimeoutError
from meilisearch.models.task import Task, TaskInfo, TaskResults
from meilisearch.models.task import Batch, BatchResults, Task, TaskInfo, TaskResults


class TaskHandler:
Expand All @@ -27,6 +27,53 @@ def __init__(self, config: Config):
self.config = config
self.http = HttpRequests(config)

def get_batches(self, parameters: Optional[MutableMapping[str, Any]] = None) -> BatchResults:
"""Get all task batches.

Parameters
----------
parameters (optional):
parameters accepted by the get batches route: https://www.meilisearch.com/docs/reference/api/batches#get-batches.

Returns
-------
batch:
BatchResults instance contining limit, from, next and results containing a list of all batches.

Raises
------
MeilisearchApiError
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
"""
if parameters is None:
parameters = {}
for param in parameters:
if isinstance(parameters[param], (list, tuple)):
parameters[param] = ",".join(parameters[param])
batches = self.http.get(f"{self.config.paths.batch}?{parse.urlencode(parameters)}")
return BatchResults(**batches)

def get_batch(self, uid: int) -> Batch:
"""Get one tasks batch.

Parameters
----------
uid:
Identifier of the batch.

Returns
-------
task:
Batch instance containing information about the progress of the asynchronous batch.

Raises
------
MeilisearchApiError
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
"""
batch = self.http.get(f"{self.config.paths.batch}/{uid}")
return Batch(**batch)

def get_tasks(self, parameters: Optional[MutableMapping[str, Any]] = None) -> TaskResults:
"""Get all tasks.

Expand Down
24 changes: 24 additions & 0 deletions tests/client/test_client_task_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,27 @@ def test_get_tasks_in_reverse(client):
reverse_tasks = client.get_tasks({"reverse": "true"})

assert reverse_tasks.results[0] == tasks.results[-1]


def test_get_batches_default(client):
"""Tests getting the batches."""
batches = client.get_batches()
assert len(batches.results) >= 1


@pytest.mark.usefixtures("create_tasks")
def test_get_batches_with_parameters(client):
"""Tests getting batches with a parameter (empty or otherwise)."""
rev_batches = client.get_batches({"reverse": "true"})
batches = client.get_batches({})

assert len(batches.results) > 1
assert rev_batches.results[0].uid == batches.results[-1].uid


def test_get_batch(client):
"""Tests getting the details of a batch."""
batches = client.get_batches({"limit": 1})
uid = batches.results[0].uid
batch = client.get_batch(uid)
assert batch.uid == uid
Loading