ruflin/Elastica

if_seq_no & if_primary_term params are ignored

Fieldistor opened this issue · 1 comments

In 7.1.0 CHANGELOG.md:
* Added if_seq_no/if_primary_termto replaceversion for [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'
        ]);
    }

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