Skip to content

feat(stats): add show_internal_database_sizes and size_format params for Meilisearch v1.44#701

Open
arieleli01212 wants to merge 3 commits into
meilisearch:mainfrom
arieleli01212:feat/stats-v1.44-params
Open

feat(stats): add show_internal_database_sizes and size_format params for Meilisearch v1.44#701
arieleli01212 wants to merge 3 commits into
meilisearch:mainfrom
arieleli01212:feat/stats-v1.44-params

Conversation

@arieleli01212
Copy link
Copy Markdown

@arieleli01212 arieleli01212 commented Jun 1, 2026

Closes #697.

Adds support for the two new query parameters introduced in Meilisearch v1.44.0 for the stats endpoints.

Changes

lib/meilisearch/index.rbstats now accepts optional keyword args:

  • show_internal_database_sizes: — when true, the response includes internalDatabaseSizes (keys subject to change)
  • size_format:'human' returns sizes as strings like "2.3 MiB", 'raw' (default) returns byte counts

lib/meilisearch/client.rb — same optional keyword args on Client#stats

.code-samples.meilisearch.yaml — updated get_index_stats_1 and get_indexes_stats_1

Both methods build the params hash and call .compact to drop nil values, so existing callers with no arguments continue to work unchanged.

Summary

This PR adds support for two new optional query parameters to the stats endpoints, aligning with Meilisearch v1.44.0:

  • show_internal_database_sizes (boolean): when true, responses include an internalDatabaseSizes mapping (keys subject to change; should not be strongly typed).
  • size_format (string: 'human' or 'raw'): controls size formatting — 'human' returns human-readable strings (e.g., "2.3 MiB"); 'raw' returns numeric byte counts (default).

Changes

  • lib/meilisearch/index.rb: Index#stats now accepts keyword args show_internal_database_sizes: nil, size_format: nil; builds a params hash with camel-cased keys (showInternalDatabaseSizes, sizeFormat), compacts nils, and passes them to the /indexes/:uid/stats HTTP GET.
  • lib/meilisearch/client.rb: Client#stats updated similarly to accept the same keyword args and pass them as query params to GET /stats.
  • .code-samples.meilisearch.yaml: Updated examples get_index_stats_1 and get_indexes_stats_1 to call the new methods with show_internal_database_sizes: true, size_format: 'human'.
  • spec/meilisearch/client/stats_spec.rb and spec/meilisearch/index/stats_spec.rb: Added tests covering both new parameters:
    • Verify size_format: 'human' returns size fields as Strings.
    • Verify show_internal_database_sizes: true includes internalDatabaseSizes in responses.
  • .rubocop_todo.yml: Incremented Metrics/ClassLength Max from 541 to 542 to address a RuboCop offense introduced by the change (class length baseline bump).

All changes preserve backward compatibility — omitting the new args leaves behavior unchanged.

Related issue

Closes #697

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 1, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 438a0e12-0d6e-4e71-ac20-8db3f3124be0

📥 Commits

Reviewing files that changed from the base of the PR and between 255418d and 0090621.

📒 Files selected for processing (2)
  • .rubocop_todo.yml
  • lib/meilisearch/index.rb
✅ Files skipped from review due to trivial changes (1)
  • .rubocop_todo.yml
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/meilisearch/index.rb

📝 Walkthrough

Walkthrough

Client#stats and Index#stats now accept optional keyword arguments show_internal_database_sizes and size_format, build a filtered camelCase params hash, and send it with the respective /stats requests. Documentation examples and RSpec tests were updated to exercise the new options.

Changes

Stats API query parameters support

Layer / File(s) Summary
Client and Index stats methods
lib/meilisearch/client.rb, lib/meilisearch/index.rb, .rubocop_todo.yml
Client#stats and Index#stats now accept optional keyword parameters show_internal_database_sizes and size_format, construct a query params hash with camelCase API keys (omitting nil values), and pass it to the stats endpoint calls. A RuboCop class length threshold was incremented by 1.
Documentation examples
.code-samples.meilisearch.yaml
Code sample entries get_index_stats_1 and get_indexes_stats_1 are updated to demonstrate calls with show_internal_database_sizes: true and size_format: 'human'.
Specs for new options
spec/meilisearch/client/stats_spec.rb, spec/meilisearch/index/stats_spec.rb
New RSpec examples verify size_format: 'human' returns human-readable size strings and show_internal_database_sizes: true includes internalDatabaseSizes in responses.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Poem

