3

how to update multiple datas in elastica ? the logic is like , I have an array or a string of id, then I need to update the status property of all the datas connected to each or to that id in one go, so how to do that?, this document don't have sample

http://elastica.io/api/classes/Elastica.Bulk.Action.UpdateDocument.html

is that even possible ?

sasori
  • 5,249
  • 16
  • 86
  • 138

3 Answers3

5

It's possible, but you can't update just one field alone, because ES don't works this way.

You need to retrieve full document first, change field value and save document to ES again (with the same id as before).

Except this, multiple upsert (there are no difference between add and update operations) looks like below:

$client = new Elastica\Client();

// Call method from client
$client->addDocuments([
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
]);

// Or from index
$client->getIndex('index')->addDocuments([
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
]);

// Or from type
$client->getIndex('index')->getType('type')->addDocuments([
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
]);
Igor Denisenko
  • 254
  • 2
  • 7
4

It's possible and you can update only one field in each document using Bulk and UpdateDocument bulk action:

$client = new Elastica\Client();

$bulk = new Elastica\Bulk();
$bulk->setIndex('index');
$bulk->setType('type');

$bulk->addAction(
    new Elastica\Bulk\Action\UpdateDocument(
        new Elastica\Document('id1', array('one_field'=>'value1'))
    )
);
$bulk->addAction(
    new Elastica\Bulk\Action\UpdateDocument(
        new Elastica\Document('id2', array('one_field'=>'value2'))
    )
);

$bulk->send();
Serge S.
  • 4,855
  • 3
  • 42
  • 46
1

according to the documentation, there is updateDocument function in order to solve my problem, I need to pass an array of documents similar to what you did in your example above

    $index = $this->client->getIndex($this->index);
    $type = $index->getType($this->type);
    $doc = new \Elastica\Document(
            $object->whateverIDisIt, $object, $this->type, $this->index
    );
    $response = $type->updateDocument($doc);

this example I added is for single document only, I meant single update. of course when I say i needed to pass an array, it meant, that thing above is inside a foreach loop for multiple updating of documents. This has solved my problem after some trial and error on my own. I don't know if it's the best practice to update. but it works just the way I want it.

sasori
  • 5,249
  • 16
  • 86
  • 138