Skip to content

fix: scope Index::refresh() to the target index (#2313)#2315

Open
ruflin wants to merge 2 commits into
9.xfrom
fix/index-refresh-scope-2313
Open

fix: scope Index::refresh() to the target index (#2313)#2315
ruflin wants to merge 2 commits into
9.xfrom
fix/index-refresh-scope-2313

Conversation

@ruflin

@ruflin ruflin commented May 21, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #2313.

Elastica\Index::refresh() was calling indices()->refresh() on the underlying elastic/elasticsearch client without passing the index parameter. That maps to POST /_refresh — a cluster-wide refresh — instead of the expected POST /{index}/_refresh.

That is surprising for an instance method on Elastica\Index, especially because every neighboring method (delete(), exists(), open(), close(), setSettings(), analyze(), forcemerge(), …) does pass ['index' => $this->getName()]. In practice, code like:

```php
$client->getIndex('my-index')->refresh();
```

was silently refreshing every index in the cluster on every call, which is expensive on shared/large clusters.

Change

  • src/Index.php: pass ['index' => \$this->getName()] to indices()->refresh() so the request is correctly scoped to the current index.
  • Client::refreshAll() is unchanged — it is the explicit cluster-wide refresh and still calls indices()->refresh() without an index, mapping to POST /_refresh.

Backward compatibility

This is a bug fix. Anyone who was actually relying on \$index->refresh() to refresh the whole cluster (almost certainly nobody, given the method name and surrounding API) can call \$client->refreshAll() instead, which has been the dedicated API for that since at least 8.x.

Test plan

  • Added a functional test IndexTest::testRefreshTargetsIndexAndNotCluster that asserts \$client->getLastRequest()->getUri()->getPath() equals /{index}/_refresh after calling \$index->refresh() (same pattern used by ClientFunctionalTest::testLastRequestResponse).
  • Verified unit test group still passes locally (vendor/bin/phpunit --group unit --filter IndexTest).
  • Verified phpstan is clean on the changed files.
  • CI runs the functional suite against Elasticsearch.

Changelog

Added an entry under `### Fixed` in the `Unreleased` section of `CHANGELOG.md` referencing #2313.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed the refresh behavior so it affects only the intended index instead of refreshing the entire cluster.
  • Tests

    • Added a functional test that verifies the refresh request targets the specific index and not the cluster-level endpoint.

Review Change Stack

Index::refresh() was calling indices()->refresh() without passing the
index name, which the underlying elasticsearch-php client maps to
`POST /_refresh` (a cluster-wide refresh) instead of the expected
`POST /{index}/_refresh`. This caused calls like
`$client->getIndex('my-index')->refresh()` to silently refresh every
index in the cluster, hurting performance on shared clusters.

Pass `['index' => $this->getName()]` so the request is correctly
scoped to the current index, matching the behavior of neighboring
methods (delete, exists, open, close, setSettings, analyze).

Client::refreshAll() is unchanged and remains the explicit
cluster-wide refresh.

Refs #2313

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes Elastica\Index::refresh() so it refreshes only the current index (calling POST /{index}/_refresh) instead of performing an unintended cluster-wide refresh (POST /_refresh), aligning the method behavior with the surrounding Index API and the expectation from issue #2313.

Changes:

  • Scope Index::refresh() to the index by passing the index parameter to the underlying Elasticsearch client call.
  • Add a functional test asserting the last request path is /{index}/_refresh after calling $index->refresh().
  • Add a changelog entry documenting the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Index.php Passes ['index' => $this->getName()] to ensure refresh targets only the current index.
tests/IndexTest.php Adds a functional regression test verifying the refresh request path is index-scoped.
CHANGELOG.md Documents the behavior change under “Fixed” for Unreleased.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/IndexTest.php
Comment on lines +1042 to +1044
$lastRequest = $client->getLastRequest();
$this->assertNotNull($lastRequest);
$this->assertSame(
@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5b269d8c-9cb4-4be1-a272-6a20ff91dc06

📥 Commits

Reviewing files that changed from the base of the PR and between 0433cf6 and 7e38040.

📒 Files selected for processing (1)
  • tests/IndexTest.php

📝 Walkthrough

Walkthrough

Index::refresh() is modified to pass ['index' => $this->getName()] to the client's refresh call, targeting the specific index endpoint instead of the cluster-wide refresh. A test validates the request path and changelog documents the fix.

Index Refresh Targeting Fix

Layer / File(s) Summary
Index refresh targeting fix
src/Index.php, tests/IndexTest.php, CHANGELOG.md
Index::refresh() now passes ['index' => $this->getName()] to scope the refresh to the targeted index endpoint instead of cluster-wide. testRefreshTargetsIndexAndNotCluster() validates the request path includes the index name in /{index}/_refresh format. Changelog documents the fix.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A refresh so focused, no more cluster-wide,
Just one index bathed in Elastica's tide,
The test ensures we hit the mark,
No cluster-spanning refresh in the dark!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: scope Index::refresh() to the target index (#2313)' directly and concisely describes the main change: fixing Index::refresh() to target a specific index rather than the cluster.
Linked Issues check ✅ Passed All coding objectives from #2313 are met: Index::refresh() now scopes to the target index via ['index' => $this->getName()], preventing unintended cluster-wide refreshes, and a functional test verifies the fix.
Out of Scope Changes check ✅ Passed All changes are directly related to the stated objective: updating Index::refresh(), adding a test, and documenting the fix in CHANGELOG.md. No extraneous changes detected.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/index-refresh-scope-2313

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.

@jhariani jhariani left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM.

Address Copilot review feedback on #2315: use assertInstanceOf with
Psr\Http\Message\RequestInterface for clearer failures and stronger
typing (consistent with ClientFunctionalTest::testLastRequestResponse).
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.

Elastica\Index::refresh() calls cluster-wide /_refresh instead of {index}/_refresh

3 participants