🐰 I nibble bytes and measure size with cheer,
CamelCase carrots now appear,
Human-readable crumbs I find,
Internal nests revealed in kind,
Docs and tests hop along — hip, hop, hooray!

🚥 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 clearly and concisely summarizes the main change: adding two new query parameters (show_internal_database_sizes and size_format) for Meilisearch v1.44 stats endpoints.
Linked Issues check ✅ Passed All linked issue #697 requirements are fully met: both Index#stats and Client#stats accept the two new parameters with proper camelCase conversion, tests verify the new behavior, and code-samples are updated.
Out of Scope Changes check ✅ Passed All changes are scoped to the linked issue: parameter additions to stats methods, corresponding test coverage, code sample updates, and a necessary RuboCop baseline adjustment due to code length.
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

@StantonMatt StantonMatt left a comment

Choose a reason for hiding this comment

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

I checked this locally. The implementation path looks right: the existing stats specs pass against the Docker-backed Meilisearch service, and direct calls to both Client#stats(show_internal_database_sizes: true, size_format: 'human') and Index#stats(show_internal_database_sizes: true, size_format: 'human') returned human-readable size values plus internalDatabaseSizes.

The main gap I see is test coverage. The linked issue explicitly includes "Add/update test cases for the new/updated methods", but this PR only changes the SDK methods and code samples. I tried a small scratch spec that asserts the new keyword args are sent as showInternalDatabaseSizes / sizeFormat for both /stats and /indexes/:uid/stats, and that the response shape includes the new human-size/internal-size behavior; that passed cleanly.

Validation I ran:

docker compose run --rm -e DISABLE_COVERAGE=1 package bash -c "bundle exec rspec spec/meilisearch/client/stats_spec.rb spec/meilisearch/index/stats_spec.rb"
docker compose run --rm package bash -c "bundle exec rubocop spec/meilisearch/client/stats_spec.rb spec/meilisearch/index/stats_spec.rb"

Both passed after adding the local scratch coverage. I think adding equivalent regression specs to this PR would make it line up with the issue checklist and protect the query-param mapping from future regressions.

@arieleli01212
Copy link
Copy Markdown
Author

Thanks for checking it locally! I've added test coverage for both new parameters:

  • Client#stats(size_format: 'human') — asserts databaseSize comes back as a String
  • Client#stats(show_internal_database_sizes: true) — asserts each index stat object has an internalDatabaseSizes key
  • Index#stats(size_format: 'human') — asserts rawDocumentDbSize is a String
  • Index#stats(show_internal_database_sizes: true) — asserts the internalDatabaseSizes hash is present

All 10 examples in the two stats specs pass against meilisearch-enterprise (verified on v1.45.2 locally).

@StantonMatt
Copy link
Copy Markdown

StantonMatt commented Jun 2, 2026

Rechecked the updated head 255418d with the touched library files included.

The stats coverage itself passes, and the diff is whitespace-clean:

docker compose run --rm -e DISABLE_COVERAGE=1 package bash -c "bundle install && bundle exec rspec spec/meilisearch/client/stats_spec.rb spec/meilisearch/index/stats_spec.rb"
git diff --check origin/main...HEAD

That was 10 stats examples, 0 failures.

One remaining issue: RuboCop fails when the changed library files are included in the scope:

lib/meilisearch/index.rb:11:3: C: Metrics/ClassLength: Class has too many lines. [545/541]

I checked main for the same file and it passes, so this looks introduced by the added Index#stats args. That probably needs either reducing/extracting a few lines in Index or an explicit local exception if the maintainers prefer that style.

Copy link
Copy Markdown

@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)
spec/meilisearch/client/stats_spec.rb (1)

17-21: 💤 Low value

Consider validating the human-readable format pattern.

The test verifies that databaseSize is returned as a String when size_format: 'human' is used, which confirms basic functionality. For more thorough coverage, you could optionally assert that the string matches the expected human-readable pattern (e.g., contains units like "MiB", "GiB").

🧪 Example of stronger assertion
 it 'gets stats with human-readable size format' do
   response = client.stats(size_format: 'human')
   expect(response).to have_key('databaseSize')
   expect(response['databaseSize']).to be_a(String)
+  expect(response['databaseSize']).to match(/\d+(\.\d+)?\s+(B|KiB|MiB|GiB|TiB)/)
 end
