Skip to content

Add showInternalDatabaseSizes and sizeFormat params to stats endpoints#1235

Open
thakoreh wants to merge 1 commit into
meilisearch:mainfrom
thakoreh:add-stats-query-params
Open

Add showInternalDatabaseSizes and sizeFormat params to stats endpoints#1235
thakoreh wants to merge 1 commit into
meilisearch:mainfrom
thakoreh:add-stats-query-params

Conversation

@thakoreh
Copy link
Copy Markdown

@thakoreh thakoreh commented May 26, 2026

Related issue

Fixes #1234

What does this PR do?

  • Adds showInternalDatabaseSizes and sizeFormat optional keyword-only parameters to Client.get_all_stats() and Index.get_stats()
  • Updates IndexStats model to include optional internal_database_sizes field
  • Adds test cases for the new parameters
  • Updates .code-samples.meilisearch.yaml with examples for get_index_stats_1 and get_indexes_stats_1

PR checklist

  • Add/update methods to handle interacting with the stats API endpoints
  • Add/update test cases for the new/updated methods
  • Update .code-samples.meilisearch.yaml

Note: AI assistance was used (GitHub Copilot) for autocompletion during development.

Overview

This PR adds support for two new query parameters introduced in Meilisearch v1.44.0 to the stats endpoints: showInternalDatabaseSizes and sizeFormat. These parameters allow clients to request internal database size breakdowns and human-readable size formatting.

Changes

API Updates

  • Client.get_all_stats(): Added optional keyword-only parameters:

    • show_internal_database_sizes: Optional[bool] - includes internal database sizes in the response
    • size_format: Optional[str] - specifies "human" for readable sizes or "raw" for byte counts
  • Index.get_stats(): Added the same optional keyword-only parameters with matching behavior

Model Updates

  • IndexStats: Added optional internal_database_sizes: Optional[Dict[str, Any]] field to represent dynamically-keyed internal database size mappings

Tests

  • Added test_get_all_stats_with_params() to verify client stats calls with query parameters
  • Added multiple test cases to cover:
    • show_internal_database_sizes parameter
    • size_format parameter
    • Combined parameter usage
  • All new tests verify proper response structure and validate that internal database sizes are returned as dictionaries when present

Documentation

  • Updated .code-samples.meilisearch.yaml with examples for get_index_stats_1 and get_indexes_stats_1 demonstrating the new parameters (show_internal_database_sizes=True, size_format='human')

Related Issues

Fixes #1234: Add human-formatted sizes and detailed DB sizes in stats

Notes

AI assistance (GitHub Copilot) was used for autocompletion during development.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 26, 2026

📝 Walkthrough

Walkthrough

This PR adds support for two new optional query parameters to the Meilisearch Python SDK's stats methods, aligning with v1.44.0 API changes: show_internal_database_sizes and size_format. The IndexStats model is extended with an internal_database_sizes field, both Index.get_stats() and Client.get_all_stats() methods are updated to accept and encode these parameters, and comprehensive tests and code samples are added.

Changes

Stats Query Parameters Enhancement

Layer / File(s) Summary
IndexStats data model update
meilisearch/models/index.py
IndexStats gains an optional internal_database_sizes: Optional[Dict[str, Any]] field to hold additional database sizing data returned by the API when requested.
Index.get_stats method with query parameters
meilisearch/index.py, tests/index/test_index_stats_meilisearch.py
Index.get_stats now accepts optional keyword-only arguments show_internal_database_sizes and size_format to control response format; implementation conditionally encodes these into query parameters; new tests validate all parameter combinations and response structure.
Client.get_all_stats method with query parameters
meilisearch/client.py, tests/client/test_client_stats_meilisearch.py
Client.get_all_stats now accepts optional keyword-only arguments show_internal_database_sizes and size_format; implementation conditionally encodes these into query parameters; new test verifies parameterized requests and response structure including conditional internalDatabaseSizes presence.
Documentation code samples update
.code-samples.meilisearch.yaml
Code examples for get_stats and get_all_stats are updated to demonstrate the new query parameters in action.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

Poem

