當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Document::setDocAsUpsert方法代碼示例

本文整理匯總了PHP中Elastica\Document::setDocAsUpsert方法的典型用法代碼示例。如果您正苦於以下問題:PHP Document::setDocAsUpsert方法的具體用法?PHP Document::setDocAsUpsert怎麽用?PHP Document::setDocAsUpsert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Elastica\Document的用法示例。


在下文中一共展示了Document::setDocAsUpsert方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testUpdate

 /**
  * @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();
//.........這裏部分代碼省略.........
開發者ID:MediaWiki-stable,項目名稱:1.26.1,代碼行數:101,代碼來源:BulkTest.php

示例2: update

 /**
  * 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);
     }
 }
開發者ID:biberlabs,項目名稱:zf2-boilerplate,代碼行數:23,代碼來源:AbstractIndexService.php

示例3: makeDocument

 /**
  * @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;
 }
開發者ID:jiangyu7408,項目名稱:notification,代碼行數:11,代碼來源:MappingTest.php

示例4: testDocAsUpsert

 /**
  * @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']);
 }
開發者ID:bungkoko,項目名稱:Elastica,代碼行數:26,代碼來源:ClientTest.php

示例5: makeDocument

 /**
  * @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;
 }
開發者ID:jiangyu7408,項目名稱:notification,代碼行數:11,代碼來源:GatewayUserPersist.php


注:本文中的Elastica\Document::setDocAsUpsert方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。