Example #1
1
 /**
  * @group functional
  */
 public function testUpdate()
 {
     $index = $this->_createIndex();
     $type = $index->getType('bulk_test');
     $client = $index->getClient();
     $doc1 = $type->createDocument(1, array('name' => 'John'));
     $doc2 = $type->createDocument(2, array('name' => 'Paul'));
     $doc3 = $type->createDocument(3, array('name' => 'George'));
     $doc4 = $type->createDocument(4, array('name' => 'Ringo'));
     $documents = array($doc1, $doc2, $doc3, $doc4);
     //index some documents
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addDocuments($documents);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     //test updating via document
     $doc2 = $type->createDocument(2, array('name' => 'The Walrus'));
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $updateAction = new \Elastica\Bulk\Action\UpdateDocument($doc2);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(2);
     $docData = $doc->getData();
     $this->assertEquals('The Walrus', $docData['name']);
     //test updating via script
     $script = new \Elastica\Script('ctx._source.name += param1;', array('param1' => ' was Paul'), null, 2);
     $doc2 = new Document();
     $script->setUpsert($doc2);
     $updateAction = Action\AbstractDocument::create($script, Action::OP_TYPE_UPDATE);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc2 = $type->getDocument(2);
     $this->assertEquals('The Walrus was Paul', $doc2->name);
     //test upsert
     $script = new \Elastica\Script('ctx._scource.counter += count', array('count' => 1), null, 5);
     $doc = new Document('', array('counter' => 1));
     $script->setUpsert($doc);
     $updateAction = Action\AbstractDocument::create($script, Action::OP_TYPE_UPDATE);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(5);
     $this->assertEquals(1, $doc->counter);
     //test doc_as_upsert
     $doc = new \Elastica\Document(6, array('test' => 'test'));
     $doc->setDocAsUpsert(true);
     $updateAction = Action\AbstractDocument::create($doc, Action::OP_TYPE_UPDATE);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(6);
     $this->assertEquals('test', $doc->test);
     //test doc_as_upsert with set of documents (use of addDocuments)
     $doc1 = new \Elastica\Document(7, array('test' => 'test1'));
     $doc1->setDocAsUpsert(true);
     $doc2 = new \Elastica\Document(8, array('test' => 'test2'));
     $doc2->setDocAsUpsert(true);
     $docs = array($doc1, $doc2);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $bulk->addDocuments($docs, \Elastica\Bulk\Action::OP_TYPE_UPDATE);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(7);
     $this->assertEquals('test1', $doc->test);
     $doc = $type->getDocument(8);
     $this->assertEquals('test2', $doc->test);
     //test updating via document with json string as data
     $doc3 = $type->createDocument(2);
     $bulk = new Bulk($client);
     $bulk->setType($type);
     $doc3->setData('{"name" : "Paul it is"}');
     $updateAction = new \Elastica\Bulk\Action\UpdateDocument($doc3);
     $bulk->addAction($updateAction);
     $response = $bulk->send();
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $index->refresh();
     $doc = $type->getDocument(2);
     $docData = $doc->getData();
     $this->assertEquals('Paul it is', $docData['name']);
     $index->delete();
 }
Example #2
0
    /**
     * @param  \Elastica\Document                  $document
     * @return \Elastica\Bulk\Action\IndexDocument
     */
    public function setDocument(Document $document)
    {
        parent::setDocument($document);

        $this->setSource($document->getData());

        return $this;
    }
Example #3
0
 /**
  * @param \Elastica\Script\AbstractScript $script
  * @param string                          $opType
  *
  * @return $this
  */
 public function addScript(AbstractScript $script, $opType = null)
 {
     $action = AbstractDocumentAction::create($script, $opType);
     return $this->addAction($action);
 }
Example #4
0
 /**
  * @param \Elastica\Document $document
  * @param string $opType
  * @return \Elastica\Bulk
  */
 public function addDocument(Document $document, $opType = null)
 {
     $action = AbstractDocumentAction::create($document, $opType);
     return $this->addAction($action);
 }