🐰 Stats now bloom with options bright,
show_internal_database_sizes in sight,
size_format makes humans smile,
Parameters added, feature complete—
The rabbit's work is ever sweet! 🌟

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding two query parameters (showInternalDatabaseSizes and sizeFormat) to the stats endpoint methods.
Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #1234: adds the two query parameters to Client.get_all_stats() and Index.get_stats(), updates IndexStats model with internal_database_sizes field, adds comprehensive tests, and updates code samples.
Out of Scope Changes check ✅ Passed All changes directly relate to implementing support for the new Meilisearch v1.44.0 stats query parameters specified in issue #1234; no out-of-scope modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
tests/index/test_index_stats_meilisearch.py (1)

20-40: ⚡ Quick win

Strengthen assertions so these tests actually validate the new query params.

Right now, the new tests still pass if params are dropped. Add assertions on internal_database_sizes and format-sensitive value types.

Suggested test tightening
 def test_get_stats_with_internal_database_sizes(empty_index):
     """Tests getting stats with showInternalDatabaseSizes parameter."""
     response = empty_index().get_stats(show_internal_database_sizes=True)
     assert isinstance(response, IndexStats)
     assert response.number_of_documents == 0
+    assert response.internal_database_sizes is not None
+    assert isinstance(response.internal_database_sizes, dict)
 
@@
 def test_get_stats_with_all_params(empty_index):
@@
     assert isinstance(response, IndexStats)
     assert response.number_of_documents == 0
