Skip to content

fix: send if_seq_no/if_primary_term in bulk index, create and update actions - #2336

Open
caioocesar wants to merge 2 commits into
ruflin:9.xfrom
caioocesar:fix/bulk-optimistic-concurrency
Open

caioocesar wants to merge 2 commits into
ruflin:9.xfrom
caioocesar:fix/bulk-optimistic-concurrency

Conversation

@caioocesar

Copy link
Copy Markdown

Fixes #2058

Problem

Document::setSequenceNumber() / setPrimaryTerm() (and the same setters on scripts) are ignored when the document goes through Bulk:

  • Bulk\Action\IndexDocument::_getMetadata() does not list if_seq_no / if_primary_term. CreateDocument and UpdateDocument extend it, so bulk index, create and update actions all send the write unconditionally. A stale document overwrites the newer one instead of failing with a version conflict.
  • Bulk\Action\DeleteDocument lists both keys, but reads them through getOptions(), which runs array_filter() and drops if_seq_no = 0, the sequence number of the first document written to a shard. Index::addDocument() already handles this case explicitly (Add support for seq_no_primary_term search option and if_seq_no/if_primary_term index options #2284).

Change

  • AbstractDocument::_addConcurrencyControlMetadata() adds both options with hasSequenceNumber() / hasPrimaryTerm(), so 0 is kept.
  • IndexDocument (and therefore CreateDocument and UpdateDocument) and DeleteDocument use it.
  • CHANGELOG entry under Fixed.

Elasticsearch accepts if_seq_no / if_primary_term on bulk index, update and delete actions. It rejects them on create ("create operations do not support compare and set"). A create action with these options now gets that error back instead of silently creating the document without the check, which matches what Index::addDocument() does with op_type=create.

Tests

  • New unit tests in tests/Bulk/Action/AbstractDocumentTest.php. They check the action metadata for index, create, update and delete with sequence numbers 5 and 0, for a script-based update, and for a document without the options.
  • Red then green: with src/ reverted to 9.x and the tests kept, 8 of the 12 cases fail, for example testDocumentConcurrencyControlMetadata with data set "update" ('update', 5): Failed asserting that two arrays are identical (the expected 'if_seq_no' => 5, 'if_primary_term' => 2 are missing). With the fix all 12 pass.
  • vendor/bin/phpunit --group=unit on PHP 8.4: 506 tests pass, 1 skipped.
  • php-cs-fixer 3.84.0 (dry run on the changed paths), Rector dry run and PHPStan: clean. A full PHPStan run reports 2 errors in tests/DocumentTest.php, which the same run also reports on unmodified 9.x.
  • Not run: the functional group against Elasticsearch (no Docker on this machine), and PHP 8.1. The change uses no syntax newer than PHP 8.1.

AI disclosure

The code, tests and this description were written by an AI agent (Claude Code, model Claude Opus 5.5) following this repository's AGENTS.md, and published from my account.

🤖 Generated with Claude Code

caioocesar and others added 2 commits November 13, 2026 17:21
…actions

Bulk IndexDocument (and CreateDocument/UpdateDocument, which extend it)
left the optimistic concurrency control options out of the action
metadata, so they were silently ignored. DeleteDocument included them but
read them through getOptions(), which filters falsy values and dropped
if_seq_no = 0 (the first document in a shard).

Fixes ruflin#2058

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

if_seq_no & if_primary_term params are ignored

1 participant