addAction() public method

public addAction ( Action $action )
$action Elastica\Bulk\Action
Beispiel #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();
 }
Beispiel #2
0
 /**
  * Reindex documents from an old index to a new index.
  *
  * @link https://www.elastic.co/guide/en/elasticsearch/guide/master/reindex.html
  *
  * @param \Elastica\Index $oldIndex
  * @param \Elastica\Index $newIndex
  * @param array           $options  keys: CrossIndex::OPTION_* constants
  *
  * @return \Elastica\Index The new index object
  */
 public static function reindex(Index $oldIndex, Index $newIndex, array $options = array())
 {
     // prepare search
     $search = new Search($oldIndex->getClient());
     $options = array_merge(array(self::OPTION_TYPE => null, self::OPTION_QUERY => new MatchAll(), self::OPTION_EXPIRY_TIME => '1m', self::OPTION_SIZE_PER_SHARD => 1000), $options);
     $search->addIndex($oldIndex);
     if (isset($options[self::OPTION_TYPE])) {
         $type = $options[self::OPTION_TYPE];
         $search->addTypes(is_array($type) ? $type : array($type));
     }
     $search->setQuery($options[self::OPTION_QUERY]);
     // search on old index and bulk insert in new index
     $scanAndScroll = new ScanAndScroll($search, $options[self::OPTION_EXPIRY_TIME], $options[self::OPTION_SIZE_PER_SHARD]);
     foreach ($scanAndScroll as $resultSet) {
         $bulk = new Bulk($newIndex->getClient());
         $bulk->setIndex($newIndex);
         foreach ($resultSet as $result) {
             $action = new Bulk\Action();
             $action->setType($result->getType());
             $action->setId($result->getId());
             $action->setSource($result->getData());
             $bulk->addAction($action);
         }
         $bulk->send();
     }
     $newIndex->refresh();
     return $newIndex;
 }
Beispiel #3
0
 /**
  * Deletes documents with the given ids, index, type from the index
  *
  * @throws \Elastica\Exception\InvalidException
  * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
  *
  * @param  array                      $ids     Document ids
  * @param  string|\Elastica\Index     $index   Index name
  * @param  string|\Elastica\Type      $type    Type of documents
  * @param  string|false               $routing Optional routing key for all ids
  * @return \Elastica\Bulk\ResponseSet Response  object
  */
 public function deleteIds(array $ids, $index, $type, $routing = false)
 {
     if (empty($ids)) {
         throw new InvalidException('Array has to consist of at least one id');
     }
     $bulk = new Bulk($this);
     $bulk->setIndex($index);
     $bulk->setType($type);
     foreach ($ids as $id) {
         $action = new Action(Action::OP_TYPE_DELETE);
         $action->setId($id);
         if (!empty($routing)) {
             $action->setRouting($routing);
         }
         $bulk->addAction($action);
     }
     return $bulk->send();
 }