+    assert response.internal_database_sizes is not None
+    assert isinstance(response.internal_database_sizes, dict)
+    assert all(isinstance(v, str) for v in response.internal_database_sizes.values())
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/index/test_index_stats_meilisearch.py` around lines 20 - 40, The tests
currently only assert number_of_documents and don't validate the new query
params; update test_get_stats_with_internal_database_sizes to assert that
calling empty_index().get_stats(show_internal_database_sizes=True) returns
response.internal_database_sizes present and of the expected type (e.g., a
dict/mapping and contains expected keys like "main" or at least non-empty),
update test_get_stats_with_size_format to call get_stats(size_format="human")
and assert that size-related fields (e.g., response.index_size,
response.total_size or whichever size properties exist on IndexStats) are
human-readable strings (type str and match a simple pattern like ending with "B"
or containing space), and update test_get_stats_with_all_params to assert both
that internal_database_sizes is present and that size fields are
string-formatted when both show_internal_database_sizes=True and
size_format="human".
tests/client/test_client_stats_meilisearch.py (1)

20-26: ⚡ Quick win

Make this test fail when the new params are not honored.

The current conditional check allows a full pass even when internalDatabaseSizes is absent. Assert presence for at least one index and validate human formatting output.

Suggested assertion update
 def test_get_all_stats_with_params(client):
@@
     assert "databaseSize" in response
     assert "indexes" in response
-    for index_stats in response["indexes"].values():
-        if "internalDatabaseSizes" in index_stats:
-            assert isinstance(index_stats["internalDatabaseSizes"], dict)
+    assert isinstance(response["databaseSize"], str)
+    assert any("internalDatabaseSizes" in s for s in response["indexes"].values())
+    for index_stats in response["indexes"].values():
+        if "internalDatabaseSizes" in index_stats:
+            assert isinstance(index_stats["internalDatabaseSizes"], dict)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/client/test_client_stats_meilisearch.py` around lines 20 - 26, Update
the test so it fails when the new parameters are not honored by asserting that
at least one index contains the "internalDatabaseSizes" key and that those sizes
are returned in human-readable string format; specifically, after calling
client.get_all_stats(show_internal_database_sizes=True, size_format="human") and
getting response, assert that any(index_stats for index_stats in
response["indexes"].values() if "internalDatabaseSizes" in index_stats) is True
and additionally validate that the values in that internalDatabaseSizes dict are
strings matching human-size formatting (e.g., end with "B", "KB", "MB", etc.)
using the existing response and index_stats variables.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@meilisearch/client.py`:
- Around line 356-364: The boolean query param show_internal_database_sizes is
being placed into params and encoded with urllib.parse.urlencode which yields
Python-style "True"/"False"; change the assignment in the code that builds
params so that when show_internal_database_sizes is a bool you store a lowercase
JSON-style string ("true" or "false") instead (e.g., set
params["showInternalDatabaseSizes"] = str(show_internal_database_sizes).lower()
when isinstance(show_internal_database_sizes, bool)), leaving size_format
handling and the rest of the path construction (self.config.paths.stat,
parse.urlencode, and the final self.http.get call) unchanged.

---

Nitpick comments:
In `@tests/client/test_client_stats_meilisearch.py`:
- Around line 20-26: Update the test so it fails when the new parameters are not
honored by asserting that at least one index contains the
"internalDatabaseSizes" key and that those sizes are returned in human-readable
string format; specifically, after calling
client.get_all_stats(show_internal_database_sizes=True, size_format="human") and
getting response, assert that any(index_stats for index_stats in
response["indexes"].values() if "internalDatabaseSizes" in index_stats) is True
and additionally validate that the values in that internalDatabaseSizes dict are
strings matching human-size formatting (e.g., end with "B", "KB", "MB", etc.)
using the existing response and index_stats variables.

In `@tests/index/test_index_stats_meilisearch.py`:
- Around line 20-40: The tests currently only assert number_of_documents and
don't validate the new query params; update
test_get_stats_with_internal_database_sizes to assert that calling
empty_index().get_stats(show_internal_database_sizes=True) returns
response.internal_database_sizes present and of the expected type (e.g., a
dict/mapping and contains expected keys like "main" or at least non-empty),
update test_get_stats_with_size_format to call get_stats(size_format="human")
and assert that size-related fields (e.g., response.index_size,
response.total_size or whichever size properties exist on IndexStats) are
human-readable strings (type str and match a simple pattern like ending with "B"
or containing space), and update test_get_stats_with_all_params to assert both
that internal_database_sizes is present and that size fields are
string-formatted when both show_internal_database_sizes=True and
size_format="human".
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e27ee6ff-6383-422e-8897-c57d80f64af2

📥 Commits

Reviewing files that changed from the base of the PR and between ada25db and f0c0403.

📒 Files selected for processing (6)
  • .code-samples.meilisearch.yaml
  • meilisearch/client.py
  • meilisearch/index.py
  • meilisearch/models/index.py
  • tests/client/test_client_stats_meilisearch.py
  • tests/index/test_index_stats_meilisearch.py

Comment thread meilisearch/client.py
Comment on lines +356 to +364
if show_internal_database_sizes is not None:
params["showInternalDatabaseSizes"] = show_internal_database_sizes
if size_format is not None:
params["sizeFormat"] = size_format

path = self.config.paths.stat
if params:
path = f"{path}?{parse.urlencode(params)}"
return self.http.get(path)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
python - <<'PY'
from urllib import parse
print(parse.urlencode({"showInternalDatabaseSizes": True}))
print(parse.urlencode({"showInternalDatabaseSizes": False}))
PY

Repository: meilisearch/meilisearch-python

Length of output: 137


Fix boolean query serialization casing for showInternalDatabaseSizes.
urllib.parse.urlencode serializes Python bool values as True/False (capitalized) in the query string (e.g., showInternalDatabaseSizes=True), so normalize to lowercase to avoid API parsing mismatches.

Proposed fix
         params: Dict[str, Any] = {}
         if show_internal_database_sizes is not None:
-            params["showInternalDatabaseSizes"] = show_internal_database_sizes
+            params["showInternalDatabaseSizes"] = str(show_internal_database_sizes).lower()
         if size_format is not None:
             params["sizeFormat"] = size_format
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@meilisearch/client.py` around lines 356 - 364, The boolean query param
show_internal_database_sizes is being placed into params and encoded with
urllib.parse.urlencode which yields Python-style "True"/"False"; change the
assignment in the code that builds params so that when
show_internal_database_sizes is a bool you store a lowercase JSON-style string
("true" or "false") instead (e.g., set params["showInternalDatabaseSizes"] =
str(show_internal_database_sizes).lower() when
isinstance(show_internal_database_sizes, bool)), leaving size_format handling
and the rest of the path construction (self.config.paths.stat, parse.urlencode,
and the final self.http.get call) unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Meilisearch v1.44.0] Add human-formatted sizes and detailed DB sizes in stats

1 participant