🤖 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 `@spec/meilisearch/client/stats_spec.rb` around lines 17 - 21, The test for
client.stats(size_format: 'human') should also assert the returned databaseSize
string matches a human-readable size pattern; update the example spec (the it
block calling client.stats and inspecting response['databaseSize']) to include a
regex-based expectation that the string contains a numeric value and a unit like
B, KiB, MiB, GiB (e.g. /\A\d+(?:\.\d+)?\s?(B|KiB|MiB|GiB|TiB)\z/ or a variant)
so the spec verifies both type and format.
spec/meilisearch/index/stats_spec.rb (1)

12-16: 💤 Low value

Consider validating the human-readable format pattern.

Similar to the client spec, this test confirms that rawDocumentDbSize is a String when size_format: 'human' is used. For additional confidence, you could optionally assert that the string matches the expected human-readable size pattern.

🧪 Example of stronger assertion
 it 'returns stats with human-readable size format' do
   response = index.stats(size_format: 'human')
   expect(response).to have_key('rawDocumentDbSize')
   expect(response['rawDocumentDbSize']).to be_a(String)
+  expect(response['rawDocumentDbSize']).to match(/\d+(\.\d+)?\s+(B|KiB|MiB|GiB|TiB)/)
 end
🤖 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 `@spec/meilisearch/index/stats_spec.rb` around lines 12 - 16, Enhance the test
that calls index.stats(size_format: 'human') by adding an assertion that
response['rawDocumentDbSize'] matches a human-readable size pattern; locate the
spec example with it 'returns stats with human-readable size format' and after
confirming it is a String, add a regex/assertion that validates formats like
"123 B", "1.2 KB", "10MB" (e.g., numeric with optional decimal followed by
optional space and unit B/KB/MB/GB/TB, case-insensitive) to ensure index.stats
returns properly formatted human-readable sizes.
🤖 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 `@spec/meilisearch/client/stats_spec.rb`:
- Around line 23-31: The shared cleanup helper clear_all_indexes in
spec/support/indexes_helpers.rb issues client.delete_index(uid) without awaiting
the returned Models::Task, which allows deletions to run asynchronously and
causes race conditions; update clear_all_indexes to await each deletion (e.g.,
call client.delete_index(uid).await or otherwise wait for task completion) so
all index deletions complete before the next example runs, ensuring tests like
stats_spec that rely on a clean state no longer race.

---

Nitpick comments:
In `@spec/meilisearch/client/stats_spec.rb`:
- Around line 17-21: The test for client.stats(size_format: 'human') should also
assert the returned databaseSize string matches a human-readable size pattern;
update the example spec (the it block calling client.stats and inspecting
response['databaseSize']) to include a regex-based expectation that the string
contains a numeric value and a unit like B, KiB, MiB, GiB (e.g.
/\A\d+(?:\.\d+)?\s?(B|KiB|MiB|GiB|TiB)\z/ or a variant) so the spec verifies
both type and format.

In `@spec/meilisearch/index/stats_spec.rb`:
- Around line 12-16: Enhance the test that calls index.stats(size_format:
'human') by adding an assertion that response['rawDocumentDbSize'] matches a
human-readable size pattern; locate the spec example with it 'returns stats with
human-readable size format' and after confirming it is a String, add a
regex/assertion that validates formats like "123 B", "1.2 KB", "10MB" (e.g.,
numeric with optional decimal followed by optional space and unit B/KB/MB/GB/TB,
case-insensitive) to ensure index.stats returns properly formatted
human-readable sizes.
🪄 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: 0ced3757-d8b3-4b59-afa7-0776f9e0b86f

📥 Commits

Reviewing files that changed from the base of the PR and between f2a58c0 and 255418d.

📒 Files selected for processing (2)
  • spec/meilisearch/client/stats_spec.rb
  • spec/meilisearch/index/stats_spec.rb

Comment on lines +23 to +31
it 'gets stats with internal database sizes' do
client.create_index('books').await
response = client.stats(show_internal_database_sizes: true)

expect(response).to have_key('indexes')
response['indexes'].each_value do |index_stats|
expect(index_stats).to have_key('internalDatabaseSizes')
end
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Look for RSpec cleanup hooks that remove test indexes

# Check for after hooks in this spec file
rg -n -A 3 -B 1 'after\(:(each|all)\)' spec/meilisearch/client/stats_spec.rb

# Check for shared cleanup contexts
rg -n -A 5 'RSpec\.configure.*after' spec/spec_helper.rb spec/support/

# Check for index deletion patterns in spec helpers
rg -n 'delete.*index|clear.*index' spec/spec_helper.rb spec/support/

Repository: meilisearch/meilisearch-ruby

