/** * @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(); }
/** * Partial Update * * @param string $docId * @param array $data * @param integer $version * * @throws IndexingException * * @return null */ public function update($docId, array $data, $version = 1) { $document = new Document(); $document->setData($data); $document->setId($docId); $document->setDocAsUpsert(true); try { $this->getType($version)->updateDocument($document); } catch (\Exception $e) { throw new IndexingException('Throw exception while updating', $e->getCode(), $e); } }
/** * @param User $user * * @return Document */ protected function makeDocument(User $user) { $document = new Document($user->snsid, (new Factory())->toArray($user)); $document->setDocAsUpsert(true)->setIndex($this->getIndexName())->setType($this->getTypeName()); return $document; }
/** * @group functional */ public function testDocAsUpsert() { $index = $this->_createIndex(); $type = $index->getType('test'); $client = $index->getClient(); //Confirm document one does not exist try { $document = $type->getDocument(1); $this->fail('Exception was not thrown. Maybe the document exists?'); } catch (\Exception $e) { //Ignore the exception because we expect the document to not exist. } $newDocument = new Document(1, array('field1' => 'value1', 'field2' => 'value2')); $newDocument->setDocAsUpsert(true); $client->updateDocument(1, $newDocument, $index->getName(), $type->getName(), array('fields' => '_source')); $document = $type->getDocument(1); $this->assertInstanceOf('Elastica\\Document', $document); $data = $document->getData(); $this->assertArrayHasKey('field1', $data); $this->assertEquals('value1', $data['field1']); $this->assertArrayHasKey('field2', $data); $this->assertEquals('value2', $data['field2']); }
/** * @param array $user * * @return Document */ protected function makeDocument(array $user) { $document = new Document($user['snsid'], $user); $document->setDocAsUpsert(true)->setIndex($this->target->index)->setType($this->target->type); return $document; }