ruflin / Elastica

Elastica is a PHP client for elasticsearch
http://elastica.io/
MIT License
2.26k stars 737 forks source link

if_seq_no & if_primary_term params are ignored #2058

Open Fieldistor opened 2 years ago

Fieldistor commented 2 years ago

In 7.1.0 CHANGELOG.md: * Addedif_seq_no/if_primary_termto replaceversionfor [optimistic concurrency control](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/optimistic-concurrency-control.html) [#1803](https://github.com/ruflin/Elastica/pull/1803)

But, even if you can set these params on Action Object - they are ignored (on any version of the library, 7.1.0+). Example of the source code, where exception should arise (because seqNo is incorrect), but doc changes are applied instead:

  $client = $this->factory->getClient();
  $bulk = new \Elastica\Bulk($client);

  $index = new Elastica\Index($client, 'foo');
  $doc = $index->getDocument("bar");
  $_seq_no = $doc->getSequenceNumber() -1 ;
  $_primary_term = $doc->getPrimaryTerm();

  $dDocUpdate = new \Elastica\Document(
      "bar",
      array('d' => 22235),
      'foo'
  );

  $dDocUpdate
      ->setDocAsUpsert(false)
      ->setSequenceNumber($_seq_no)
      ->setPrimaryTerm($_primary_term);

  $bulk->addAction(new \Elastica\Bulk\Action\UpdateDocument($dDocUpdate));

  $r = $bulk->send();

  $g = 0;

Folowing quick fix in ruflin/elastica/src/Bulk/Action/IndexDocument.php solves the problem:

    /**
     * {@inheritdoc}
     */
    protected function _getMetadata(AbstractUpdateAction $action): array
    {
        return $action->getOptions([
            '_index',
            '_id',
            'version',
            'version_type',
            'routing',
            'parent',
            'retry_on_conflict',
            'if_seq_no',
            'if_primary_term'
        ]);
    }
ruflin commented 2 years ago

@Fieldistor Interested to open a PR against the repo with the change? This should make discussions much easier.