Length of output: 274


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# 1) See where clear_all_indexes is used
rg -n "clear_all_indexes\(" spec | head -n 50

# 2) Inspect stats_spec.rb for hooks / shared contexts
rg -n "shared_context|default_shared_context|include.*default|after\(|before\(" spec/meilisearch/client/stats_spec.rb

# 3) Print stats_spec.rb with line numbers (only the relevant region if short)
wc -l spec/meilisearch/client/stats_spec.rb
cat -n spec/meilisearch/client/stats_spec.rb

# 4) Inspect helper implementations referenced by the initial search
wc -l spec/support/indexes_helpers.rb spec/support/default_shared_context.rb
cat -n spec/support/indexes_helpers.rb
cat -n spec/support/default_shared_context.rb

Repository: meilisearch/meilisearch-ruby

Length of output: 209


🏁 Script executed:

#!/bin/bash
set -u
# Don't fail on missing matches; we need absence info for verification.
set +e

# A) Confirm how stats_spec.rb sets up/tears down the client/indexes
echo "== stats_spec.rb =="
wc -l spec/meilisearch/client/stats_spec.rb
sed -n '1,160p' spec/meilisearch/client/stats_spec.rb

echo
echo "== usages of clear_all_indexes in spec/ =="
rg -n "clear_all_indexes\(" spec | head -n 50

echo
echo "== inclusion of default_shared_context in specs =="
rg -n "default_shared_context" spec | head -n 50
rg -n "include.*default_shared_context|include.*indexes_helpers|include.*shared_context" spec | head -n 50

echo
echo "== after/before hooks in stats_spec.rb =="
rg -n "before\(|after\(|around\(|shared_context" spec/meilisearch/client/stats_spec.rb

echo
echo "== helper implementations =="
wc -l spec/support/indexes_helpers.rb spec/support/default_shared_context.rb
cat -n spec/support/indexes_helpers.rb
cat -n spec/support/default_shared_context.rb

echo
echo "== RSpec configure after hooks =="
rg -n "RSpec\.configure|config\.after|config\.before" spec/spec_helper.rb spec/spec_helper.rb spec/support/ | head -n 80

Repository: meilisearch/meilisearch-ruby

Length of output: 2637


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== find where shared context 'test defaults' is included =="
rg -n "include_context\s+['\"]test defaults['\"]|shared_context\s+['\"]test defaults['\"]" spec | head -n 100

echo
echo "== inspect spec/spec_helper.rb around RSpec.configure =="
sed -n '1,140p' spec/spec_helper.rb
sed -n '140,260p' spec/spec_helper.rb

echo
echo "== confirm whether stats_spec.rb includes default shared context =="
rg -n "include_context|shared_context|default_shared_context" spec/meilisearch/client/stats_spec.rb

echo
echo "== check for global metadata including shared context =="
rg -n "use.*shared_context|before.*include_context|RSpec\.configure" spec/spec_helper.rb spec/support | head -n 200

Repository: meilisearch/meilisearch-ruby

Length of output: 3314


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate delete_index implementation and any usage patterns
rg -n "def delete_index\b" lib spec | head -n 50
rg -n "delete_index\(" lib spec | head -n 50
rg -n "delete_index\(.+\)\.await" lib spec | head -n 50

# Print the delete_index method (first match) with surrounding context
file="$(rg -l "def delete_index\b" lib spec | head -n 1)"
echo "== delete_index file: $file =="
if [ -n "${file:-}" ]; then
  # show a window around the method
  line="$(rg -n "def delete_index\b" "$file" | head -n 1 | cut -d: -f1)"
  start=$((line-20))
  end=$((line+60))
  sed -n "${start},${end}p" "$file"
fi

Repository: meilisearch/meilisearch-ruby

Length of output: 349


🏁 Script executed:

#!/bin/bash
set +e

echo "== delete_index implementation =="
file="lib/meilisearch/client.rb"
line="$(rg -n "def delete_index\b" "$file" | head -n 1 | cut -d: -f1)"
echo "found at line: $line"
start=$((line-20))
end=$((line+80))
sed -n "${start},${end}p" "$file"

echo
echo "== usages of delete_index(...).await =="
rg -n "delete_index\([^)]+\)\.await" lib spec | head -n 50

echo
echo "== usages of delete_index =="
rg -n "delete_index\(" lib spec | head -n 80

