Skip to content
Closed
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
25 changes: 23 additions & 2 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,25 @@ def update_documents_by_function(
body=dict(queries),
)

def get_all_stats(self) -> dict[str, Any]:
def get_all_stats(
self,
show_internal_database_sizes: bool = False,
size_format: str | None = None,
) -> dict[str, Any]:
"""Get all stats of Meilisearch

Get information about database size and all indexes
https://www.meilisearch.com/docs/reference/api/stats

Parameters
----------
show_internal_database_sizes (optional):
When True, each index stat object also contains an ``internalDatabaseSizes``
dictionary of internal database names and their current size.
size_format (optional):
"human" to return database sizes as strings with a unit (MiB, GiB, ...),
or "raw" (the default) to return them as byte counts.

Returns
-------
stats:
Expand All @@ -362,7 +375,15 @@ def get_all_stats(self) -> dict[str, Any]:
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.http.get(self.config.paths.stat)
parameters: dict[str, str] = {}
if show_internal_database_sizes:
parameters["showInternalDatabaseSizes"] = "true"
if size_format is not None:
parameters["sizeFormat"] = size_format
url = self.config.paths.stat
if parameters:
url += f"?{parse.urlencode(parameters)}"
return self.http.get(url)

def health(self) -> dict[str, str]:
"""Get health of the Meilisearch server.
Expand Down
25 changes: 23 additions & 2 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,25 @@ def wait_for_task(
"""
return self.task_handler.wait_for_task(uid, timeout_in_ms, interval_in_ms)

def get_stats(self) -> IndexStats:
def get_stats(
self,
show_internal_database_sizes: bool = False,
size_format: str | None = None,
) -> IndexStats:
"""Get stats of the index.

Get information about the number of documents, field frequencies, ...
https://www.meilisearch.com/docs/reference/api/stats

Parameters
----------
show_internal_database_sizes (optional):
When True, the returned stats also contain an ``internal_database_sizes``
dictionary of internal database names and their current size.
size_format (optional):
"human" to return database sizes as strings with a unit (MiB, GiB, ...),
or "raw" (the default) to return them as byte counts.

Returns
-------
stats:
Expand All @@ -320,7 +333,15 @@ def get_stats(self) -> IndexStats:
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
"""
stats = self.http.get(f"{self.config.paths.index}/{self.uid}/{self.config.paths.stat}")
parameters: dict[str, str] = {}
if show_internal_database_sizes:
parameters["showInternalDatabaseSizes"] = "true"
if size_format is not None:
parameters["sizeFormat"] = size_format
url = f"{self.config.paths.index}/{self.uid}/{self.config.paths.stat}"
if parameters:
url += f"?{parse.urlencode(parameters)}"
stats = self.http.get(url)
return IndexStats(**stats)

@version_error_hint_message
Expand Down
2 changes: 2 additions & 0 deletions meilisearch/models/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class IndexStats(CamelBase):
number_of_documents: int
is_indexing: bool
field_distribution: FieldDistribution
raw_database_size: int | None = None
internal_database_sizes: dict[str, Any] | None = None

@field_validator("field_distribution", mode="before")
@classmethod
Expand Down
16 changes: 16 additions & 0 deletions tests/client/test_client_stats_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ def test_get_all_stats(client):
assert "indexes" in response
assert "indexUID" in response["indexes"]
assert "indexUID2" in response["indexes"]


@pytest.mark.usefixtures("indexes_sample")
def test_get_all_stats_human_size_format(client):
"""Tests getting all stats with human-formatted sizes."""
response = client.get_all_stats(size_format="human")
assert isinstance(response, dict)
assert isinstance(response["databaseSize"], str)


@pytest.mark.usefixtures("indexes_sample")
def test_get_all_stats_internal_database_sizes(client):
"""Tests getting all stats with internal database sizes."""
response = client.get_all_stats(show_internal_database_sizes=True)
assert isinstance(response, dict)
assert "internalDatabaseSizes" in response["indexes"]["indexUID"]
8 changes: 8 additions & 0 deletions tests/index/test_index_stats_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ def test_get_stats_default(index_with_documents):
assert response.number_of_documents == 31
assert hasattr(response.field_distribution, "genre")
assert response.field_distribution.genre == 11


def test_get_stats_internal_database_sizes(index_with_documents):
"""Tests exposing internal database sizes."""
response = index_with_documents().get_stats(show_internal_database_sizes=True)
assert isinstance(response, IndexStats)
assert isinstance(response.internal_database_sizes, dict)
assert response.internal_database_sizes