/**
  * Switches an index to become the new target for an alias. Only applies for
  * indexes that are set to use aliases.
  *
  * $force will delete an index encountered where an alias is expected.
  *
  * @param IndexConfig $indexConfig
  * @param Index       $index
  * @param bool        $force
  *
  * @throws AliasIsIndexException
  * @throws \RuntimeException
  */
 public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false)
 {
     $client = $index->getClient();
     $aliasName = $indexConfig->getElasticSearchName();
     $oldIndexName = null;
     $newIndexName = $index->getName();
     try {
         $oldIndexName = $this->getAliasedIndex($client, $aliasName);
     } catch (AliasIsIndexException $e) {
         if (!$force) {
             throw $e;
         }
         $this->deleteIndex($client, $aliasName);
     }
     try {
         $aliasUpdateRequest = $this->buildAliasUpdateRequest($oldIndexName, $aliasName, $newIndexName);
         $client->request('_aliases', 'POST', $aliasUpdateRequest);
     } catch (ExceptionInterface $e) {
         $this->cleanupRenameFailure($client, $newIndexName, $e);
     }
     // Delete the old index after the alias has been switched
     if (null !== $oldIndexName) {
         $this->deleteIndex($client, $oldIndexName);
     }
 }
 /**
  * Switches an index to become the new target for an alias. Only applies for
  * indexes that are set to use aliases.
  *
  * $force will delete an index encountered where an alias is expected.
  *
  * @param IndexConfig $indexConfig
  * @param Index $index
  * @param bool $force
  * @throws AliasIsIndexException
  * @throws \RuntimeException
  */
 public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false)
 {
     $client = $index->getClient();
     $aliasName = $indexConfig->getElasticSearchName();
     $oldIndexName = false;
     $newIndexName = $index->getName();
     try {
         $aliasedIndexes = $this->getAliasedIndexes($client, $aliasName);
     } catch (AliasIsIndexException $e) {
         if (!$force) {
             throw $e;
         }
         $this->deleteIndex($client, $aliasName);
         $aliasedIndexes = array();
     }
     if (count($aliasedIndexes) > 1) {
         throw new \RuntimeException(sprintf('Alias %s is used for multiple indexes: [%s].
                 Make sure it\'s either not used or is assigned to one index only', $aliasName, join(', ', $aliasedIndexes)));
     }
     $aliasUpdateRequest = array('actions' => array());
     if (count($aliasedIndexes) === 1) {
         // if the alias is set - add an action to remove it
         $oldIndexName = $aliasedIndexes[0];
         $aliasUpdateRequest['actions'][] = array('remove' => array('index' => $oldIndexName, 'alias' => $aliasName));
     }
     // add an action to point the alias to the new index
     $aliasUpdateRequest['actions'][] = array('add' => array('index' => $newIndexName, 'alias' => $aliasName));
     try {
         $client->request('_aliases', 'POST', $aliasUpdateRequest);
     } catch (ExceptionInterface $renameAliasException) {
         $additionalError = '';
         // if we failed to move the alias, delete the newly built index
         try {
             $index->delete();
         } catch (ExceptionInterface $deleteNewIndexException) {
             $additionalError = sprintf('Tried to delete newly built index %s, but also failed: %s', $newIndexName, $deleteNewIndexException->getMessage());
         }
         throw new \RuntimeException(sprintf('Failed to updated index alias: %s. %s', $renameAliasException->getMessage(), $additionalError ?: sprintf('Newly built index %s was deleted', $newIndexName)), 0, $renameAliasException);
     }
     // Delete the old index after the alias has been switched
     if ($oldIndexName) {
         $oldIndex = new Index($client, $oldIndexName);
         try {
             $oldIndex->delete();
         } catch (ExceptionInterface $deleteOldIndexException) {
             throw new \RuntimeException(sprintf('Failed to delete old index %s with message: %s', $oldIndexName, $deleteOldIndexException->getMessage()), 0, $deleteOldIndexException);
         }
     }
 }
Example #3
0
 public function searchJournal($status = 3, $published = true)
 {
     $bool = new Query\Bool();
     $match = new Query\Match();
     $match->setField('status', $status);
     $bool->addMust($match);
     $match = new Query\Match();
     $match->setField('published', $published);
     $bool->addMust($match);
     if (!empty($this->filter)) {
         foreach ($this->filter as $key => $filter) {
             $filterObj = new Query\Match();
             $this->applyFilter($filterObj, $key, $filter);
             $bool->addMust($filterObj);
         }
     }
     $query = new Query();
     $query->setQuery($bool);
     $query->setFrom(($this->getPage() - 1) * $this->getLimit());
     $query->setSize($this->getLimit());
     $aggregation = new Terms('institution');
     $aggregation->setField('institution.institution_type.id');
     $aggregation->setOrder('_count', 'desc');
     $qb = $this->em->createQueryBuilder();
     $qb->select('count(r.id)')->from('OjsJournalBundle:InstitutionTypes', 'r');
     $aggregation->setSize($qb->getQuery()->getSingleScalarResult());
     $query->addAggregation($aggregation);
     $aggregation = new Terms('subject');
     $aggregation->setField('subjects.id');
     $aggregation->setOrder('_count', 'desc');
     $qb = $this->em->createQueryBuilder();
     $qb->select('count(r.id)')->from('OjsJournalBundle:Subject', 'r');
     $aggregation->setSize($qb->getQuery()->getSingleScalarResult());
     $query->addAggregation($aggregation);
     $search = $this->index->search($query);
     $result = $search->getResults();
     $transformer = new ElasticaToModelTransformer($this->registry, 'OjsJournalBundle:Journal');
     $transformer->setPropertyAccessor($this->propertyAccessor);
     $this->result = $transformer->transform($result);
     $this->setCount($search->getTotalHits());
     $this->addAggregation('institution', $this->transform($search->getAggregation('institution')['buckets'], 'OjsJournalBundle:InstitutionTypes'));
     $this->addAggregation('subject', $this->transform($search->getAggregation('subject')['buckets'], 'OjsJournalBundle:Subject'));
     return $this;
 }