echo
echo "== inspect indexes_spec.rb around delete_index usage =="
spec_file="spec/meilisearch/client/indexes_spec.rb"
line2="$(rg -n "task = client\.delete_index" "$spec_file" | head -n 1 | cut -d: -f1)"
echo "found at line: $line2"
start2=$((line2-40))
end2=$((line2+60))
sed -n "${start2},${end2}p" "$spec_file"

Repository: meilisearch/meilisearch-ruby

Length of output: 7628


🏁 Script executed:

#!/bin/bash
set +euo pipefail

# Find clear_all_keys implementation and how it's called
rg -n "def clear_all_keys|clear_all_keys\(" spec/support spec | head -n 50

# Inspect the helper file that defines clear_all_keys (if found)
file="$(rg -l "def clear_all_keys\b" spec/support spec | head -n 1)"
echo "== clear_all_keys file: ${file:-<none>} =="
if [ -n "${file:-}" ]; then
  sed -n '1,200p' "$file"
fi

# Inspect if any cleanup helpers await deletion tasks
rg -n "clear_all_indexes|delete_index\(.+?\)\.await|.delete_index\(.+?\)" spec/support | head -n 80

Repository: meilisearch/meilisearch-ruby

Length of output: 783


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Check delete_key implementation
file="$(rg -l "def delete_key\b" lib/meilisearch/client.rb lib spec | head -n 1)"
echo "== delete_key file: ${file:-<none>} =="
if [ -n "${file:-}" ]; then
  line="$(rg -n "def delete_key\b" "$file" | head -n 1 | cut -d: -f1)"
  start=$((line-20))
  end=$((line+70))
  sed -n "${start},${end}p" "$file"
fi

# Check usage patterns of delete_key(...).await
rg -n "delete_key\([^)]+\)\.await" lib spec | head -n 50
rg -n "delete_key\(" lib spec | head -n 50

# Also check whether delete_index returns an async task (already saw return type in comment)
# but let's inspect the Index#delete used underneath
file2="$(rg -l "def delete\b" lib/meilisearch/index.rb lib/meilisearch -g'*.rb' | head -n 20)"
echo
echo "== candidate delete methods files (showing first few): =="
echo "$file2" | head -n 5

Repository: meilisearch/meilisearch-ruby

Length of output: 3890


Await async index deletion in shared test cleanup to prevent race conditions

spec/meilisearch/client/stats_spec.rb doesn’t clean up the 'books' index itself, but the suite includes the shared context test defaults (via config.include_context 'test defaults') that calls clear_all_indexes(client) before each example. However, clear_all_indexes calls client.delete_index(uid) without awaiting the returned Models::Task, so index deletions may still be in progress when the next example starts. Update spec/support/indexes_helpers.rb to await each delete_index task (e.g., client.delete_index(uid).await) or otherwise ensure cleanup completes before the example runs.

🤖 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 `@spec/meilisearch/client/stats_spec.rb` around lines 23 - 31, The shared
cleanup helper clear_all_indexes in spec/support/indexes_helpers.rb issues
client.delete_index(uid) without awaiting the returned Models::Task, which
allows deletions to run asynchronously and causes race conditions; update
clear_all_indexes to await each deletion (e.g., call
client.delete_index(uid).await or otherwise wait for task completion) so all
index deletions complete before the next example runs, ensuring tests like
stats_spec that rely on a clean state no longer race.

@arieleli01212
Copy link
Copy Markdown
Author

Good catch on the RuboCop Metrics/ClassLength offense. I condensed Index#stats to build the params hash on a single line (within the 120-char limit), which brings the class down to 542 lines, and bumped the Max in .rubocop_todo.yml by 1 (541 → 542) to match — consistent with how that auto-generated baseline already tracks this file.

bundle exec rubocop now reports no offenses on the touched files, and the 10 stats examples still pass.

@StantonMatt
Copy link
Copy Markdown

Rechecked head 0090621. The RuboCop issue I flagged is resolved on this branch now.\n\nValidation passed locally:\n\nbash\ndocker compose run --rm -e DISABLE_COVERAGE=1 package bash -c "bundle install && bundle exec rspec spec/meilisearch/client/stats_spec.rb spec/meilisearch/index/stats_spec.rb && bundle exec rubocop lib/meilisearch/client.rb lib/meilisearch/index.rb spec/meilisearch/client/stats_spec.rb spec/meilisearch/index/stats_spec.rb"\ngit diff --check origin/main...HEAD\n\n\nThat was 10 stats examples, 0 failures, and RuboCop reported no offenses on the touched Ruby files. GitHub currently reports CodeRabbit green; I still do not see another hosted CI result to claim.

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

2 participants