/** * 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; }
/** * {@inheritdoc} * @see \Silex\ServiceProviderInterface::register() */ public function register(Application $app) { $app['elastic.client'] = $app->share(function () use($app) { $config = $app['config']('elastic.connection', array()); $client = new Client($config); return $client; }); $app['elastic.bulk'] = $app->protect(function ($index) use($app) { $bulk = new Bulk($app['elastic.client']); $bulk->setIndex($index); return $bulk; }); $app['elastic.document'] = $app->protect(function ($type, array $data) { $document = new Document(); $document->setType($type); $document->setData($data); return $document; }); }
/** * 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(); }
/** * @group unit */ public function testGetPath() { $client = $this->_getClient(); $bulk = new Bulk($client); $this->assertEquals('_bulk', $bulk->getPath()); $indexName = 'testIndex'; $bulk->setIndex($indexName); $this->assertEquals($indexName . '/_bulk', $bulk->getPath()); $typeName = 'testType'; $bulk->setType($typeName); $this->assertEquals($indexName . '/' . $typeName . '/_bulk', $bulk->getPath()); }